<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\GallerySection;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;

class GallerySectionController extends Controller
{
    public function index(): View
    {
        return view('admin.gallery-sections.index', [
            'items' => GallerySection::query()->orderBy('sort_order')->get(),
        ]);
    }

    public function create(): View
    {
        return view('admin.gallery-sections.form', ['item' => new GallerySection]);
    }

    public function store(Request $request): RedirectResponse
    {
        GallerySection::query()->create($this->validated($request));

        return redirect()->route('admin.gallery-sections.index')->with('success', 'Gallery section created.');
    }

    public function edit(GallerySection $gallerySection): View
    {
        return view('admin.gallery-sections.form', ['item' => $gallerySection]);
    }

    public function update(Request $request, GallerySection $gallerySection): RedirectResponse
    {
        $gallerySection->update($this->validated($request));

        return redirect()->route('admin.gallery-sections.index')->with('success', 'Gallery section updated.');
    }

    public function destroy(GallerySection $gallerySection): RedirectResponse
    {
        $gallerySection->delete();

        return redirect()->route('admin.gallery-sections.index')->with('success', 'Gallery section deleted.');
    }

    /** @return array<string, mixed> */
    private function validated(Request $request): array
    {
        return $request->validate([
            'name' => 'required|string|max:255',
            'subtitle' => 'nullable|string|max:255',
            'slug' => 'required|string|max:255',
            'sort_order' => 'required|integer|min:0',
        ]);
    }
}
