<?php

namespace App\Http\Controllers\Media;

use App\Http\Controllers\Controller;
use App\Models\GalleryType;
use App\Http\Requests\StoreGalleryTypeRequest;
use App\Http\Requests\UpdateGalleryTypeRequest;
use App\Services\GalleryService;
use Illuminate\Http\Request;
use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;

/**
 * GalleryTypeController
 *
 * Handles CRUD operations for gallery types.
 * Gallery types categorize galleries and provide organizational structure.
 * Only accessible by users with appropriate permissions.
 */
class GalleryTypeController extends Controller
{
    public function __construct(protected GalleryService $galleryService) {}

    /**
     * Display a listing of gallery types.
     */
    public function index(Request $request): View
    {
        abort_unless(auth()->user()->can('view gallery types'), 403,
            'You do not have permission to view gallery types.');

        $query = GalleryType::with('galleries');

        if ($request->filled('search')) {
            $query->search($request->search);
        }

        $galleryTypes = $query->ordered()->paginate(20);

        return view('media.gallery-types.index', compact('galleryTypes'));
    }

    /**
     * Show the form for creating a new gallery type.
     */
    public function create(): View
    {
        abort_unless(auth()->user()->can('create gallery types'), 403,
            'You do not have permission to create gallery types.');

        return view('media.gallery-types.create');
    }

    /**
     * Store a newly created gallery type in storage.
     */
    public function store(StoreGalleryTypeRequest $request): RedirectResponse
    {
        $galleryType = $this->galleryService->createGalleryType($request->validated());

        return redirect()
            ->route('media.gallery-types.show', $galleryType)
            ->with('success', 'Gallery type created successfully!');
    }

    /**
     * Display the specified gallery type.
     */
    public function show(GalleryType $galleryType): View
    {
        abort_unless(auth()->user()->can('view gallery types'), 403,
            'You do not have permission to view gallery types.');

        $galleryType->load(['galleries' => function ($query) {
            $query->ordered()->paginate(10);
        }]);

        return view('media.gallery-types.show', compact('galleryType'));
    }

    /**
     * Show the form for editing the specified gallery type.
     */
    public function edit(GalleryType $galleryType): View
    {
        abort_unless(auth()->user()->can('edit gallery types'), 403,
            'You do not have permission to edit gallery types.');

        return view('media.gallery-types.edit', compact('galleryType'));
    }

    /**
     * Update the specified gallery type in storage.
     */
    public function update(UpdateGalleryTypeRequest $request, GalleryType $galleryType): RedirectResponse
    {
        $this->galleryService->updateGalleryType($galleryType, $request->validated());

        return redirect()
            ->route('media.gallery-types.show', $galleryType)
            ->with('success', 'Gallery type updated successfully!');
    }

    /**
     * Remove the specified gallery type from storage.
     */
    public function destroy(GalleryType $galleryType): RedirectResponse
    {
        abort_unless(auth()->user()->can('delete gallery types'), 403,
            'You do not have permission to delete gallery types.');

        try {
            $this->galleryService->deleteGalleryType($galleryType);

            return redirect()
                ->route('media.gallery-types.index')
                ->with('success', 'Gallery type deleted successfully!');
        } catch (\RuntimeException $e) {
            return redirect()
                ->route('media.gallery-types.show', $galleryType)
                ->with('error', $e->getMessage());
        }
    }
}
