<?php

namespace App\Console\Commands;

use App\Models\User;
use Illuminate\Console\Command;
use Spatie\Permission\Models\Role;

class AssignAdminRole extends Command
{
    protected $signature = 'user:make-admin {user_id?}';
    protected $description = 'Assign admin role to a user';

    public function handle()
    {
        $userId = $this->argument('user_id') ?? 1; // Default to ID 1 if not provided
        $user = User::find($userId);

        if (!$user) {
            $this->error('User not found!');
            return 1;
        }

        // Create admin role if it doesn't exist
        $adminRole = Role::firstOrCreate(['name' => 'admin']);
        
        // Assign the role to user
        $user->assignRole($adminRole);

        $this->info("Admin role assigned to user {$user->name} (ID: {$user->id})");
        return 0;
    }
}