<?php

namespace Database\Seeders;

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

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

        $master = PermissionCatalog::MASTER;
        $academicClasses = PermissionCatalog::ACADEMIC_CLASSES;
        $manage = PermissionCatalog::MANAGE;
        $finances = PermissionCatalog::FINANCES;
        $services = PermissionCatalog::SERVICES;
        $users = PermissionCatalog::USERS;

        $feedbackSelfService = [
            'view-feedback',
            'create-feedback',
            'update-feedback',
            'delete-feedback',
        ];

        $permissionNames = PermissionCatalog::all();

        foreach ($permissionNames as $name) {
            Permission::firstOrCreate(['name' => $name, 'guard_name' => 'web']);
        }

        app()[PermissionRegistrar::class]->forgetCachedPermissions();

        $roles = [
            'mahasiswa' => [
                'view-dashboard',
                'view-academic-terms',
                'view-academic-advising-logs',
                'view-courses',
                'view-course-registrations',
                'view-letter-requests',
                'create-letter-requests',
                'update-letter-requests',
                'delete-letter-requests',
                'view-schedules',
                'view-materials',
                'view-assignments',
                'submit-assignments',
                'view-announcements',
                ...$feedbackSelfService,
            ],
            'dosen' => [
                'view-dashboard',
                'view-courses',
                'view-academic-advising-logs',
                'view-students',
                'view-course-registrations',
                'approve-course-registrations',
                'reject-course-registrations',
                'view-schedules',
                'view-materials',
                'create-materials',
                'update-materials',
                'delete-materials',
                'view-assignments',
                'create-assignments',
                'update-assignments',
                'delete-assignments',
                'view-assignment-submissions',
                'update-assignment-submissions',
                'view-attendances',
                'create-attendances',
                'delete-attendances',
                'view-letter-requests',
                'create-letter-requests',
                'update-letter-requests',
                'delete-letter-requests',
                'view-announcements',
                ...$feedbackSelfService,
            ],
            'staff-admin' => [
                'view-dashboard',
                ...$master,
                ...$academicClasses,
                ...$manage,
                ...array_diff($services, ['create-letter-requests', 'update-letter-requests']),
                ...$users,
                'view-feedback',
                'update-feedback-status',
                'reply-feedback',
            ],
            'staff-keuangan' => [
                'view-dashboard',
                ...$finances,
                ...$feedbackSelfService,
            ],
            'kaprodi' => [
                'view-dashboard',
                'view-academic-terms',
                'view-courses',
                'view-course-classes',
                'view-course-registrations',
                'view-students',
                ...$feedbackSelfService,
            ],
            'developer' => $permissionNames,
        ];

        foreach ($roles as $roleName => $rolePermissions) {
            $role = Role::firstOrCreate(['name' => $roleName, 'guard_name' => 'web']);
            $role->syncPermissions($rolePermissions);
        }
    }
}
