<?php

namespace Database\Seeders;

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

class DefaultAdminSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        $email = (string) config('admin.default_email');

        $admin = User::query()->firstOrNew([
            'email' => $email,
        ]);

        $admin->forceFill([
            'name' => $admin->exists ? $admin->name : config('admin.default_name'),
            'email_verified_at' => $admin->email_verified_at ?? now(),
            'role' => 'admin',
        ]);

        if (! $admin->exists || blank($admin->password)) {
            $admin->password = Hash::make((string) config('admin.default_password'));
            $admin->password_set_at = now();
            $admin->must_change_password = true;
        }

        $admin->save();
    }
}
