<?php

namespace Database\Seeders;

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

class PermissionSeeder extends Seeder
{
    public function run(): void
    {
        $permissions = [
            // Dashboard
            ['module' => 'dashboard', 'action' => 'view', 'name' => 'View Dashboard', 'risk_level' => 'normal'],

            // Customers
            ['module' => 'customers', 'action' => 'view', 'name' => 'View Customers', 'risk_level' => 'normal'],
            ['module' => 'customers', 'action' => 'create', 'name' => 'Create Customers', 'risk_level' => 'normal'],
            ['module' => 'customers', 'action' => 'edit', 'name' => 'Edit Customers', 'risk_level' => 'normal'],
            ['module' => 'customers', 'action' => 'delete', 'name' => 'Delete Customers', 'risk_level' => 'sensitive'],
            ['module' => 'customers', 'action' => 'export', 'name' => 'Export Customers', 'risk_level' => 'sensitive'],

            // Applications
            ['module' => 'applications', 'action' => 'view', 'name' => 'View Applications', 'risk_level' => 'normal'],
            ['module' => 'applications', 'action' => 'create', 'name' => 'Create Applications', 'risk_level' => 'normal'],
            ['module' => 'applications', 'action' => 'edit', 'name' => 'Edit Applications', 'risk_level' => 'normal'],
            ['module' => 'applications', 'action' => 'assign', 'name' => 'Assign Applications', 'risk_level' => 'normal'],
            ['module' => 'applications', 'action' => 'submit', 'name' => 'Submit Applications', 'risk_level' => 'normal'],
            ['module' => 'applications', 'action' => 'approve', 'name' => 'Approve Applications', 'risk_level' => 'sensitive'],
            ['module' => 'applications', 'action' => 'reject', 'name' => 'Reject Applications', 'risk_level' => 'sensitive'],
            ['module' => 'applications', 'action' => 'cancel', 'name' => 'Cancel Applications', 'risk_level' => 'sensitive'],

            // Documents
            ['module' => 'documents', 'action' => 'view', 'name' => 'View Documents', 'risk_level' => 'normal'],
            ['module' => 'documents', 'action' => 'upload', 'name' => 'Upload Documents', 'risk_level' => 'normal'],
            ['module' => 'documents', 'action' => 'download', 'name' => 'Download Documents', 'risk_level' => 'normal'],
            ['module' => 'documents', 'action' => 'delete', 'name' => 'Delete Documents', 'risk_level' => 'sensitive'],

            // Payments
            ['module' => 'payments', 'action' => 'view', 'name' => 'View Payments', 'risk_level' => 'normal'],
            ['module' => 'payments', 'action' => 'collect', 'name' => 'Collect Payments', 'risk_level' => 'normal'],
            ['module' => 'payments', 'action' => 'refund_request', 'name' => 'Request Payment Refund', 'risk_level' => 'sensitive'],
            ['module' => 'payments', 'action' => 'refund_approve', 'name' => 'Approve Payment Refund', 'risk_level' => 'critical'],

            // Invoices
            ['module' => 'invoices', 'action' => 'view', 'name' => 'View Invoices', 'risk_level' => 'normal'],
            ['module' => 'invoices', 'action' => 'create', 'name' => 'Create Invoices', 'risk_level' => 'normal'],
            ['module' => 'invoices', 'action' => 'print', 'name' => 'Print Invoices', 'risk_level' => 'normal'],
            ['module' => 'invoices', 'action' => 'cancel', 'name' => 'Cancel Invoices', 'risk_level' => 'sensitive'],

            // Reports
            ['module' => 'reports', 'action' => 'view', 'name' => 'View Reports', 'risk_level' => 'normal'],
            ['module' => 'reports', 'action' => 'export', 'name' => 'Export Reports', 'risk_level' => 'sensitive'],

            // Employees
            ['module' => 'employees', 'action' => 'view', 'name' => 'View Employees', 'risk_level' => 'normal'],
            ['module' => 'employees', 'action' => 'create', 'name' => 'Create Employees', 'risk_level' => 'sensitive'],
            ['module' => 'employees', 'action' => 'edit', 'name' => 'Edit Employees', 'risk_level' => 'sensitive'],
            ['module' => 'employees', 'action' => 'delete', 'name' => 'Delete Employees', 'risk_level' => 'critical'],

            // HR, Attendance, Leave, Payroll & End of Service
            ['module' => 'shifts', 'action' => 'view', 'name' => 'View Shifts', 'risk_level' => 'normal'],
            ['module' => 'shifts', 'action' => 'manage', 'name' => 'Manage Shifts', 'risk_level' => 'sensitive'],
            ['module' => 'attendance', 'action' => 'view', 'name' => 'View Attendance', 'risk_level' => 'normal'],
            ['module' => 'attendance', 'action' => 'manage', 'name' => 'Record and Import Attendance', 'risk_level' => 'sensitive'],
            ['module' => 'attendance', 'action' => 'approve', 'name' => 'Approve Attendance Corrections', 'risk_level' => 'critical'],
            ['module' => 'leave', 'action' => 'view', 'name' => 'View Leave', 'risk_level' => 'normal'],
            ['module' => 'leave', 'action' => 'create', 'name' => 'Create Leave Requests', 'risk_level' => 'normal'],
            ['module' => 'leave', 'action' => 'approve', 'name' => 'Approve Leave Requests', 'risk_level' => 'sensitive'],
            ['module' => 'payroll', 'action' => 'view', 'name' => 'View Payroll', 'risk_level' => 'sensitive'],
            ['module' => 'payroll', 'action' => 'process', 'name' => 'Calculate and Adjust Payroll', 'risk_level' => 'critical'],
            ['module' => 'payroll', 'action' => 'review', 'name' => 'Review Payroll', 'risk_level' => 'critical'],
            ['module' => 'payroll', 'action' => 'approve', 'name' => 'Review and Approve Payroll', 'risk_level' => 'critical'],
            ['module' => 'payroll', 'action' => 'salary.view', 'name' => 'View Employee Salary', 'risk_level' => 'critical'],
            ['module' => 'payroll', 'action' => 'salary.edit', 'name' => 'Edit Employee Salary', 'risk_level' => 'critical'],
            ['module' => 'payroll', 'action' => 'wps.export', 'name' => 'Prepare WPS Salary Export', 'risk_level' => 'critical'],
            ['module' => 'gratuity', 'action' => 'view', 'name' => 'View Gratuity', 'risk_level' => 'sensitive'],
            ['module' => 'gratuity', 'action' => 'manage', 'name' => 'Calculate Gratuity', 'risk_level' => 'critical'],
            ['module' => 'gratuity', 'action' => 'approve', 'name' => 'Approve Gratuity and Settlement', 'risk_level' => 'critical'],

            // Roles & Permissions
            ['module' => 'roles', 'action' => 'view', 'name' => 'View Roles', 'risk_level' => 'sensitive'],
            ['module' => 'roles', 'action' => 'create', 'name' => 'Create Roles', 'risk_level' => 'critical'],
            ['module' => 'roles', 'action' => 'edit', 'name' => 'Edit Roles', 'risk_level' => 'critical'],
            ['module' => 'roles', 'action' => 'delete', 'name' => 'Delete Roles', 'risk_level' => 'critical'],

            // Settings
            ['module' => 'settings', 'action' => 'view', 'name' => 'View Settings', 'risk_level' => 'normal'],
            ['module' => 'settings', 'action' => 'edit', 'name' => 'Edit Settings', 'risk_level' => 'critical'],

            // Audit
            ['module' => 'audit', 'action' => 'view', 'name' => 'View Audit Logs', 'risk_level' => 'sensitive'],

            // Renewals & CRM
            ['module' => 'renewals', 'action' => 'view', 'name' => 'View Renewals', 'risk_level' => 'normal'],
            ['module' => 'renewals', 'action' => 'manage', 'name' => 'Manage Renewals', 'risk_level' => 'normal'],
            ['module' => 'crm', 'action' => 'view', 'name' => 'View CRM & Leads', 'risk_level' => 'normal'],
            ['module' => 'crm', 'action' => 'manage', 'name' => 'Manage CRM & Leads', 'risk_level' => 'normal'],

            // Advanced operations
            ['module' => 'communications', 'action' => 'view', 'name' => 'View Communication Center', 'risk_level' => 'normal'],
            ['module' => 'communications', 'action' => 'manage', 'name' => 'Send Messages & Manage Templates', 'risk_level' => 'sensitive'],
            ['module' => 'pro', 'action' => 'view', 'name' => 'View PRO Operations', 'risk_level' => 'normal'],
            ['module' => 'pro', 'action' => 'manage', 'name' => 'Manage PRO Jobs & Custody', 'risk_level' => 'sensitive'],
            ['module' => 'queue', 'action' => 'view', 'name' => 'View Queue & Tokens', 'risk_level' => 'normal'],
            ['module' => 'queue', 'action' => 'manage', 'name' => 'Call and Manage Queue Tokens', 'risk_level' => 'normal'],
            ['module' => 'appointments', 'action' => 'view', 'name' => 'View Appointments', 'risk_level' => 'normal'],
            ['module' => 'appointments', 'action' => 'manage', 'name' => 'Create and Manage Appointments', 'risk_level' => 'normal'],
            ['module' => 'tasks', 'action' => 'view', 'name' => 'View Tasks', 'risk_level' => 'normal'],
            ['module' => 'tasks', 'action' => 'manage', 'name' => 'Create and Manage Tasks', 'risk_level' => 'normal'],
            ['module' => 'approvals', 'action' => 'view', 'name' => 'View Approval Requests', 'risk_level' => 'sensitive'],
            ['module' => 'approvals', 'action' => 'manage', 'name' => 'Approve or Reject Requests', 'risk_level' => 'critical'],
        ];

        foreach ($permissions as $p) {
            $slug = $p['module'].'.'.$p['action'];
            Permission::updateOrCreate(['slug' => $slug], array_merge($p, ['slug' => $slug]));
        }
    }
}
