<?php

namespace Database\Seeders;

use App\Models\User;
use Illuminate\Database\Seeder;
use Spatie\Permission\Models\Role;
use Spatie\Permission\PermissionRegistrar;

class DatabaseSeeder extends Seeder
{
    public function run(): void
    {
        app(PermissionRegistrar::class)->forgetCachedPermissions();

        foreach (['admin', 'finance', 'supervisor', 'staff'] as $role) {
            Role::firstOrCreate(['name' => $role]);
        }

        $accounts = [
            ['Administrator', 'admin@myindependent.com.my', 'admin'],
            ['Finance Officer', 'finance@myindependent.com.my', 'finance'],
            ['Site Supervisor', 'supervisor@myindependent.com.my', 'supervisor'],
        ];

        foreach ($accounts as [$name, $email, $role]) {
            $user = User::firstOrCreate(
                ['email' => $email],
                ['name' => $name, 'password' => 'password']
            );
            $user->syncRoles([$role]);
        }

        // Seed the full country list for the nationality dropdown.
        $this->command->call('nationalities:seed');
    }
}
