<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;

class PermissionSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        $roles = [
            'AboutUs',
            'AboutUsMessage',
            'Academic',
            'Alumni',
            'AlumniMember',
            'ApllyNowSubmission',
            'ApplyNowInfo',
            'BoardMember',
            'Facility',
            'FacilityMember',
            'Footer',
            'Gallery',
            'Game',
            'Header',
            'HeaderLogo',
            'Home',
            'IndividualZone',
            'JoinTeamSubmission',
            'Library',
            'NewEventContain',
            'NewsEvent',
            'OnlineResources',
            'Permission',
            'Program',
            'RequestInfo',
            'RequestInfoSubmission',
            'Role',
            'Semester',
            'StudentLife',
            'StudentLifeEvents',
            'Subject',
            'OfferPopUp',
            'TopHeader',
            'User',
            'Zone',
            'UniPopUp',
        ];

        // Models that should only have view and delete permissions
        $limitedPermissionModels = [
            'RequestInfoSubmission',
            'JoinTeamSubmission',
            'ApllyNowSubmission'
        ];

        foreach ($roles as $role) {
            // Role::firstOrCreate(['name' => $role]);

            if (in_array($role, $limitedPermissionModels)) {
                // Create only view and delete permissions for specific models
                Permission::firstOrCreate(['name' => 'view ' . strtolower($role)]);
                Permission::firstOrCreate(['name' => 'delete ' . strtolower($role)]);
            } else {
                // Create full CRUD permissions for other models
                Permission::firstOrCreate(['name' => 'view ' . strtolower($role)]);
                Permission::firstOrCreate(['name' => 'create ' . strtolower($role)]);
                Permission::firstOrCreate(['name' => 'edit ' . strtolower($role)]);
                Permission::firstOrCreate(['name' => 'delete ' . strtolower($role)]);
                Permission::firstOrCreate(['name' => 'restore ' . strtolower($role)]);
                Permission::firstOrCreate(['name' => 'force delete ' . strtolower($role)]);
            }
        }

        // Create or get admin role and assign all permissions
        $adminRole = Role::firstOrCreate(['name' => 'admin']);
        $permissions = Permission::all();
        $adminRole->syncPermissions($permissions);
    }
}
