<?php

declare(strict_types=1);

namespace App\Jobs;

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

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

    public int $tries = 1;

    public int $timeout = 120;

    public function __construct(
        private readonly int $afterId = 0,
        private readonly int $batchSize = 200,
        private readonly bool $publicOnly = false,
        private readonly int $staleHours = 0,
    ) {
        $queue = (string) config('recommendations.queue', config('vision.queue', 'default'));
        if ($queue !== '') {
            $this->onQueue($queue);
        }
    }

    public function handle(): void
    {
        $batch = max(1, min($this->batchSize, 1000));
        $staleHours = max(0, $this->staleHours);
        $staleBefore = $staleHours > 0 ? now()->subHours($staleHours) : null;

        $query = Artwork::query()
            ->where('id', '>', $this->afterId)
            ->whereNotNull('hash')
            ->whereHas('embeddings')
            ->when($this->publicOnly, static fn ($query) => $query->public()->published())
            ->orderBy('id')
            ->limit($batch);

        if ($staleBefore !== null) {
            $query->where(static function ($innerQuery) use ($staleBefore): void {
                $innerQuery->whereNull('last_vector_indexed_at')
                    ->orWhere('last_vector_indexed_at', '<=', $staleBefore);
            });
        }

        $artworks = $query->get(['id']);

        if ($artworks->isEmpty()) {
            return;
        }

        foreach ($artworks as $artwork) {
            SyncArtworkVectorIndexJob::dispatch((int) $artwork->id);
        }

        if ($artworks->count() === $batch) {
            $lastId = (int) $artworks->last()->id;
            self::dispatch($lastId, $batch, $this->publicOnly, $staleHours);
        }
    }
}
