<?php

namespace App\Console\Commands;

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

class MakeAdmin extends Command
{
    protected $signature = 'zt:make-admin {email} {--remove : Change the user back to the client role}';
    protected $description = 'Grant or remove the ZT portal admin role for an existing user';

    public function handle(): int
    {
        $user = User::where('email', strtolower(trim((string) $this->argument('email'))))->first();
        if (! $user) {
            $this->error('No portal user exists with that email address.');
            return self::FAILURE;
        }

        $user->update(['role' => $this->option('remove') ? 'client' : 'admin']);
        $this->info($user->email.' is now '.$user->role.'.');
        return self::SUCCESS;
    }
}
