<?php
namespace App\Security;

class RbacCatalog
{
    public const SCOPE_TYPES = ['none', 'self', 'selected_users', 'all'];

    public static function resources(): array
    {
        return [
            'dashboard' => ['title' => 'Dashboard', 'sort' => 10],
            'leads' => ['title' => 'Leads', 'sort' => 20],
            'deals' => ['title' => 'Deals', 'sort' => 30],
            'payments' => ['title' => 'Payments', 'sort' => 40],
            'activities' => ['title' => 'Activities', 'sort' => 50],
            'reminders' => ['title' => 'Reminders', 'sort' => 60],
            'support' => ['title' => 'Support', 'sort' => 70],
            'satisfaction' => ['title' => 'Satisfaction', 'sort' => 80],
            'referrals' => ['title' => 'Referrals', 'sort' => 90],
            'users' => ['title' => 'Users', 'sort' => 100],
            'lookups' => ['title' => 'Lookups', 'sort' => 110],
            'field_config' => ['title' => 'Field configuration', 'sort' => 120],
            'products' => ['title' => 'Products', 'sort' => 130],
            'settings' => ['title' => 'Settings', 'sort' => 140],
            'exports' => ['title' => 'Exports', 'sort' => 150],
            'audit_logs' => ['title' => 'Audit logs', 'sort' => 160],
            'sync' => ['title' => 'Sync', 'sort' => 170],
            'migration' => ['title' => 'Migration', 'sort' => 180],
            'kpi' => ['title' => 'KPI', 'sort' => 190],
            'performance' => ['title' => 'Performance', 'sort' => 200],
            'operation_manager' => ['title' => 'Operation manager', 'sort' => 210],
            'lead_pools' => ['title' => 'Lead pools', 'sort' => 220],
        ];
    }

    public static function resourceKeys(): array
    {
        return array_keys(self::resources());
    }

    public static function isValidResource(string $resourceKey): bool
    {
        return isset(self::resources()[$resourceKey]);
    }

    public static function isValidScopeType(string $scopeType): bool
    {
        return in_array($scopeType, self::SCOPE_TYPES, true);
    }

