<?php

namespace App\Console\Commands;

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

class CreateAdminUser extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'admin:create {email} {password} {--name=系统管理员}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '创建管理员用户';

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

        // 检查用户是否已存在
        if (User::where('email', $email)->exists()) {
            $this->error("用户 {$email} 已存在！");
            return 1;
        }

        // 创建管理员用户
        $admin = User::create([
            'name' => $name,
            'email' => $email,
            'password' => Hash::make($password),
            'email_verified_at' => now(),
        ]);

        $this->info("管理员用户创建成功！");
        $this->info("姓名: {$admin->name}");
        $this->info("邮箱: {$admin->email}");
        $this->info("密码: {$password}");

        return 0;
    }
}