<?php

namespace App\Command;

use App\Service\DiscordWebhookService;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

#[AsCommand(
    name: 'app:test-discord-webhook',
    description: 'Tests the Discord Webhook Service',
)]
class TestDiscordWebhookCommand extends Command
{
    public function __construct(
        private DiscordWebhookService $discordWebhookService
    ) {
        parent::__construct();
    }

    protected function configure(): void
    {
        $this
            ->addArgument('message', InputArgument::OPTIONAL, 'The message to send', 'Hello from Symfony!')
            ->addOption('embed', null, null, 'Send as an embed')
        ;
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $io = new SymfonyStyle($input, $output);
        $message = $input->getArgument('message');
        $useEmbed = $input->getOption('embed');

        $io->note(sprintf('Sending message: %s', $message));

        try {
            if ($useEmbed) {
                $this->discordWebhookService->send(null, [[
                    'title' => 'Test Embed',
                    'description' => $message,
                    'color' => 5763719, // Green
                    'fields' => [
                        ['name' => 'Field 1', 'value' => 'Value 1', 'inline' => true],
                        ['name' => 'Field 2', 'value' => 'Value 2', 'inline' => true],
                    ],
                    'footer' => ['text' => 'Sent from CLI'],
                    'timestamp' => date('c'),
                ]]);
            } else {
                $this->discordWebhookService->send($message);
            }
            $io->success('Message sent successfully (or error logged if failed).');
        } catch (\Exception $e) {
            $io->error('Error sending message: ' . $e->getMessage());
            return Command::FAILURE;
        }

        return Command::SUCCESS;
    }
}