    public static function capabilities(): array
    {
        $caps = [
            // Menus
            ['key' => 'menu.dashboard', 'title' => 'Menu: Dashboard', 'cat' => 'menu', 'module' => 'dashboard', 'parent' => null, 'sort' => 10],
            ['key' => 'menu.performance_dashboard', 'title' => 'Menu: Performance dashboard', 'cat' => 'menu', 'module' => 'performance', 'parent' => null, 'sort' => 20],
            ['key' => 'menu.leads', 'title' => 'Menu: Leads', 'cat' => 'menu', 'module' => 'leads', 'parent' => null, 'sort' => 30],
            ['key' => 'menu.blataklif_leads', 'title' => 'Menu: Outstanding leads', 'cat' => 'menu', 'module' => 'leads', 'parent' => null, 'sort' => 40],
            ['key' => 'menu.deals', 'title' => 'Menu: Deals', 'cat' => 'menu', 'module' => 'deals', 'parent' => null, 'sort' => 50],
            ['key' => 'menu.legacy_deals', 'title' => 'Menu: Legacy deals', 'cat' => 'menu', 'module' => 'deals', 'parent' => null, 'sort' => 60],
            ['key' => 'menu.referrals', 'title' => 'Menu: Referrals', 'cat' => 'menu', 'module' => 'referrals', 'parent' => null, 'sort' => 70],
            ['key' => 'menu.persons', 'title' => 'Menu: Persons', 'cat' => 'menu', 'module' => 'persons', 'parent' => null, 'sort' => 80],
            ['key' => 'menu.reminders', 'title' => 'Menu: Reminders', 'cat' => 'menu', 'module' => 'reminders', 'parent' => null, 'sort' => 90],
            ['key' => 'menu.settings', 'title' => 'Menu: Settings', 'cat' => 'menu', 'module' => 'settings', 'parent' => null, 'sort' => 100],
            ['key' => 'menu.lookups', 'title' => 'Menu: Lookups', 'cat' => 'menu', 'module' => 'lookups', 'parent' => null, 'sort' => 110],
            ['key' => 'menu.exports', 'title' => 'Menu: Exports', 'cat' => 'menu', 'module' => 'exports', 'parent' => null, 'sort' => 120],
            ['key' => 'menu.audit_logs', 'title' => 'Menu: Audit logs', 'cat' => 'menu', 'module' => 'audit_logs', 'parent' => null, 'sort' => 130],
            ['key' => 'menu.users', 'title' => 'Menu: Users', 'cat' => 'menu', 'module' => 'users', 'parent' => null, 'sort' => 140],
            ['key' => 'menu.financial_dashboard', 'title' => 'Menu: Financial dashboard', 'cat' => 'menu', 'module' => 'financial_dashboard', 'parent' => null, 'sort' => 150],
            ['key' => 'menu.kpi_dashboard', 'title' => 'Menu: KPI dashboard', 'cat' => 'menu', 'module' => 'kpi', 'parent' => null, 'sort' => 160],
            ['key' => 'menu.operation_manager', 'title' => 'Menu: Operation manager', 'cat' => 'menu', 'module' => 'operation_manager', 'parent' => null, 'sort' => 170],
        ];

        $caps = array_merge($caps, [
            ['key' => 'dashboard.view', 'title' => 'View dashboard', 'cat' => 'dashboard', 'module' => 'dashboard', 'parent' => 'menu.dashboard', 'sort' => 180],

            ['key' => 'leads.view', 'title' => 'View leads', 'cat' => 'leads', 'module' => 'leads', 'parent' => 'menu.leads', 'sort' => 200],
            ['key' => 'leads.profile.view', 'title' => 'View lead profile', 'cat' => 'leads', 'module' => 'leads', 'parent' => 'leads.view', 'sort' => 201],
            ['key' => 'leads.lookup_phone', 'title' => 'Lookup lead phone', 'cat' => 'leads', 'module' => 'leads', 'parent' => 'leads.view', 'sort' => 202],
            ['key' => 'leads.create', 'title' => 'Create lead', 'cat' => 'leads', 'module' => 'leads', 'parent' => 'leads.view', 'sort' => 203],
            ['key' => 'leads.edit', 'title' => 'Edit lead', 'cat' => 'leads', 'module' => 'leads', 'parent' => 'leads.view', 'sort' => 204],
            ['key' => 'leads.delete', 'title' => 'Delete lead', 'cat' => 'leads', 'module' => 'leads', 'parent' => 'leads.view', 'sort' => 205],
            ['key' => 'leads.transfer_owner', 'title' => 'Transfer lead owner', 'cat' => 'leads', 'module' => 'leads', 'parent' => 'leads.edit', 'sort' => 206],
            ['key' => 'leads.backfill_products', 'title' => 'Backfill lead products', 'cat' => 'leads', 'module' => 'leads', 'parent' => 'leads.edit', 'sort' => 207],

            ['key' => 'deals.view', 'title' => 'View deals', 'cat' => 'deals', 'module' => 'deals', 'parent' => 'menu.deals', 'sort' => 220],
            ['key' => 'deals.detail.view', 'title' => 'View deal details', 'cat' => 'deals', 'module' => 'deals', 'parent' => 'deals.view', 'sort' => 221],
            ['key' => 'deals.create', 'title' => 'Create deal', 'cat' => 'deals', 'module' => 'deals', 'parent' => 'deals.view', 'sort' => 222],
            ['key' => 'deals.edit', 'title' => 'Edit deal', 'cat' => 'deals', 'module' => 'deals', 'parent' => 'deals.view', 'sort' => 223],
            ['key' => 'deals.delete', 'title' => 'Delete deal', 'cat' => 'deals', 'module' => 'deals', 'parent' => 'deals.view', 'sort' => 224],
            ['key' => 'deals.status.edit', 'title' => 'Edit deal status', 'cat' => 'deals', 'module' => 'deals', 'parent' => 'deals.edit', 'sort' => 225],
            ['key' => 'deals.pipeline.edit', 'title' => 'Edit deal pipeline', 'cat' => 'deals', 'module' => 'deals', 'parent' => 'deals.edit', 'sort' => 226],
            ['key' => 'deals.course.edit', 'title' => 'Edit course status', 'cat' => 'deals', 'module' => 'deals', 'parent' => 'deals.edit', 'sort' => 227],
            ['key' => 'deals.reactivate', 'title' => 'Reactivate deal', 'cat' => 'deals', 'module' => 'deals', 'parent' => 'deals.edit', 'sort' => 228],
            ['key' => 'deals.reconcile', 'title' => 'Reconcile deals', 'cat' => 'deals', 'module' => 'deals', 'parent' => 'deals.edit', 'sort' => 229],
            ['key' => 'deals.force_success', 'title' => 'Force deal success', 'cat' => 'deals', 'module' => 'deals', 'parent' => 'deals.edit', 'sort' => 230],

            ['key' => 'payments.create', 'title' => 'Create payment', 'cat' => 'payments', 'module' => 'payments', 'parent' => 'deals.edit', 'sort' => 240],
            ['key' => 'payments.edit', 'title' => 'Edit payment', 'cat' => 'payments', 'module' => 'payments', 'parent' => 'deals.edit', 'sort' => 241],
            ['key' => 'payments.delete', 'title' => 'Delete payment', 'cat' => 'payments', 'module' => 'payments', 'parent' => 'deals.edit', 'sort' => 242],

            ['key' => 'activities.view', 'title' => 'View activities', 'cat' => 'activities', 'module' => 'activities', 'parent' => null, 'sort' => 260],
            ['key' => 'activities.create', 'title' => 'Create activity', 'cat' => 'activities', 'module' => 'activities', 'parent' => 'activities.view', 'sort' => 261],
            ['key' => 'activities.edit', 'title' => 'Edit activity', 'cat' => 'activities', 'module' => 'activities', 'parent' => 'activities.view', 'sort' => 262],
            ['key' => 'activities.delete', 'title' => 'Delete activity', 'cat' => 'activities', 'module' => 'activities', 'parent' => 'activities.view', 'sort' => 263],
            ['key' => 'activities.types.view', 'title' => 'View activity types', 'cat' => 'activities', 'module' => 'activities', 'parent' => 'activities.view', 'sort' => 264],

            ['key' => 'support.view', 'title' => 'View support', 'cat' => 'support', 'module' => 'support', 'parent' => null, 'sort' => 280],
            ['key' => 'support.create', 'title' => 'Create support', 'cat' => 'support', 'module' => 'support', 'parent' => 'support.view', 'sort' => 281],
            ['key' => 'support.edit', 'title' => 'Edit support', 'cat' => 'support', 'module' => 'support', 'parent' => 'support.view', 'sort' => 282],
            ['key' => 'support.delete', 'title' => 'Delete support', 'cat' => 'support', 'module' => 'support', 'parent' => 'support.view', 'sort' => 283],
            ['key' => 'satisfaction.view', 'title' => 'View satisfaction', 'cat' => 'satisfaction', 'module' => 'satisfaction', 'parent' => null, 'sort' => 290],
            ['key' => 'satisfaction.create', 'title' => 'Create satisfaction', 'cat' => 'satisfaction', 'module' => 'satisfaction', 'parent' => 'satisfaction.view', 'sort' => 291],
            ['key' => 'referrals.view', 'title' => 'View referrals', 'cat' => 'referrals', 'module' => 'referrals', 'parent' => 'menu.referrals', 'sort' => 300],

            ['key' => 'users.view', 'title' => 'View users', 'cat' => 'users', 'module' => 'users', 'parent' => 'menu.users', 'sort' => 320],
            ['key' => 'users.create', 'title' => 'Create users', 'cat' => 'users', 'module' => 'users', 'parent' => 'users.view', 'sort' => 321],
            ['key' => 'users.edit', 'title' => 'Edit users', 'cat' => 'users', 'module' => 'users', 'parent' => 'users.view', 'sort' => 322],
            ['key' => 'users.delete', 'title' => 'Delete users', 'cat' => 'users', 'module' => 'users', 'parent' => 'users.view', 'sort' => 323],
            ['key' => 'users.roles.edit', 'title' => 'Edit user roles', 'cat' => 'users', 'module' => 'users', 'parent' => 'users.edit', 'sort' => 324],
            ['key' => 'users.passwords.change', 'title' => 'Change user passwords', 'cat' => 'users', 'module' => 'users', 'parent' => 'users.edit', 'sort' => 325],
            ['key' => 'users.transfer_data', 'title' => 'Transfer user data', 'cat' => 'users', 'module' => 'users', 'parent' => 'users.edit', 'sort' => 326],
            ['key' => 'users.sync_didar', 'title' => 'Sync Didar users', 'cat' => 'users', 'module' => 'users', 'parent' => 'users.edit', 'sort' => 327],
            ['key' => 'users.scope_options.view', 'title' => 'View users for scopes', 'cat' => 'users', 'module' => 'users', 'parent' => 'users.view', 'sort' => 328],

            ['key' => 'lookups.view', 'title' => 'View lookups', 'cat' => 'lookups', 'module' => 'lookups', 'parent' => 'menu.lookups', 'sort' => 340],
            ['key' => 'lookups.manage', 'title' => 'Manage lookups', 'cat' => 'lookups', 'module' => 'lookups', 'parent' => 'lookups.view', 'sort' => 341],
            ['key' => 'field_config.view', 'title' => 'View field config', 'cat' => 'field_config', 'module' => 'field_config', 'parent' => null, 'sort' => 350],
            ['key' => 'field_config.manage', 'title' => 'Manage field config', 'cat' => 'field_config', 'module' => 'field_config', 'parent' => 'field_config.view', 'sort' => 351],
            ['key' => 'products.view', 'title' => 'View products', 'cat' => 'products', 'module' => 'products', 'parent' => null, 'sort' => 360],
            ['key' => 'products.manage', 'title' => 'Manage products', 'cat' => 'products', 'module' => 'products', 'parent' => 'products.view', 'sort' => 361],
            ['key' => 'products.service_links.manage', 'title' => 'Manage service/product links', 'cat' => 'products', 'module' => 'products', 'parent' => 'products.manage', 'sort' => 362],

            ['key' => 'settings.didar_write.manage', 'title' => 'Manage Didar write settings', 'cat' => 'settings', 'module' => 'settings', 'parent' => 'menu.settings', 'sort' => 380],
            ['key' => 'settings.auto_migration.manage', 'title' => 'Manage auto migration settings', 'cat' => 'settings', 'module' => 'settings', 'parent' => 'menu.settings', 'sort' => 381],
            ['key' => 'settings.external_api.manage', 'title' => 'Manage external API settings', 'cat' => 'settings', 'module' => 'settings', 'parent' => 'menu.settings', 'sort' => 382],
            ['key' => 'settings.master_password.manage', 'title' => 'Manage master password', 'cat' => 'settings', 'module' => 'settings', 'parent' => 'menu.settings', 'sort' => 383],
            ['key' => 'notifications.view', 'title' => 'View notifications', 'cat' => 'notifications', 'module' => 'notifications', 'parent' => null, 'sort' => 390],
            ['key' => 'notifications.manage', 'title' => 'Manage notifications', 'cat' => 'notifications', 'module' => 'notifications', 'parent' => 'notifications.view', 'sort' => 391],
            ['key' => 'reminders.view', 'title' => 'View reminders', 'cat' => 'reminders', 'module' => 'reminders', 'parent' => 'menu.reminders', 'sort' => 400],
            ['key' => 'reminders.manage', 'title' => 'Manage reminders', 'cat' => 'reminders', 'module' => 'reminders', 'parent' => 'reminders.view', 'sort' => 401],
            ['key' => 'audit_logs.view', 'title' => 'View audit logs', 'cat' => 'audit_logs', 'module' => 'audit_logs', 'parent' => 'menu.audit_logs', 'sort' => 410],
            ['key' => 'audit_logs.recover', 'title' => 'Recover audit records', 'cat' => 'audit_logs', 'module' => 'audit_logs', 'parent' => 'audit_logs.view', 'sort' => 411],
            ['key' => 'sync.run', 'title' => 'Run sync', 'cat' => 'sync', 'module' => 'sync', 'parent' => null, 'sort' => 420],
            ['key' => 'migration.preview', 'title' => 'Preview migration', 'cat' => 'migration', 'module' => 'migration', 'parent' => null, 'sort' => 430],
            ['key' => 'migration.run', 'title' => 'Run migration', 'cat' => 'migration', 'module' => 'migration', 'parent' => 'migration.preview', 'sort' => 431],
            ['key' => 'activity_repair.preview', 'title' => 'Preview activity repair', 'cat' => 'maintenance', 'module' => 'activities', 'parent' => null, 'sort' => 440],
            ['key' => 'activity_repair.run', 'title' => 'Run activity repair', 'cat' => 'maintenance', 'module' => 'activities', 'parent' => 'activity_repair.preview', 'sort' => 441],

            ['key' => 'operation_manager.view', 'title' => 'View operation manager', 'cat' => 'operation_manager', 'module' => 'operation_manager', 'parent' => 'menu.operation_manager', 'sort' => 460],
            ['key' => 'operation_manager.preview', 'title' => 'Preview operations', 'cat' => 'operation_manager', 'module' => 'operation_manager', 'parent' => 'operation_manager.view', 'sort' => 461],
            ['key' => 'operation_manager.execute', 'title' => 'Execute operations', 'cat' => 'operation_manager', 'module' => 'operation_manager', 'parent' => 'operation_manager.preview', 'sort' => 462],
            ['key' => 'operation_manager.rules.view', 'title' => 'View operation rules', 'cat' => 'operation_manager', 'module' => 'operation_manager', 'parent' => 'operation_manager.view', 'sort' => 463],
            ['key' => 'operation_manager.rules.manage', 'title' => 'Manage operation rules', 'cat' => 'operation_manager', 'module' => 'operation_manager', 'parent' => 'operation_manager.rules.view', 'sort' => 464],
            ['key' => 'operation_manager.automation.run', 'title' => 'Run operation automation', 'cat' => 'operation_manager', 'module' => 'operation_manager', 'parent' => 'operation_manager.rules.manage', 'sort' => 465],
            ['key' => 'lead_pools.view', 'title' => 'View lead pools', 'cat' => 'lead_pools', 'module' => 'operation_manager', 'parent' => 'operation_manager.view', 'sort' => 470],
            ['key' => 'lead_pools.claim', 'title' => 'Claim pool leads', 'cat' => 'lead_pools', 'module' => 'operation_manager', 'parent' => 'lead_pools.view', 'sort' => 471],
            ['key' => 'lead_pools.assign', 'title' => 'Assign pool leads', 'cat' => 'lead_pools', 'module' => 'operation_manager', 'parent' => 'lead_pools.view', 'sort' => 472],
            ['key' => 'lead_pools.manage', 'title' => 'Manage lead pools', 'cat' => 'lead_pools', 'module' => 'operation_manager', 'parent' => 'lead_pools.view', 'sort' => 473],
            ['key' => 'lead_owner_history.view', 'title' => 'View lead owner history', 'cat' => 'leads', 'module' => 'leads', 'parent' => 'leads.profile.view', 'sort' => 474],
        ]);

        $caps = array_merge($caps, self::analyticsCapabilities(), self::exportCapabilities(), self::legacyAliases());
        $unique = [];
        foreach ($caps as $cap) {
            $unique[$cap['key']] = $cap;
        }
        return array_values($unique);
    }

