<?php

namespace App\Console\Commands;

use App\Models\RegisterToken;
use Illuminate\Console\Attributes\Description;
use Illuminate\Console\Attributes\Signature;
use Illuminate\Console\Command;
use Nette\Utils\Random;

#[Signature('app:generate-register-token')]
#[Description('Generate a register token for the internal authentication platform.')]
class GenerateRegisterToken extends Command
{
    /**
     * Execute the console command.
     */
    public function handle()
    {
        $newToken = Random::generate(12, "0-9A-Z");

        try {
            $rt = new RegisterToken;
            $rt->token = $newToken;
            $rt->save();
        } catch (\ErrorException $e) {
            $this->error($e->getMessage());
            return 1;
        }

        $this->info("Register token generated: \n" . $newToken);

        return 0;
    }
}
