<?php

declare(strict_types=1);

namespace App\Command;

use App\Bootstrap;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

#[AsCommand(name: 'webhook:unsubscribe', description: 'DELETE /subscriptions — remove webhook registration.')]
final class UnsubscribeWebhookCommand extends AbstractCommand
{

    protected function configure(): void
    {
        $this->addArgument('url', InputArgument::OPTIONAL, 'Webhook URL to remove (defaults to WEBHOOK_URL)');
    }

    protected function doExecute(SymfonyStyle $io, InputInterface $input, OutputInterface $output): int
    {
        $config = Bootstrap::envConfig();
        $url = (string) ($input->getArgument('url') ?: $config['webhook.url']);
        if ($url === '') {
            $io->error('URL argument required (or set WEBHOOK_URL).');
            return self::FAILURE;
        }
        $result = $this->api->subscriptions->unsubscribe($url);
        $io->success(sprintf('Unsubscribe %s: success=%s', $url, $result->success ? 'true' : 'false'));
        return $result->success ? self::SUCCESS : self::FAILURE;
    }
}
