<?php

namespace AutoAdmin\Bundle\Admin\Command\Admin;

use AutoAdmin\Bundle\Admin\Service\AdministratorManager;
use Exception;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ChoiceQuestion;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\Console\Question\Question;

#[AsCommand(name: 'aa:admin:create-administrator')]
class CreateAdministratorCommand extends Command
{
    private AdministratorManager $adminService;

    public function __construct(AdministratorManager $adminService)
    {
        $this->adminService = $adminService;

        parent::__construct();
    }

    protected function configure(): void
    {
        $this->setDescription('Create a new Admin account.');
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $outputStyle = new OutputFormatterStyle('red', null);
        $output->getFormatter()->setStyle('error', $outputStyle);

        $adminData = $this->askInformations($input, $output);

        if (!$adminData) {
            $output->writeln("<comment>Command Canceled</comment>");
        }

        try {
            $this->adminService->create(
                $adminData['username'],
                $adminData['email'],
                $adminData['password'],
                $adminData['roles'],
                true
            );
        } catch (Exception $e) {
            $output->writeln("<error>Error: " . $e->getMessage() . "</error>");

            return self::FAILURE;
        }

        $output->writeln("<info>The admin '" . $adminData['username'] . "' has been created</info>");

        return self::SUCCESS;
    }

    private function askInformations(InputInterface $input, OutputInterface $output)
    {
        $helper = $this->getHelper('question');

        // Username
        $usernameQuestion = new Question('Username : ', 'username');

        // Email
        $emailQuestion = new Question('Email : ', 'example@domain.tld');

        // Password
        $passwordQuestion = new Question('Password : ', 'not_secret_change_it');

        // Roles
        $rolesQuestion = new ChoiceQuestion('Roles : ', [
            // This can be dynamic ?
            'ROLE_ADMIN',
            'ROLE_SUPER_ADMIN',
        ], '0');

        $rolesQuestion->setMultiselect(true);

        // Confirmation
        $confirmationQuestion = new ConfirmationQuestion('This informations is correct ? (y/n) ', false);

        // Ask questions to user
        $username = $helper->ask($input, $output, $usernameQuestion);
        $email = $helper->ask($input, $output, $emailQuestion);
        $password = $helper->ask($input, $output, $passwordQuestion);
        $roles = $helper->ask($input, $output, $rolesQuestion);

        // Reformatting values
        $username = strtolower($username);
        $email = strtolower($email);

        foreach ($roles as $k => $value) {
            $roles[$k] = trim(strtoupper($value));
        }

        // User validate infos
        if (!$helper->ask($input, $output, $confirmationQuestion)) {
            return false;
        }

        // Return statements
        return [
            'username' => strtolower($username),
            'email' => strtolower($email),
            'password' => $password,
            'roles' => $roles,
        ];
    }
}
