<?php

namespace Database\Seeders;

use App\enum\UserLevel;
use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;

class AdminSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        $adminEmail = config('app.admin_email');

        if (! $adminEmail) {
            $this->command->error('ADMIN_EMAIL not configured in config/app.php or .env file');

            return;
        }

        // Check if admin user already exists
        $existingAdmin = User::where('email', $adminEmail)->first();
        if ($existingAdmin) {
            $this->command->info("Admin user with email {$adminEmail} already exists");

            return;
        }

        // Ask for admin password
        $password = $this->command->secret('Enter admin password (input will be hidden):');

        if (empty($password)) {
            $this->command->error('Password cannot be empty');

            return;
        }

        // Confirm password
        $passwordConfirmation = $this->command->secret('Confirm admin password:');

        if ($password !== $passwordConfirmation) {
            $this->command->error('Passwords do not match');

            return;
        }

        User::query()->create([
            'name' => 'admin',
            'email' => $adminEmail,
            'password' => Hash::make($password),
            'level' => UserLevel::SUPERADMIN,
            'email_verified_at' => now(),
        ]);

        $this->command->info("Admin user created successfully with email: {$adminEmail}");
    }
}
