<?php

declare(strict_types=1);

namespace App\Command;

use App\Vector\VectorSearchClient;
use App\Tag\TagVectorSearchClient;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

#[AsCommand(name: 'mto:agent:test-vector')]
final class TestVectorCommand extends Command
{
    public function __construct(
        private readonly VectorSearchClient $vectorSearchClient,
        private readonly TagVectorSearchClient $tagVectorSearchClient,
    ) {
        parent::__construct();
    }

    protected function configure(): void
    {
        $this->addArgument(
            'prompt',
            InputArgument::REQUIRED,
            'User prompt (realistic retrieval test)'
        );
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $prompt = (string) $input->getArgument('prompt');
        $limit = 8;

        $output->writeln('');
        $output->writeln('<info>Prompt:</info> ' . $prompt);
        $output->writeln('');

        $totalStart = microtime(true);

        // ----------------------------
        // 1️⃣ Tag Routing Phase
        // ----------------------------
        $tagStart = microtime(true);
        $tagResults = $this->tagVectorSearchClient->search($prompt, $limit);
        $tagDuration = (microtime(true) - $tagStart) * 1000;

        // ----------------------------
        // 2️⃣ Chunk Retrieval Phase
        // ----------------------------
        $chunkStart = microtime(true);
        $chunkResults = $this->vectorSearchClient->search($prompt, $limit);
        $chunkDuration = (microtime(true) - $chunkStart) * 1000;

        $totalDuration = (microtime(true) - $totalStart) * 1000;

        // ----------------------------
        // Output
        // ----------------------------
        $output->writeln('<comment>Tag Routing Time:</comment> ' . round($tagDuration, 2) . ' ms');
        $output->writeln('<comment>Chunk Retrieval Time:</comment> ' . round($chunkDuration, 2) . ' ms');
        $output->writeln('<comment>Total Retrieval Time:</comment> ' . round($totalDuration, 2) . ' ms');
        $output->writeln('');

        $output->writeln('--- Tag Results ---');
        $output->writeln(json_encode($tagResults, JSON_PRETTY_PRINT));
        $output->writeln('');

        $output->writeln('--- Chunk Results ---');
        $output->writeln(json_encode($chunkResults, JSON_PRETTY_PRINT));
        $output->writeln('');

        return Command::SUCCESS;
    }
}