<?php
/**
 * AYCore Backend Capability Registry
 *
 * This is the canonical feature map used by the web control suite. It keeps
 * panel-specific features discoverable without forcing every backend into a
 * fake common denominator.
 */

if (!function_exists('aycore_panel_capabilities')) {
    function aycore_panel_capabilities(?string $type = null): array
    {
        $all = [
            'marzban' => [
                'label' => 'Marzban', 'family' => 'REST API', 'generation' => 'modern',
                'features' => ['create_user','read_user','update_user','delete_user','enable_disable','reset_traffic','revoke_subscription','extend','extra_volume','extra_time','inbounds','proxies','subscription_link','test_account'],
            ],
            'pasarguard' => [
                'label' => 'PasarGuard API', 'family' => 'REST API + RBAC', 'generation' => 'modern',
                'features' => ['create_user','read_user','update_user','delete_user','enable_disable','reset_traffic','revoke_subscription','extend','extra_volume','extra_time','subscription_link','groups','proxy_settings','admin_roles','reseller_create','reseller_update','reseller_disable','reseller_usage','reseller_users_bulk','reseller_delete','api_key_auth','api_key_manager','bearer_auth','password_auth'],
            ],
            'pasargard' => [
                'label' => 'Pasargard Legacy Alias', 'family' => 'Marzban-compatible', 'generation' => 'legacy',
                'features' => ['create_user','read_user','update_user','delete_user','enable_disable','reset_traffic','revoke_subscription','extend','extra_volume','extra_time','subscription_link'],
            ],
            'rebecca' => [
                'label' => 'Rebecca', 'family' => 'REST API + Services + Nodes', 'generation' => 'modern',
                'features' => [
                    'create_user','read_user','update_user','delete_user','enable_disable','reset_traffic','revoke_subscription','extend','extra_volume','extra_time','subscription_link',
                    'services','service_users','service_limits','service_usage','service_auto_inbound','bulk_user_actions','next_plans','periodic_reset','ip_limit','auto_delete','on_hold',
                    'nodes','node_metrics','node_usage','node_runtime','node_logs','node_certificates','node_tokens','hosts','inbounds','xray_config','reality','warp','outbound_tools',
                    'system_metrics','maintenance','backup_export','backup_import','telegram_settings','webhook_settings','subscription_templates','openapi_explorer','api_key_auth','legacy_login'
                ],
            ],
            'marzneshin' => [
                'label' => 'Marzneshin', 'family' => 'REST API', 'generation' => 'modern',
                'features' => ['create_user','read_user','update_user','delete_user','enable_disable','reset_traffic','extend','extra_volume','extra_time','subscription_link'],
            ],
            'hiddify' => [
                'label' => 'Hiddify', 'family' => 'REST API', 'generation' => 'modern',
                'features' => ['create_user','read_user','update_user','delete_user','enable_disable','extend','extra_volume','extra_time','subscription_link'],
            ],
            'x-ui_single' => [
                'label' => '3x-ui / Sanaei', 'family' => 'Xray panel', 'generation' => 'dual',
                'features' => ['create_user','read_user','update_user','delete_user','enable_disable','reset_traffic','extend','extra_volume','extra_time','subscription_link','inbounds','multi_inbound','auto_inbound','bearer_auth','legacy_login','subscription_info','raw_subscription','tgid_lookup','custom_monthly_reset','identity_tokens','xhttp_session'],
            ],
            'alireza_single' => [
                'label' => 'Alireza Single', 'family' => 'Xray panel', 'generation' => 'legacy',
                'features' => ['create_user','read_user','update_user','delete_user','reset_traffic','extend','extra_volume','extra_time','subscription_link','inbounds'],
            ],
            'guard' => [
                'label' => 'GuardCore', 'family' => 'API', 'generation' => 'modern',
                'features' => ['create_user','read_user','update_user','delete_user','enable_disable','extend','extra_volume','extra_time','service_ids','auto_renew','auto_delete'],
            ],
            'WGDashboard' => [
                'label' => 'WGDashboard', 'family' => 'WireGuard', 'generation' => 'modern',
                'features' => ['create_user','read_user','update_user','delete_user','enable_disable','extend','extra_volume','extra_time','wireguard_profile'],
            ],
            's_ui' => [
                'label' => 'S-UI', 'family' => 'Sing-box', 'generation' => 'modern',
                'features' => ['create_user','read_user','update_user','delete_user','enable_disable','reset_traffic','extend','extra_volume','extra_time','subscription_link','inbounds'],
            ],
            'ibsng' => [
                'label' => 'IBSng', 'family' => 'Accounting', 'generation' => 'legacy',
                'features' => ['create_user','read_user','update_user','delete_user','enable_disable','extend','extra_volume','extra_time','username_password'],
            ],
            'mikrotik' => [
                'label' => 'MikroTik', 'family' => 'RouterOS Accounting', 'generation' => 'legacy',
                'features' => ['create_user','read_user','update_user','delete_user','enable_disable','extend','extra_volume','extra_time','username_password'],
            ],
            'Manualsale' => [
                'label' => 'Manual Inventory', 'family' => 'Stock', 'generation' => 'manual',
                'features' => ['inventory_delivery','manual_credentials'],
            ],
        ];
        if ($type === null) return $all;
        return $all[$type] ?? ['label' => $type, 'family' => 'Unknown', 'generation' => 'unknown', 'features' => []];
    }
}

