<?php

namespace Database\Seeders;

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

class UserSeeder extends Seeder
{
    public function run(): void
    {
        $superAdmin = User::updateOrCreate(
            ['email' => 'superadmin@ipa.test'],
            [
                'name' => 'Super Admin',
                'password' => Hash::make('password'),
                'opd_id' => null,
                'email_verified_at' => now(),
            ]
        );
        $superAdmin->syncRoles([UserRoleEnum::SUPER_ADMIN->value]);

        $opds = Opd::all();

        foreach ($opds as $index => $opd) {
            $email = 'opd' . ($index + 1) . '@ipa.test';

            $opdAdmin = User::updateOrCreate(
                ['email' => $email],
                [
                    'name' => 'Admin ' . $opd->name,
                    'password' => Hash::make('password'),
                    'opd_id' => $opd->id,
                    'email_verified_at' => now(),
                ]
            );
            $opdAdmin->syncRoles([UserRoleEnum::OPD_ADMIN->value]);
        }
    }
}