<?php

namespace Database\Seeders;

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

class FixedAssetPermissionsSeeder extends Seeder
{
    public function run(): void
    {
        $permissions = [
            // Fixed Asset Catalog
            ['name' => 'warehouse.fixed-assets.view', 'display_name' => 'View Fixed Assets', 'description' => 'Can view fixed assets catalog and details', 'group' => 'warehouse_fixed_assets'],
            ['name' => 'warehouse.fixed-assets.create', 'display_name' => 'Create Fixed Assets', 'description' => 'Can create new fixed assets', 'group' => 'warehouse_fixed_assets'],
            ['name' => 'warehouse.fixed-assets.edit', 'display_name' => 'Edit Fixed Assets', 'description' => 'Can edit fixed assets information', 'group' => 'warehouse_fixed_assets'],
            ['name' => 'warehouse.fixed-assets.delete', 'display_name' => 'Delete Fixed Assets', 'description' => 'Can delete fixed assets', 'group' => 'warehouse_fixed_assets'],

            // Fixed Asset Incoming
            ['name' => 'warehouse.fixed-asset-incoming.view', 'display_name' => 'View Fixed Asset Incoming', 'description' => 'Can view fixed asset incoming receipts', 'group' => 'warehouse_fixed_asset_incoming'],
            ['name' => 'warehouse.fixed-asset-incoming.create', 'display_name' => 'Create Fixed Asset Incoming', 'description' => 'Can create fixed asset incoming receipts', 'group' => 'warehouse_fixed_asset_incoming'],
            ['name' => 'warehouse.fixed-asset-incoming.edit', 'display_name' => 'Edit Fixed Asset Incoming', 'description' => 'Can edit fixed asset incoming receipts', 'group' => 'warehouse_fixed_asset_incoming'],
            ['name' => 'warehouse.fixed-asset-incoming.process', 'display_name' => 'Process Fixed Asset Incoming', 'description' => 'Can approve and complete fixed asset incoming receipts', 'group' => 'warehouse_fixed_asset_incoming'],

            // Fixed Asset Allocations
            ['name' => 'warehouse.fixed-asset-allocations.view', 'display_name' => 'View Fixed Asset Allocations', 'description' => 'Can view fixed asset allocations', 'group' => 'warehouse_fixed_asset_allocations'],
            ['name' => 'warehouse.fixed-asset-allocations.create', 'display_name' => 'Create Fixed Asset Allocations', 'description' => 'Can create fixed asset allocations', 'group' => 'warehouse_fixed_asset_allocations'],
            ['name' => 'warehouse.fixed-asset-allocations.approve', 'display_name' => 'Approve Fixed Asset Allocations', 'description' => 'Can approve fixed asset allocations', 'group' => 'warehouse_fixed_asset_allocations'],
            ['name' => 'warehouse.fixed-asset-allocations.allocate', 'display_name' => 'Allocate Fixed Assets', 'description' => 'Can allocate fixed assets to projects', 'group' => 'warehouse_fixed_asset_allocations'],

            // Fixed Asset Returns
            ['name' => 'warehouse.fixed-asset-returns.view', 'display_name' => 'View Fixed Asset Returns', 'description' => 'Can view fixed asset returns', 'group' => 'warehouse_fixed_asset_returns'],
            ['name' => 'warehouse.fixed-asset-returns.create', 'display_name' => 'Create Fixed Asset Returns', 'description' => 'Can create fixed asset returns', 'group' => 'warehouse_fixed_asset_returns'],
            ['name' => 'warehouse.fixed-asset-returns.process', 'display_name' => 'Process Fixed Asset Returns', 'description' => 'Can inspect and complete fixed asset returns', 'group' => 'warehouse_fixed_asset_returns'],

            // Fixed Asset Maintenance
            ['name' => 'warehouse.fixed-asset-maintenance.view', 'display_name' => 'View Fixed Asset Maintenance', 'description' => 'Can view fixed asset maintenance records', 'group' => 'warehouse_fixed_asset_maintenance'],
            ['name' => 'warehouse.fixed-asset-maintenance.create', 'display_name' => 'Create Fixed Asset Maintenance', 'description' => 'Can create fixed asset maintenance records', 'group' => 'warehouse_fixed_asset_maintenance'],
            ['name' => 'warehouse.fixed-asset-maintenance.edit', 'display_name' => 'Edit Fixed Asset Maintenance', 'description' => 'Can edit fixed asset maintenance records', 'group' => 'warehouse_fixed_asset_maintenance'],
            ['name' => 'warehouse.fixed-asset-maintenance.process', 'display_name' => 'Process Fixed Asset Maintenance', 'description' => 'Can start and complete fixed asset maintenance', 'group' => 'warehouse_fixed_asset_maintenance'],

            // Fixed Asset Depreciation
            ['name' => 'warehouse.fixed-asset-depreciation.view', 'display_name' => 'View Fixed Asset Depreciation', 'description' => 'Can view fixed asset depreciation reports', 'group' => 'warehouse_fixed_asset_depreciation'],
            ['name' => 'warehouse.fixed-asset-depreciation.calculate', 'display_name' => 'Calculate Fixed Asset Depreciation', 'description' => 'Can run depreciation calculations', 'group' => 'warehouse_fixed_asset_depreciation'],

            // Fixed Asset Disposal
            ['name' => 'warehouse.fixed-asset-disposal.view', 'display_name' => 'View Fixed Asset Disposal', 'description' => 'Can view fixed asset disposal records', 'group' => 'warehouse_fixed_asset_disposal'],
            ['name' => 'warehouse.fixed-asset-disposal.create', 'display_name' => 'Create Fixed Asset Disposal', 'description' => 'Can create fixed asset disposal requests', 'group' => 'warehouse_fixed_asset_disposal'],
            ['name' => 'warehouse.fixed-asset-disposal.approve', 'display_name' => 'Approve Fixed Asset Disposal', 'description' => 'Can approve fixed asset disposal requests', 'group' => 'warehouse_fixed_asset_disposal'],
            ['name' => 'warehouse.fixed-asset-disposal.process', 'display_name' => 'Process Fixed Asset Disposal', 'description' => 'Can complete fixed asset disposal', 'group' => 'warehouse_fixed_asset_disposal'],

            // Fixed Asset Inventory / Register
            ['name' => 'warehouse.fixed-asset-inventory.view', 'display_name' => 'View Fixed Asset Inventory', 'description' => 'Can view fixed asset inventory dashboard', 'group' => 'warehouse_fixed_asset_inventory'],
        ];

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

        $rolePermissions = [
            'warehouse_manager' => [
                'warehouse.fixed-assets.view', 'warehouse.fixed-assets.create', 'warehouse.fixed-assets.edit', 'warehouse.fixed-assets.delete',
                'warehouse.fixed-asset-incoming.view', 'warehouse.fixed-asset-incoming.create', 'warehouse.fixed-asset-incoming.edit', 'warehouse.fixed-asset-incoming.process',
                'warehouse.fixed-asset-allocations.view', 'warehouse.fixed-asset-allocations.create', 'warehouse.fixed-asset-allocations.approve', 'warehouse.fixed-asset-allocations.allocate',
                'warehouse.fixed-asset-returns.view', 'warehouse.fixed-asset-returns.create', 'warehouse.fixed-asset-returns.process',
                'warehouse.fixed-asset-maintenance.view', 'warehouse.fixed-asset-maintenance.create', 'warehouse.fixed-asset-maintenance.edit', 'warehouse.fixed-asset-maintenance.process',
                'warehouse.fixed-asset-depreciation.view', 'warehouse.fixed-asset-depreciation.calculate',
                'warehouse.fixed-asset-disposal.view', 'warehouse.fixed-asset-disposal.create', 'warehouse.fixed-asset-disposal.approve', 'warehouse.fixed-asset-disposal.process',
                'warehouse.fixed-asset-inventory.view',
            ],
            'warehouse_supervisor' => [
                'warehouse.fixed-assets.view', 'warehouse.fixed-assets.create', 'warehouse.fixed-assets.edit',
                'warehouse.fixed-asset-incoming.view', 'warehouse.fixed-asset-incoming.create', 'warehouse.fixed-asset-incoming.edit', 'warehouse.fixed-asset-incoming.process',
                'warehouse.fixed-asset-allocations.view', 'warehouse.fixed-asset-allocations.create', 'warehouse.fixed-asset-allocations.approve', 'warehouse.fixed-asset-allocations.allocate',
                'warehouse.fixed-asset-returns.view', 'warehouse.fixed-asset-returns.create', 'warehouse.fixed-asset-returns.process',
                'warehouse.fixed-asset-maintenance.view', 'warehouse.fixed-asset-maintenance.create', 'warehouse.fixed-asset-maintenance.edit', 'warehouse.fixed-asset-maintenance.process',
                'warehouse.fixed-asset-depreciation.view', 'warehouse.fixed-asset-depreciation.calculate',
                'warehouse.fixed-asset-disposal.view', 'warehouse.fixed-asset-disposal.create', 'warehouse.fixed-asset-disposal.approve', 'warehouse.fixed-asset-disposal.process',
                'warehouse.fixed-asset-inventory.view',
            ],
            'warehouse_clerk' => [
                'warehouse.fixed-assets.view', 'warehouse.fixed-assets.create', 'warehouse.fixed-assets.edit',
                'warehouse.fixed-asset-incoming.view', 'warehouse.fixed-asset-incoming.create', 'warehouse.fixed-asset-incoming.edit', 'warehouse.fixed-asset-incoming.process',
                'warehouse.fixed-asset-allocations.view', 'warehouse.fixed-asset-allocations.create', 'warehouse.fixed-asset-allocations.allocate',
                'warehouse.fixed-asset-returns.view', 'warehouse.fixed-asset-returns.create', 'warehouse.fixed-asset-returns.process',
                'warehouse.fixed-asset-maintenance.view', 'warehouse.fixed-asset-maintenance.create', 'warehouse.fixed-asset-maintenance.edit',
                'warehouse.fixed-asset-depreciation.view',
                'warehouse.fixed-asset-disposal.view', 'warehouse.fixed-asset-disposal.create',
                'warehouse.fixed-asset-inventory.view',
            ],
            'project_manager' => [
                'warehouse.fixed-assets.view',
                'warehouse.fixed-asset-allocations.view', 'warehouse.fixed-asset-allocations.create',
                'warehouse.fixed-asset-returns.view', 'warehouse.fixed-asset-returns.create',
                'warehouse.fixed-asset-maintenance.view',
                'warehouse.fixed-asset-depreciation.view',
                'warehouse.fixed-asset-inventory.view',
            ],
            'warehouse_viewer' => [
                'warehouse.fixed-assets.view',
                'warehouse.fixed-asset-incoming.view',
                'warehouse.fixed-asset-allocations.view',
                'warehouse.fixed-asset-returns.view',
                'warehouse.fixed-asset-maintenance.view',
                'warehouse.fixed-asset-depreciation.view',
                'warehouse.fixed-asset-disposal.view',
                'warehouse.fixed-asset-inventory.view',
            ],
        ];

        foreach ($rolePermissions as $roleName => $permNames) {
            $role = Role::where('name', $roleName)->first();
            if ($role) {
                $permissionIds = Permission::whereIn('name', $permNames)->pluck('id')->toArray();
                $existingIds = $role->permissions()->pluck('permissions.id')->toArray();
                $role->permissions()->sync(array_unique(array_merge($existingIds, $permissionIds)));
            }
        }

        $this->command->info('Fixed Asset permissions have been created and assigned to roles successfully!');
    }
}
