<?php

namespace App\Http\Controllers\Dashboard;

use App\Enums\CatalogReviewStatus;
use App\Http\Controllers\Controller;
use App\Models\Category;
use App\Models\Role;
use App\Models\Venue;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;

/**
 * Cola de moderación: sedes (`venues`) y categorías creadas por organizadores con estado pendiente.
 *
 * Solo administración de plataforma; aprueba registros y los marca como visibles en catálogos públicos.
 */
final class CatalogPendingReviewController extends Controller
{
    private function authorizeStaff(Request $request): void
    {
        $user = $request->user();
        abort_unless(
            $user && ($user->hasRole(Role::SLUG_SUPER_ADMIN) || $user->hasRole(Role::SLUG_ADMINISTRADOR)),
            403
        );
    }

    public function index(Request $request): View
    {
        $this->authorizeStaff($request);

        $pendingVenues = Venue::query()
            ->where('catalog_review_status', CatalogReviewStatus::PendingReview)
            ->with('submittedBy')
            ->orderByDesc('created_at')
            ->get();

        $pendingCategories = Category::query()
            ->where('catalog_review_status', CatalogReviewStatus::PendingReview)
            ->with('submittedBy')
            ->orderByDesc('created_at')
            ->get();

        return view('dashboard.catalog.pending-review', [
            'pendingVenues' => $pendingVenues,
            'pendingCategories' => $pendingCategories,
        ]);
    }

    public function approveVenue(Request $request, Venue $venue): RedirectResponse
    {
        $this->authorizeStaff($request);

        abort_unless($venue->catalog_review_status === CatalogReviewStatus::PendingReview, 404);

        $venue->forceFill([
            'catalog_review_status' => CatalogReviewStatus::Approved,
        ])->save();

        return redirect()
            ->route('dashboard.catalog.review.index')
            ->with('success', __('Sede «:name» aprobada.', ['name' => $venue->name]));
    }

    public function approveCategory(Request $request, Category $category): RedirectResponse
    {
        $this->authorizeStaff($request);

        abort_unless($category->catalog_review_status === CatalogReviewStatus::PendingReview, 404);

        $category->forceFill([
            'catalog_review_status' => CatalogReviewStatus::Approved,
        ])->save();

        return redirect()
            ->route('dashboard.catalog.review.index')
            ->with('success', __('Categoría «:name» aprobada.', ['name' => $category->name]));
    }
}
