<?php

namespace Database\Seeders;

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

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

        // All system modules that need CRUD permissions
        $modules = [
            'dashboard', 'leads', 'customers', 'properties', 'projects', 'blocks',
            'quotations', 'bookings', 'agreements', 'rentals', 'installment_plans',
            'invoices', 'transactions', 'reports', 'agents', 'attendance', 'dealers',
            'vendors', 'site_surveys', 'follow_ups', 'users', 'roles', 'settings',
            'master_data', 'activity_logs'
        ];

        // Actions per module
        $actions = ['view', 'create', 'edit', 'delete'];

        // Generate Permissions
        $permissions = [];
        foreach ($modules as $module) {
            foreach ($actions as $action) {
                // e.g. view_leads, create_leads, edit_leads, delete_leads
                $permissionName = $action . '_' . $module;
                $permissions[] = $permissionName;
                Permission::firstOrCreate(['name' => $permissionName]);
            }
        }

        // Define Default Roles
        $superAdmin = Role::firstOrCreate(['name' => 'Super Admin']);
        $salesManager = Role::firstOrCreate(['name' => 'Sales Manager']);
        $salesAgent = Role::firstOrCreate(['name' => 'Sales Agent']);
        $accountant = Role::firstOrCreate(['name' => 'Accountant']);
        $support = Role::firstOrCreate(['name' => 'Customer Support']);
        
        // Ensure Customer role exists but gets NO permissions assigned automatically
        $customer = Role::firstOrCreate(['name' => 'Customer']);

        // Super Admin gets everything
        $superAdmin->syncPermissions(Permission::all());

        // Sales Manager
        $managerPermissions = array_filter($permissions, function ($p) {
            return !str_contains($p, 'settings') && !str_contains($p, 'roles') && !str_contains($p, 'master_data');
        });
        $salesManager->syncPermissions($managerPermissions);

        // Sales Agent
        $agentModules = ['dashboard', 'leads', 'customers', 'properties', 'projects', 'blocks', 'quotations', 'bookings', 'follow_ups'];
        $agentPermissions = [];
        foreach ($agentModules as $module) {
            $agentPermissions[] = 'view_' . $module;
            $agentPermissions[] = 'create_' . $module;
            $agentPermissions[] = 'edit_' . $module; // Cannot delete
        }
        $salesAgent->syncPermissions($agentPermissions);

        // Accountant
        $financeModules = ['dashboard', 'invoices', 'transactions', 'installment_plans', 'rentals', 'agreements', 'quotations', 'bookings'];
        $accountantPermissions = [];
        foreach ($financeModules as $module) {
            $accountantPermissions[] = 'view_' . $module;
            $accountantPermissions[] = 'create_' . $module;
            $accountantPermissions[] = 'edit_' . $module;
            if (in_array($module, ['invoices', 'transactions'])) {
                $accountantPermissions[] = 'delete_' . $module;
            }
        }
        $accountant->syncPermissions($accountantPermissions);

        // Customer Support
        $supportModules = ['dashboard', 'leads', 'customers', 'properties', 'projects', 'follow_ups'];
        $supportPermissions = [];
        foreach ($supportModules as $module) {
            $supportPermissions[] = 'view_' . $module;
            // Only view, no create/edit/delete
        }
        $support->syncPermissions($supportPermissions);
    }
}
