<?php

namespace App\Command;

use App\Service\S3PostPolicy;
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;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

/**
 * Produit une POST policy S3 signée (mêmes paramètres que /api/s3/sign) pour tester
 * l'upload navigateur→S3 depuis un poste qui a Internet :
 *   php bin/console app:s3:posttest <objectKey>
 * Affiche un JSON {url, fields, redirect}.
 */
#[AsCommand(name: 'app:s3:posttest', description: 'Génère une POST policy S3 signée pour test')]
final class S3PostTestCommand extends Command
{
    public function __construct(
        private readonly S3PostPolicy $policy,
        private readonly UrlGeneratorInterface $urls,
    ) {
        parent::__construct();
    }

    protected function configure(): void
    {
        $this->addArgument('key', InputArgument::REQUIRED, 'Clé de l\'objet S3');
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $redirect = $this->urls->generate('app_s3_done', [], UrlGeneratorInterface::ABSOLUTE_URL);
        $built = $this->policy->build((string) $input->getArgument('key'), 'image/jpeg', 20 * 1024 * 1024, $redirect);
        $output->writeln((string) json_encode([
            'url' => $built['url'],
            'fields' => $built['fields'],
            'redirect' => $redirect,
        ]));

        return Command::SUCCESS;
    }
}
