<?php

declare(strict_types=1);

namespace App\Jobs;

use App\Models\Artwork;
use App\Services\Studio\StudioAiAssistService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

final class AnalyzeArtworkAiAssistJob implements ShouldQueue
{
    use Dispatchable;
    use InteractsWithQueue;
    use Queueable;
    use SerializesModels;

    public int $tries = 3;

    public int $timeout = 45;

    public function __construct(
        private readonly int $artworkId,
        private readonly bool $force = false,
    ) {
        $queue = (string) config('vision.queue', 'default');
        if ($queue !== '') {
            $this->onQueue($queue);
        }
    }

    public function backoff(): array
    {
        return [5, 20, 60];
    }

    public function handle(StudioAiAssistService $aiAssist): void
    {
        $artwork = Artwork::query()->find($this->artworkId);
        if (! $artwork) {
            return;
        }

        $aiAssist->analyze($artwork, $this->force);
    }
}