    private static function analyticsCapabilities(): array
    {
        $caps = [
            ['key' => 'kpi.view', 'title' => 'View KPI dashboard', 'cat' => 'kpi', 'module' => 'kpi', 'parent' => 'menu.kpi_dashboard', 'sort' => 500],
            ['key' => 'kpi.drilldown', 'title' => 'KPI drilldown', 'cat' => 'kpi', 'module' => 'kpi', 'parent' => 'kpi.view', 'sort' => 501],
            ['key' => 'kpi.targets.manage', 'title' => 'Manage KPI targets', 'cat' => 'kpi', 'module' => 'kpi', 'parent' => 'kpi.view', 'sort' => 502],
            ['key' => 'kpi.deals.details', 'title' => 'KPI deal details', 'cat' => 'kpi', 'module' => 'kpi', 'parent' => 'kpi.view', 'sort' => 503],
            ['key' => 'performance.view', 'title' => 'View performance dashboard', 'cat' => 'performance', 'module' => 'performance', 'parent' => 'menu.performance_dashboard', 'sort' => 520],
            ['key' => 'performance.drilldown', 'title' => 'Performance drilldown', 'cat' => 'performance', 'module' => 'performance', 'parent' => 'performance.view', 'sort' => 521],
        ];
        foreach ([
            'chart.daily_sales', 'chart.daily_activities', 'chart.deal_status', 'chart.product_share', 'chart.conversion_funnel',
            'card.sales_total', 'card.outstanding_amount_sum', 'card.settled_count', 'card.lost_count', 'card.won_count', 'card.open_pipeline_count', 'card.without_payment_count',
            'table.activity_summary', 'table.activity_type_details', 'table.deal_status', 'table.lead_status', 'table.conversion_status', 'table.payment_mix', 'table.payment_status', 'table.product_status',
            'table.product_sold_by_owner_day', 'table.product_requests_by_owner_day', 'table.product_failed_by_owner_day', 'table.failure_reasons',
        ] as $idx => $suffix) {
            $caps[] = ['key' => 'performance.' . $suffix, 'title' => 'Performance ' . $suffix, 'cat' => 'performance_widget', 'module' => 'performance', 'parent' => 'performance.view', 'sort' => 530 + $idx];
        }
        return $caps;
    }

