<?php

namespace App\Console\Commands;

use App\Models\User;
use Illuminate\Console\Command;

class MakeAdmin extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'app:make-admin {email : The email address of the user to make admin}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Makes someone an admin based on their email address';

    /**
     * Execute the console command.
     */
    public function handle()
    {
        $email = $this->argument('email');

        // Find the user by email
        $user = User::where('email', $email)->first();

        if (!$user) {
            $this->error("User with email {$email} not found.");
            return 1;
        }

        // Check if user is already an admin
        if ($user->hasRole('admin')) {
            $this->info("User {$email} is already an admin.");
            return 0;
        }

        // Assign admin role
        $user->assignRole('admin');

        $this->info("User {$email} has been made an admin successfully.");
        return 0;
    }
}
