<?php

namespace App\Base\Framework\Command;

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 Twig\Environment;

class InitWorker extends Command
{
    public function __construct(
        private Environment $twig,
    ) {
        parent::__construct();
    }

    protected function configure(): void
    {
        $this
            ->setName('init:worker')
            ->addOption('async', null, InputOption::VALUE_OPTIONAL, 'Run the async worker.',false)
            ->addOption('download', null, InputOption::VALUE_OPTIONAL, 'Run the download worker.', false)
            ->addOption('monitor', null, InputOption::VALUE_OPTIONAL, 'Run the monitor worker.', false)
        ;
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $configFile = $this->twig->render("config/supervisord.conf.twig", []);

        if ($this->optionExists($input, $output, 'async')) {
            $configFile .= $this->twig->render("config/async.conf.twig", [
                'replicas' => $this->getOptionValue('async', $input),
            ]) . "\n\n";
        }

        if ($this->optionExists($input, $output, 'download')) {
            $configFile .= $this->twig->render("config/download.conf.twig", [
                'replicas' => $this->getOptionValue('download', $input),
            ]). "\n\n";
        }

        if ($this->optionExists($input, $output, 'monitor')) {
            $configFile .= $this->twig->render("config/monitor.conf.twig", [
                'replicas' => $this->getOptionValue('monitor', $input),
            ]). "\n\n";
        }

        if ("" !== $configFile) {
            $output->writeln("[init:worker] Writing /etc/supervisor/conf.d/supervisord.conf");
            file_put_contents("/etc/supervisor/conf.d/supervisord.conf", $configFile);

            $output->writeln("[init:worker] Starting supervisord");
            shell_exec("/usr/bin/supervisord -c /etc/supervisor/conf.d/supervisord.conf");
            return Command::SUCCESS;
        }

        $output->writeln("[init:worker] No workers selected. Exiting.");
        return Command::FAILURE;
    }

    private function optionExists(InputInterface $input, OutputInterface $output, string $option): bool
    {
        if ($input->getOption($option) !== false) {
            $value = $input->getOption($option) ?? 1;
            $output->writeln("[init:worker] transport: $option // $value // input var");;
            return true;
        }

        $optionKey = 'WORKER_' . strtoupper($option);
        if (array_key_exists($optionKey, $_SERVER) && $_SERVER[$optionKey] !== null && $_SERVER[$optionKey] !== '') {
            $output->writeln("[init:worker] transport: $option // $_SERVER[$optionKey] // env var");
            return true;
        }

        return false;
    }

    private function getOptionValue(string $option, InputInterface $input): int
    {
        if ($input->getOption($option) !== false) {
            return $input->getOption($option) ?? 1;
        }

        $optionKey = 'WORKER_' . strtoupper($option);
        return $_SERVER[$optionKey];
    }
}
