<?php

declare(strict_types=1);

namespace App\Http\Controllers\Studio;

use App\Http\Controllers\Controller;
use App\Http\Requests\Groups\StoreGroupAssetRequest;
use App\Http\Requests\Groups\UpdateGroupAssetRequest;
use App\Models\Group;
use App\Models\GroupAsset;
use App\Services\GroupAssetService;
use App\Services\GroupService;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;

class GroupAssetStudioController extends Controller
{
    public function __construct(
        private readonly GroupService $groups,
        private readonly GroupAssetService $assets,
    ) {
    }

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

        return Inertia::render('Studio/StudioGroupAssets', [
            'title' => $group->name . ' Assets',
            'description' => 'Manage reusable group files, templates, brand assets, and reference packs.',
            'studioGroup' => $this->groups->mapGroupDetail($group, $request->user()),
            'listing' => $this->assets->studioListing($group, $request->user(), $request->only(['bucket', 'category', 'q', 'page', 'per_page'])),
            'projectOptions' => $group->projects()->orderBy('title')->get(['id', 'title'])->map(fn ($project): array => ['id' => (int) $project->id, 'title' => $project->title])->values()->all(),
            'categoryOptions' => collect((array) config('groups.assets.categories', []))->map(fn (string $value): array => ['value' => $value, 'label' => ucwords(str_replace('_', ' ', $value))])->values()->all(),
            'visibilityOptions' => collect((array) config('groups.assets.visibility_options', []))->map(fn (string $value): array => ['value' => $value, 'label' => ucwords(str_replace('_', ' ', $value))])->values()->all(),
            'statusOptions' => collect((array) config('groups.assets.statuses', []))->map(fn (string $value): array => ['value' => $value, 'label' => ucfirst($value)])->values()->all(),
            'storeUrl' => $group->canManageAssets($request->user()) ? route('studio.groups.assets.store', ['group' => $group]) : null,
            'updatePattern' => $group->canManageAssets($request->user()) ? route('studio.groups.assets.update', ['group' => $group, 'asset' => '__ASSET__']) : null,
        ]);
    }

    public function store(StoreGroupAssetRequest $request, Group $group): RedirectResponse
    {
        $this->authorize('manageAssets', $group);

        $this->assets->store($group, $request->user(), $request->validated());

        return back()->with('success', 'Asset uploaded.');
    }

    public function update(UpdateGroupAssetRequest $request, Group $group, GroupAsset $asset): RedirectResponse
    {
        $this->authorize('manageAssets', $group);
        abort_unless((int) $asset->group_id === (int) $group->id, 404);

        $this->assets->update($asset, $request->user(), $request->validated());

        return back()->with('success', 'Asset updated.');
    }
}