<?php

namespace Database\Seeders;

use App\Models\Permission;
use App\Models\Role;
use Illuminate\Database\Seeder;

class RolePermissionSeeder extends Seeder
{
    protected array $permissions = [
        'kabupaten.manage',
        'kecamatan.manage',
        'desa.manage',
        'admin-desa.manage',
        'settings.manage',
        'banner.manage',
        'visitor-log.view',
        'backup.manage',
        'activity-log.view',
        'audit-log.view',
        'role.manage',
        'landing-page.manage',
        'candidate.manage',
        'voter.manage',
        'voter.search',
        'faq.manage',
        'contact.manage',
        'statistic.view',
    ];

    public function run(): void
    {
        $permissions = collect($this->permissions)->mapWithKeys(
            fn (string $name) => [$name => Permission::firstOrCreate(['name' => $name])]
        );

        $superAdmin = Role::firstOrCreate(['name' => 'super_admin']);
        $superAdmin->permissions()->sync($permissions->pluck('id'));

        $adminDesa = Role::firstOrCreate(['name' => 'admin_desa']);
        $adminDesaPermissions = $permissions->only([
            'landing-page.manage',
            'banner.manage',
            'candidate.manage',
            'voter.manage',
            'faq.manage',
            'contact.manage',
            'statistic.view',
        ]);
        $adminDesa->permissions()->sync($adminDesaPermissions->pluck('id'));
    }
}
