<?php

namespace app\process;

use app\service\AiMetadataQueue;
use app\service\ArchiveMetadataEnrichmentService;
use app\service\ArchiveRepository;
use Throwable;
use Workerman\Timer;

class AiMetadata
{
    private AiMetadataQueue $queue;
    private ArchiveRepository $archives;
    private ArchiveMetadataEnrichmentService $enrichment;

    public function __construct()
    {
        $this->queue = new AiMetadataQueue();
        $this->archives = new ArchiveRepository();
        $this->enrichment = new ArchiveMetadataEnrichmentService();
    }

    public function onWorkerStart(): void
    {
        Timer::add(10, fn (): int => $this->queue->releaseDueDelayed());

        while (true) {
            $this->queue->releaseDueDelayed();
            $archiveUid = $this->queue->pop($this->queue->blockTimeout());
            if ($archiveUid === null) {
                continue;
            }

            $this->handle($archiveUid);
        }
    }

    private function handle(string $archiveUid): void
    {
        try {
            $archive = $this->archives->findArchive($archiveUid);
            if ($archive === null) {
                $this->queue->clearRetry($archiveUid);
                return;
            }

            if (!$this->archives->archiveNeedsMetadata($archive)) {
                $this->queue->clearRetry($archiveUid);
                return;
            }

            $payload = $archive;
            $payload['content'] = $this->archives->findChunksText($archiveUid);

            $enriched = $this->enrichment->enrich($payload);
            $aiMeta = $enriched['metadata']['ai_enrichment'] ?? [];

            if (($aiMeta['attempted'] ?? false) !== true || ($aiMeta['error'] ?? null)) {
                $this->queue->retryLater($archiveUid, $aiMeta['error'] ?? 'AI metadata enrichment did not complete.');
                return;
            }

            $fields = [];
            foreach (['title', 'year', 'author', 'tags', 'summary'] as $field) {
                if (array_key_exists($field, $enriched)) {
                    $fields[$field] = $enriched[$field];
                }
            }

            $this->archives->updateMetadata($archiveUid, $fields, $aiMeta);
            $this->queue->clearRetry($archiveUid);
        } catch (Throwable $exception) {
            $this->queue->retryLater($archiveUid, $exception->getMessage());
        }
    }
}
