<?php

declare(strict_types=1);

namespace App\Command;

use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\Process;

#[AsCommand(
    name: 'mto:agent:vector:control',
    description: 'Production-safe vector service control (deps/install/start/stop/reload/status)'
)]
final class VectorControlCommand extends Command
{
    public function __construct(
        private readonly string $vectorPythonBin = '.venv/bin/python',
        private readonly string $vectorControlScript = 'python/vector/vector_control.py',
        private readonly string $defaultHost = '0.0.0.0',
        private readonly int $defaultPort = 8090,
        private readonly int $timeoutSeconds = 300,
    ) {
        parent::__construct();
    }

    protected function configure(): void
    {
        $this
            ->addOption('install', null, InputOption::VALUE_NONE, 'Install missing python deps into .venv')
            ->addOption('start', null, InputOption::VALUE_NONE, 'Start service if not running')
            ->addOption('stop', null, InputOption::VALUE_NONE, 'Stop service using PID file')
            ->addOption('force', null, InputOption::VALUE_NONE, 'Force stop (SIGKILL) if needed')
            ->addOption('reload', null, InputOption::VALUE_NONE, 'Trigger /reload')
            ->addOption('status', null, InputOption::VALUE_NONE, 'Print status')
            ->addOption('foreground', null, InputOption::VALUE_NONE, 'Start in foreground (rare)')
            ->addOption('port', null, InputOption::VALUE_OPTIONAL, 'Port', (string) $this->defaultPort)
            ->addOption('host', null, InputOption::VALUE_OPTIONAL, 'Host', $this->defaultHost);
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $cmd = [$this->vectorPythonBin, $this->vectorControlScript];

        if ($input->getOption('install')) {
            $cmd[] = '--install';
        }
        if ($input->getOption('start')) {
            $cmd[] = '--start';
        }
        if ($input->getOption('stop')) {
            $cmd[] = '--stop';
        }
        if ($input->getOption('force')) {
            $cmd[] = '--force';
        }
        if ($input->getOption('reload')) {
            $cmd[] = '--reload';
        }
        if ($input->getOption('status')) {
            $cmd[] = '--status';
        }
        if ($input->getOption('foreground')) {
            $cmd[] = '--foreground';
        }

        $cmd[] = '--port';
        $cmd[] = (string) $input->getOption('port');

        $cmd[] = '--host';
        $cmd[] = (string) $input->getOption('host');

        $process = new Process($cmd);
        $process->setTimeout($this->timeoutSeconds);
        $process->run();

        $output->writeln($process->getOutput());

        return $process->isSuccessful() ? Command::SUCCESS : Command::FAILURE;
    }
}
