<?php

namespace Database\Seeders;

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

class RoleAndPermissionSeeder extends Seeder
{
    public function run(): void
    {
        // Reset cached roles and permissions
        app()[\Spatie\Permission\PermissionRegistrar::class]->forgetCachedPermissions();

        // Create permissions
        $permissions = [
            // User management
            'view_users',
            'create_users',
            'edit_users',
            'delete_users',
            'approve_users',

            // Company management
            'view_companies',
            'create_companies',
            'edit_companies',
            'delete_companies',

            // Office management
            'view_offices',
            'create_offices',
            'edit_offices',
            'delete_offices',

            // Attendance management
            'view_attendances',
            'view_all_attendances',
            'edit_attendances',
            'delete_attendances',

            // Business trip management
            'view_business_trips',
            'view_all_business_trips',
            'create_business_trips',
            'edit_business_trips',
            'delete_business_trips',
            'approve_business_trips',

            // Document management
            'view_documents',
            'view_all_documents',
            'upload_documents',
            'edit_documents',
            'delete_documents',
            'approve_documents',
            'receive_hardcopy',

            // Reports
            'view_reports',
            'export_reports',
            'export_spj',

            // Settings
            'manage_settings',
            'manage_roles',
        ];

        foreach ($permissions as $permission) {
            Permission::create(['name' => $permission]);
        }

        // Create roles and assign permissions

        // Super Admin - full access
        $superAdminRole = Role::create(['name' => 'super_admin']);
        $superAdminRole->givePermissionTo(Permission::all());

        // Admin - manage company operations
        $adminRole = Role::create(['name' => 'admin']);
        $adminRole->givePermissionTo([
            'view_users',
            'create_users',
            'edit_users',
            'delete_users',
            'approve_users',
            'view_offices',
            'create_offices',
            'edit_offices',
            'delete_offices',
            'view_all_attendances',
            'edit_attendances',
            'view_all_business_trips',
            'create_business_trips',
            'edit_business_trips',
            'delete_business_trips',
            'approve_business_trips',
            'view_all_documents',
            'upload_documents',
            'edit_documents',
            'delete_documents',
            'approve_documents',
            'receive_hardcopy',
            'view_reports',
            'export_reports',
            'export_spj',
            'manage_settings',
        ]);

        // Manager - mid-level access (can view team data)
        $managerRole = Role::create(['name' => 'manager']);
        $managerRole->givePermissionTo([
            'view_users',
            'view_attendances',
            'view_all_attendances',
            'view_business_trips',
            'view_all_business_trips',
            'approve_business_trips',
            'view_documents',
            'view_all_documents',
            'upload_documents',
            'approve_documents',
            'view_reports',
        ]);

        // Employee - basic access
        $employeeRole = Role::create(['name' => 'employee']);
        $employeeRole->givePermissionTo([
            'view_attendances',
            'view_business_trips',
            'view_documents',
            'upload_documents',
        ]);
    }
}