    private static function exportCapabilities(): array
    {
        return [
            ['key' => 'export.catalog', 'title' => 'View export catalog', 'cat' => 'export', 'module' => 'exports', 'parent' => 'menu.exports', 'sort' => 600],
            ['key' => 'export.dataset.leads', 'title' => 'Export leads', 'cat' => 'export', 'module' => 'exports', 'parent' => 'export.catalog', 'sort' => 601],
            ['key' => 'export.dataset.deals', 'title' => 'Export deals', 'cat' => 'export', 'module' => 'exports', 'parent' => 'export.catalog', 'sort' => 602],
            ['key' => 'export.dataset.payments', 'title' => 'Export payments', 'cat' => 'export', 'module' => 'exports', 'parent' => 'export.catalog', 'sort' => 603],
        ];
    }

    private static function legacyAliases(): array
    {
        return [
            ['key' => 'action.leads.create', 'title' => 'Alias: Create lead', 'cat' => 'legacy_alias', 'module' => 'leads', 'parent' => 'leads.create', 'sort' => 700],
            ['key' => 'action.leads.edit', 'title' => 'Alias: Edit lead', 'cat' => 'legacy_alias', 'module' => 'leads', 'parent' => 'leads.edit', 'sort' => 701],
            ['key' => 'action.leads.delete', 'title' => 'Alias: Delete lead', 'cat' => 'legacy_alias', 'module' => 'leads', 'parent' => 'leads.delete', 'sort' => 702],
            ['key' => 'action.deals.create', 'title' => 'Alias: Create deal', 'cat' => 'legacy_alias', 'module' => 'deals', 'parent' => 'deals.create', 'sort' => 703],
            ['key' => 'action.deals.edit', 'title' => 'Alias: Edit deal', 'cat' => 'legacy_alias', 'module' => 'deals', 'parent' => 'deals.edit', 'sort' => 704],
            ['key' => 'action.deals.delete', 'title' => 'Alias: Delete deal', 'cat' => 'legacy_alias', 'module' => 'deals', 'parent' => 'deals.delete', 'sort' => 705],
            ['key' => 'action.payments.create', 'title' => 'Alias: Create payment', 'cat' => 'legacy_alias', 'module' => 'payments', 'parent' => 'payments.create', 'sort' => 706],
            ['key' => 'action.payments.edit', 'title' => 'Alias: Edit payment', 'cat' => 'legacy_alias', 'module' => 'payments', 'parent' => 'payments.edit', 'sort' => 707],
            ['key' => 'action.payments.delete', 'title' => 'Alias: Delete payment', 'cat' => 'legacy_alias', 'module' => 'payments', 'parent' => 'payments.delete', 'sort' => 708],
        ];
    }

