<?php

declare(strict_types=1);

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\Artwork;
use App\Services\TagNormalizer;
use App\Services\Vision\VisionService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

/**
 * Synchronous Vision tag suggestions for the upload wizard.
 *
 * POST /api/uploads/{id}/vision-suggest
 *
 * Calls the Vision gateway (/analyze/all) synchronously and returns
 * normalised tag suggestions immediately — without going through the queue.
 * The queue-based AutoTagArtworkJob still runs in the background and writes
 * to the DB; this endpoint gives the user instant pre-fill on Step 2.
 */
final class UploadVisionSuggestController extends Controller
{
    public function __construct(
        private readonly VisionService $vision,
        private readonly TagNormalizer $normalizer,
    ) {}

    public function __invoke(int $id, Request $request): JsonResponse
    {
        $artwork = Artwork::query()->findOrFail($id);
        $this->authorizeOrNotFound($request->user(), $artwork);

        return response()->json([
            'tags' => [],
            'vision_enabled' => false,
            'reason' => 'disabled',
        ]);
    }

    private function authorizeOrNotFound(mixed $user, Artwork $artwork): void
    {
        if (! $user) {
            abort(404);
        }
        if ((int) $artwork->user_id !== (int) $user->id) {
            abort(404);
        }
    }
}
