<?php

namespace App\Console\Commands;

use App\Models\User;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Hash;

class CreateAdmin extends Command
{
    protected $signature = "wherewoof:admin {email} {--password=}";
    protected $description = "Create or promote a user to admin";

    public function handle(): int
    {
        $email = $this->argument("email");
        $password = $this->option("password") ?: "admin" . bin2hex(random_bytes(4));

        $user = User::firstOrCreate(
            ["email" => $email],
            ["name" => "Admin", "password_hash" => Hash::make($password)],
        );
        $user->is_admin = true;
        $user->save();

        $this->info("Admin ready: $email (password: $password)");
        return self::SUCCESS;
    }
}
