<?php

namespace App\Command;

use App\Service\InfraService;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;

/**
 * @codeCoverageIgnore Infrastructure snapshot — reads Docker socket, /proc, network
 */
#[AsCommand(
    name: 'app:infra:snapshot',
    description: 'Generate infrastructure stats snapshot to var/infra.json',
)]
class InfraSnapshotCommand extends Command
{
    public function __construct(
        private InfraService $infra,
        #[Autowire('%kernel.project_dir%')] private string $projectDir,
    ) {
        parent::__construct();
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $data = $this->infra->getAll();
        $data['generated_at'] = (new \DateTimeImmutable())->format(\DateTimeInterface::ATOM);

        $path = $this->projectDir.'/var/infra.json';
        file_put_contents($path, json_encode($data, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE));

        $output->writeln(sprintf('Snapshot written to %s (%d bytes)', $path, filesize($path)));

        return Command::SUCCESS;
    }
}
