<?php

declare(strict_types=1);

namespace App\Http\Controllers\Studio;

use App\Http\Controllers\Controller;
use App\Http\Requests\Groups\ReviewGroupArtworkRequest;
use App\Models\Artwork;
use App\Models\Group;
use App\Services\GroupArtworkReviewService;
use App\Services\GroupService;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;

class GroupReviewStudioController extends Controller
{
    public function __construct(
        private readonly GroupService $groups,
        private readonly GroupArtworkReviewService $reviews,
    ) {
    }

    public function index(Request $request, Group $group): Response
    {
        $this->authorize('viewStudio', $group);

        return Inertia::render('Studio/StudioGroupReviewQueue', [
            'title' => $group->name . ' Review queue',
            'description' => 'Approve, reject, or request changes for artwork submitted under this group identity.',
            'studioGroup' => $this->groups->mapGroupDetail($group, $request->user()),
            'listing' => $this->reviews->listing($group, $request->user(), $request->only(['bucket', 'page', 'per_page'])),
            'recentHistory' => $this->groups->recentHistory($group),
        ]);
    }

    public function approve(ReviewGroupArtworkRequest $request, Group $group, Artwork $artwork): RedirectResponse
    {
        $this->authorize('reviewSubmissions', $group);
        $this->reviews->approve($group, $artwork, $request->user(), $request->validated('review_notes'));

        return back()->with('success', 'Artwork approved and published.');
    }

    public function needsChanges(ReviewGroupArtworkRequest $request, Group $group, Artwork $artwork): RedirectResponse
    {
        $this->authorize('reviewSubmissions', $group);
        $this->reviews->requestChanges($group, $artwork, $request->user(), $request->validated('review_notes'));

        return back()->with('success', 'Changes requested from the uploader.');
    }

    public function reject(ReviewGroupArtworkRequest $request, Group $group, Artwork $artwork): RedirectResponse
    {
        $this->authorize('reviewSubmissions', $group);
        $this->reviews->reject($group, $artwork, $request->user(), $request->validated('review_notes'));

        return back()->with('success', 'Artwork rejected.');
    }
}