<?php

declare(strict_types=1);

namespace App\Command;

use App\Entity\Menu;
use App\Entity\User;
use App\Model\UserGroupPermissions;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\PasswordHasher\Hasher\PasswordHasherFactory;

#[AsCommand(name: 'opengnsys:load-default-user', description: 'Load the default user')]
class LoadDefaultUserAdminCommand extends Command
{
    CONST string PLAIN_PASSWORD = '12345678';
    const string USERNAME = 'ogadmin';

    const string DEFAULT_GROUPS_VIEW = 'card';

    public function __construct(
        private readonly EntityManagerInterface $entityManager
    )
    {
        parent::__construct();
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $factory = new PasswordHasherFactory([
            'auto' => ['algorithm' => 'auto'],
        ]);
        $hasher = $factory->getPasswordHasher('auto');
        $hash = $hasher->hash(self::PLAIN_PASSWORD);

        $user = new User();
        $user->setUsername(self::USERNAME);
        $user->setGroupsView(self::DEFAULT_GROUPS_VIEW);
        $user->setRoles([UserGroupPermissions::ROLE_SUPER_ADMIN]);
        $user->setPassword($hash);

        $this->entityManager->persist($user);
        $this->entityManager->flush();

        return Command::SUCCESS;
    }
}