    public static function aliasMap(): array
    {
        return [
            'menu.dashboard' => 'dashboard.view',
            'menu.leads' => 'leads.view',
            'menu.deals' => 'deals.view',
            'menu.performance_dashboard' => 'performance.view',
            'menu.kpi_dashboard' => 'kpi.view',
            'menu.operation_manager' => 'operation_manager.view',
            'action.leads.create' => 'leads.create',
            'action.leads.edit' => 'leads.edit',
            'action.leads.delete' => 'leads.delete',
            'action.deals.create' => 'deals.create',
            'action.deals.edit' => 'deals.edit',
            'action.deals.delete' => 'deals.delete',
            'action.payments.create' => 'payments.create',
            'action.payments.edit' => 'payments.edit',
            'action.payments.delete' => 'payments.delete',
        ];
    }

    public static function defaultGrants(): array
    {
        $all = array_map(static fn($cap) => $cap['key'], self::capabilities());
        $agent = [
            'menu.dashboard', 'menu.performance_dashboard', 'menu.leads', 'menu.blataklif_leads', 'menu.deals', 'menu.legacy_deals', 'menu.persons', 'menu.reminders', 'menu.settings',
            'dashboard.view',
            'leads.view', 'leads.profile.view', 'leads.lookup_phone', 'leads.edit',
            'deals.view', 'deals.detail.view', 'deals.create', 'deals.edit', 'deals.status.edit', 'deals.pipeline.edit', 'deals.course.edit',
            'payments.create', 'payments.edit',
            'activities.view', 'activities.create', 'activities.edit', 'activities.types.view',
            'notifications.view', 'notifications.manage', 'reminders.view', 'reminders.manage',
            'users.scope_options.view',
            'lookups.view',
            'products.view',
            'performance.view', 'performance.drilldown',
        ];
        foreach (self::analyticsCapabilities() as $cap) {
            if (strpos($cap['key'], 'performance.') === 0) {
                $agent[] = $cap['key'];
            }
        }
        return [
            'admin' => $all,
            'system' => $all,
            'accountant' => ['menu.exports', 'export.catalog', 'export.dataset.payments', 'lookups.view', 'products.view'],
            'crm_specialist' => [
                'menu.dashboard', 'menu.leads', 'menu.blataklif_leads', 'menu.deals', 'menu.legacy_deals', 'menu.referrals', 'menu.persons', 'menu.reminders', 'menu.settings', 'menu.exports',
                'dashboard.view',
                'leads.view', 'leads.profile.view', 'leads.lookup_phone', 'deals.view', 'deals.detail.view', 'deals.edit', 'deals.status.edit', 'deals.pipeline.edit', 'deals.course.edit',
                'payments.create', 'payments.edit',
                'activities.view', 'activities.types.view',
                'support.view', 'support.create', 'satisfaction.view', 'satisfaction.create', 'referrals.view',
                'export.catalog', 'export.dataset.payments', 'notifications.view', 'notifications.manage', 'reminders.view', 'users.scope_options.view', 'lookups.view', 'products.view',
                'operation_manager.view', 'operation_manager.preview', 'operation_manager.rules.view', 'lead_pools.view', 'lead_pools.claim', 'lead_owner_history.view',
            ],
            'agent' => array_values(array_unique(array_merge($agent, [
                'action.leads.edit', 'action.deals.create', 'action.deals.edit', 'action.payments.create', 'action.payments.edit',
            ]))),
        ];
    }

    public static function defaultScopeTypeForRole(string $roleKey): string
    {
        return in_array($roleKey, ['admin', 'system', 'crm_specialist', 'accountant'], true) ? 'all' : 'self';
    }
}
