<?php

declare(strict_types=1);

namespace App\SuperAdmin\Infrastructure\Console;

use App\Administration\Application\Port\PasswordHasher;
use App\Shared\Domain\Clock;
use App\SuperAdmin\Domain\Model\SuperAdmin\SuperAdmin;
use App\SuperAdmin\Domain\Repository\SuperAdminRepository;

use function sprintf;

use Symfony\Component\Console\Attribute\AsCommand;
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 Symfony\Component\Console\Style\SymfonyStyle;

/**
 * Creates an already-activated test super admin for E2E tests.
 */
#[AsCommand(
    name: 'app:dev:create-test-super-admin',
    description: 'Creates a test super admin for E2E tests',
)]
final class CreateTestSuperAdminCommand extends Command
{
    public function __construct(
        private readonly SuperAdminRepository $superAdminRepository,
        private readonly PasswordHasher $passwordHasher,
        private readonly Clock $clock,
    ) {
        parent::__construct();
    }

    protected function configure(): void
    {
        $this
            ->addOption('email', null, InputOption::VALUE_OPTIONAL, 'Email address', 'sadmin@test.com')
            ->addOption('password', null, InputOption::VALUE_OPTIONAL, 'Password (plain text)', 'SuperAdmin123')
            ->addOption('first-name', null, InputOption::VALUE_OPTIONAL, 'First name', 'Super')
            ->addOption('last-name', null, InputOption::VALUE_OPTIONAL, 'Last name', 'Admin');
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $io = new SymfonyStyle($input, $output);

        /** @var string $email */
        $email = $input->getOption('email');
        /** @var string $password */
        $password = $input->getOption('password');
        /** @var string $firstName */
        $firstName = $input->getOption('first-name');
        /** @var string $lastName */
        $lastName = $input->getOption('last-name');

        // Check if super admin already exists
        $existing = $this->superAdminRepository->findByEmail($email);
        if ($existing !== null) {
            $io->warning(sprintf('Super admin with email "%s" already exists.', $email));

            $io->table(
                ['Property', 'Value'],
                [
                    ['Super Admin ID', (string) $existing->id],
                    ['Email', $email],
                    ['Password', $password],
                    ['Status', $existing->status->value],
                ]
            );

            return Command::SUCCESS;
        }

        $hashedPassword = $this->passwordHasher->hash($password);
        $now = $this->clock->now();

        $superAdmin = SuperAdmin::creer(
            email: $email,
            hashedPassword: $hashedPassword,
            firstName: $firstName,
            lastName: $lastName,
            createdAt: $now,
        );

        $this->superAdminRepository->save($superAdmin);

        $io->success('Test super admin created successfully!');

        $io->table(
            ['Property', 'Value'],
            [
                ['Super Admin ID', (string) $superAdmin->id],
                ['Email', $email],
                ['Password', $password],
                ['First Name', $firstName],
                ['Last Name', $lastName],
                ['Status', $superAdmin->status->value],
            ]
        );

        return Command::SUCCESS;
    }
}
