<?php

declare(strict_types=1);

namespace Database\Seeders;

use App\Models\User;
use BezhanSalleh\FilamentShield\Support\Utils;
use Illuminate\Database\Seeder;
use Spatie\Permission\Models\Role;

class RoleSeeder extends Seeder
{
    /**
     * @var list<string>
     */
    private const ROLES = [
        'administrator',
        'hr_manager',
        'supervisor',
        'department_manager',
        'viewer',
    ];

    public function run(): void
    {
        Utils::createRole();

        $guardName = $this->guardName();

        foreach (self::ROLES as $roleName) {
            Role::firstOrCreate([
                'name' => $roleName,
                'guard_name' => $guardName,
            ]);
        }

        $admin = User::query()->where('email', 'admin@company.com')->first();

        if ($admin !== null) {
            $admin->assignRole(Utils::getSuperAdminName());
        }
    }

    private function guardName(): string
    {
        $guard = Utils::getFilamentAuthGuard();

        return $guard !== '' ? $guard : (string) config('auth.defaults.guard', 'web');
    }
}
