<?php

declare(strict_types=1);

namespace App\Jobs;

use App\Services\Uploads\UploadPipelineService;
use App\Jobs\AutoTagArtworkJob;
use App\Jobs\GenerateArtworkEmbeddingJob;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

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

    public function __construct(
        private readonly string $sessionId,
        private readonly string $hash,
        private readonly int $artworkId,
        private readonly ?string $originalFileName = null
    ) {
    }

    public function handle(UploadPipelineService $pipeline): void
    {
        $pipeline->processAndPublish($this->sessionId, $this->hash, $this->artworkId, $this->originalFileName);

        // Auto-tagging is async and must never block publish.
        AutoTagArtworkJob::dispatch($this->artworkId, $this->hash)->afterCommit();
        GenerateArtworkEmbeddingJob::dispatch($this->artworkId, $this->hash)->afterCommit();
    }
}