if (!function_exists('aycore_panel_has_capability')) {
    function aycore_panel_has_capability(string $type, string $feature): bool
    {
        $row = aycore_panel_capabilities($type);
        return in_array($feature, $row['features'] ?? [], true);
    }
}

if (!function_exists('aycore_capability_label')) {
    function aycore_capability_label(string $feature): string
    {
        static $labels = [
            'create_user'=>'ساخت سرویس','read_user'=>'اطلاعات سرویس','update_user'=>'ویرایش سرویس','delete_user'=>'حذف سرویس',
            'enable_disable'=>'فعال/تعلیق','reset_traffic'=>'ریست ترافیک','revoke_subscription'=>'تعویض لینک اشتراک','extend'=>'تمدید زمان',
            'extra_volume'=>'حجم اضافه','extra_time'=>'زمان اضافه','inbounds'=>'مدیریت Inbound','multi_inbound'=>'چند Inbound',
            'auto_inbound'=>'Auto Inbound','proxies'=>'Proxy Settings','subscription_link'=>'لینک اشتراک','test_account'=>'اکانت تست',
            'groups'=>'گروه‌ها','proxy_settings'=>'Proxy Settings','admin_roles'=>'Role Management','reseller_create'=>'ساخت نمایندگی',
            'reseller_update'=>'ویرایش نمایندگی','reseller_disable'=>'تعلیق نمایندگی','reseller_usage'=>'مصرف نمایندگی',
            'reseller_users_bulk'=>'عملیات کاربران نماینده','reseller_delete'=>'حذف نمایندگی','api_key_auth'=>'API Key رسمی','api_key_manager'=>'مدیریت API Key','bearer_auth'=>'Bearer Token','password_auth'=>'API Login',
            'services'=>'Serviceها','service_users'=>'کاربران Service','service_limits'=>'محدودیت Service','service_usage'=>'مصرف Service','service_auto_inbound'=>'Auto-Inbound Service',
            'bulk_user_actions'=>'عملیات گروهی کاربران','next_plans'=>'Next Plans','periodic_reset'=>'ریست دوره‌ای','ip_limit'=>'محدودیت IP','auto_delete'=>'حذف خودکار','on_hold'=>'On-Hold',
            'nodes'=>'Nodeها','node_metrics'=>'متریک Node','node_usage'=>'مصرف Node','node_runtime'=>'کنترل Runtime Node','node_logs'=>'لاگ Node','node_certificates'=>'گواهی Node','node_tokens'=>'توکن Node',
            'hosts'=>'Hostها','xray_config'=>'Xray Config','reality'=>'REALITY','warp'=>'WARP','outbound_tools'=>'Outbound Tools','system_metrics'=>'System Metrics','maintenance'=>'Maintenance',
            'backup_export'=>'Backup Export','backup_import'=>'Backup Import','telegram_settings'=>'Telegram Settings','webhook_settings'=>'Webhook Settings','subscription_templates'=>'Subscription Templates','openapi_explorer'=>'OpenAPI Explorer',
            'subscription_info'=>'Subscription Info 3.6','raw_subscription'=>'Raw Subscription','tgid_lookup'=>'Telegram ID Lookup','custom_monthly_reset'=>'Custom Monthly Reset','identity_tokens'=>'Identity Tokens','xhttp_session'=>'XHTTP Session',
            'legacy_login'=>'Legacy Login','service_ids'=>'Service IDs','auto_renew'=>'Auto Renew','auto_delete'=>'Auto Delete',
            'wireguard_profile'=>'WireGuard Profile','username_password'=>'Username/Password','inventory_delivery'=>'تحویل از انبار',
            'manual_credentials'=>'اطلاعات دستی',
        ];
        return $labels[$feature] ?? str_replace('_', ' ', $feature);
    }
}
