<?php

namespace Database\Seeders;

use App\Models\Role;
use App\Models\User;
use App\Services\Authorization\AuthorizationAssignmentService;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;

class AdminUserSeeder extends Seeder
{
    public function run(AuthorizationAssignmentService $authorization): void
    {
        $user = User::query()->firstOrCreate(
            ['email' => 'admin@microfinance.local'],
            [
                'name' => 'System Administrator',
                'employee_number' => 'ADMIN-001',
                'username' => 'admin',
                'email_verified_at' => now(),
                'status' => 'active',
                'password' => Hash::make('Admin@12345'),
            ],
        );

        if (! $user->email_verified_at) {
            $user->forceFill(['email_verified_at' => now()])->save();
        }

        if ($role = Role::where('name', 'super-administrator')->first()) {
            $authorization->assignUserRole($user, $role);
        }
    }
}
