<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Permission extends Model
{
    use HasFactory;

    protected $fillable = [
        'permission_key',
        'name',
        'description',
        'group',
        'scope',
    ];

    /**
     * Scope constants
     */
    const SCOPE_ADMIN = 'admin';
    const SCOPE_CLIENT = 'client';
    const SCOPE_BOTH = 'both';

    /**
     * Get all default permissions for the system.
     */
    public static function getDefaultPermissions(): array
    {
        return array_merge(
            self::getAdminScopePermissions(),
            self::getClientScopePermissions()
        );
    }

    /**
     * Admin-scope permissions (for admin dashboard).
     */
    public static function getAdminScopePermissions(): array
    {
        return [
            // User Management Permissions
            [
                'permission_key' => 'users.view',
                'name' => 'View Users',
                'description' => 'Can view user list and details',
                'group' => 'users',
                'scope' => self::SCOPE_ADMIN,
            ],
            [
                'permission_key' => 'users.create',
                'name' => 'Create Users',
                'description' => 'Can create new users',
                'group' => 'users',
                'scope' => self::SCOPE_ADMIN,
            ],
            [
                'permission_key' => 'users.edit',
                'name' => 'Edit Users',
                'description' => 'Can edit user details',
                'group' => 'users',
                'scope' => self::SCOPE_ADMIN,
            ],
            [
                'permission_key' => 'users.delete',
                'name' => 'Delete Users',
                'description' => 'Can permanently delete users',
                'group' => 'users',
                'scope' => self::SCOPE_ADMIN,
            ],
            [
                'permission_key' => 'users.toggle_status',
                'name' => 'Toggle User Status',
                'description' => 'Can enable/disable users',
                'group' => 'users',
                'scope' => self::SCOPE_ADMIN,
            ],
            [
                'permission_key' => 'users.toggle_sms',
                'name' => 'Toggle SMS Service',
                'description' => 'Can enable/disable SMS service per user',
                'group' => 'users',
                'scope' => self::SCOPE_ADMIN,
            ],
            [
                'permission_key' => 'users.toggle_whatsapp',
                'name' => 'Toggle WhatsApp Service',
                'description' => 'Can enable/disable WhatsApp service per user',
                'group' => 'users',
                'scope' => self::SCOPE_ADMIN,
            ],
            [
                'permission_key' => 'users.toggle_promotions',
                'name' => 'Toggle Promotions',
                'description' => 'Can enable/disable promotions per user',
                'group' => 'users',
                'scope' => self::SCOPE_ADMIN,
            ],
            [
                'permission_key' => 'users.force_logout',
                'name' => 'Force Logout',
                'description' => 'Can force logout users from all devices',
                'group' => 'users',
                'scope' => self::SCOPE_ADMIN,
            ],
            [
                'permission_key' => 'users.reset_password',
                'name' => 'Reset Password',
                'description' => 'Can reset user passwords',
                'group' => 'users',
                'scope' => self::SCOPE_ADMIN,
            ],

            // Template Permissions
            [
                'permission_key' => 'templates.view',
                'name' => 'View Templates',
                'description' => 'Can view message templates',
                'group' => 'templates',
                'scope' => self::SCOPE_ADMIN,
            ],
            [
                'permission_key' => 'templates.create',
                'name' => 'Create Templates',
                'description' => 'Can create new templates',
                'group' => 'templates',
                'scope' => self::SCOPE_ADMIN,
            ],
            [
                'permission_key' => 'templates.edit',
                'name' => 'Edit Templates',
                'description' => 'Can edit templates',
                'group' => 'templates',
                'scope' => self::SCOPE_ADMIN,
            ],
            [
                'permission_key' => 'templates.delete',
                'name' => 'Delete Templates',
                'description' => 'Can delete templates',
                'group' => 'templates',
                'scope' => self::SCOPE_ADMIN,
            ],
            [
                'permission_key' => 'templates.assign',
                'name' => 'Assign Templates',
                'description' => 'Can assign templates to users',
                'group' => 'templates',
                'scope' => self::SCOPE_ADMIN,
            ],

            // Logs Permissions
            [
                'permission_key' => 'logs.call.view',
                'name' => 'View Call Logs',
                'description' => 'Can view call logs',
                'group' => 'logs',
                'scope' => self::SCOPE_ADMIN,
            ],
            [
                'permission_key' => 'logs.call.view_full',
                'name' => 'View Full Call Logs',
                'description' => 'Can view full phone numbers in call logs',
                'group' => 'logs',
                'scope' => self::SCOPE_ADMIN,
            ],
            [
                'permission_key' => 'logs.sms.view',
                'name' => 'View SMS Logs',
                'description' => 'Can view SMS logs',
                'group' => 'logs',
                'scope' => self::SCOPE_ADMIN,
            ],
            [
                'permission_key' => 'logs.sms.view_full',
                'name' => 'View Full SMS Logs',
                'description' => 'Can view full phone numbers and content in SMS logs',
                'group' => 'logs',
                'scope' => self::SCOPE_ADMIN,
            ],
            [
                'permission_key' => 'logs.promotion.view',
                'name' => 'View Promotion Logs',
                'description' => 'Can view promotion logs',
                'group' => 'logs',
                'scope' => self::SCOPE_ADMIN,
            ],
            [
                'permission_key' => 'logs.promotion.view_full',
                'name' => 'View Full Promotion Logs',
                'description' => 'Can view full details in promotion logs',
                'group' => 'logs',
                'scope' => self::SCOPE_ADMIN,
            ],
            [
                'permission_key' => 'logs.session.view',
                'name' => 'View Session Logs',
                'description' => 'Can view session logs',
                'group' => 'logs',
                'scope' => self::SCOPE_ADMIN,
            ],
            [
                'permission_key' => 'logs.export',
                'name' => 'Export Logs',
                'description' => 'Can export logs as CSV/Excel',
                'group' => 'logs',
                'scope' => self::SCOPE_ADMIN,
            ],

            // Analytics Permissions
            [
                'permission_key' => 'analytics.view',
                'name' => 'View Analytics',
                'description' => 'Can view analytics and reports',
                'group' => 'analytics',
                'scope' => self::SCOPE_ADMIN,
            ],
            [
                'permission_key' => 'analytics.view_detailed',
                'name' => 'View Detailed Analytics',
                'description' => 'Can view per-user and per-device analytics',
                'group' => 'analytics',
                'scope' => self::SCOPE_ADMIN,
            ],
            [
                'permission_key' => 'analytics.export',
                'name' => 'Export Analytics',
                'description' => 'Can export analytics reports',
                'group' => 'analytics',
                'scope' => self::SCOPE_ADMIN,
            ],

            // Settings Permissions
            [
                'permission_key' => 'settings.view',
                'name' => 'View Settings',
                'description' => 'Can view system settings',
                'group' => 'settings',
                'scope' => self::SCOPE_ADMIN,
            ],
            [
                'permission_key' => 'settings.edit',
                'name' => 'Edit Settings',
                'description' => 'Can edit system settings',
                'group' => 'settings',
                'scope' => self::SCOPE_ADMIN,
            ],

            // Admin Management Permissions
            [
                'permission_key' => 'admins.view',
                'name' => 'View Admins',
                'description' => 'Can view admin list',
                'group' => 'admins',
                'scope' => self::SCOPE_ADMIN,
            ],
            [
                'permission_key' => 'admins.create',
                'name' => 'Create Admins',
                'description' => 'Can create new admins',
                'group' => 'admins',
                'scope' => self::SCOPE_ADMIN,
            ],
            [
                'permission_key' => 'admins.edit',
                'name' => 'Edit Admins',
                'description' => 'Can edit admin details and permissions',
                'group' => 'admins',
                'scope' => self::SCOPE_ADMIN,
            ],
            [
                'permission_key' => 'admins.delete',
                'name' => 'Delete Admins',
                'description' => 'Can delete admins',
                'group' => 'admins',
                'scope' => self::SCOPE_ADMIN,
            ],

            // Client Management Permissions (previously superadmin-only)
            [
                'permission_key' => 'clients.view',
                'name' => 'View Clients',
                'description' => 'Can view client list and details',
                'group' => 'clients',
                'scope' => self::SCOPE_ADMIN,
            ],
            [
                'permission_key' => 'clients.create',
                'name' => 'Create Clients',
                'description' => 'Can create new clients',
                'group' => 'clients',
                'scope' => self::SCOPE_ADMIN,
            ],
            [
                'permission_key' => 'clients.edit',
                'name' => 'Edit Clients',
                'description' => 'Can edit client details',
                'group' => 'clients',
                'scope' => self::SCOPE_ADMIN,
            ],
            [
                'permission_key' => 'clients.toggle_status',
                'name' => 'Toggle Client Status',
                'description' => 'Can enable/disable client accounts',
                'group' => 'clients',
                'scope' => self::SCOPE_ADMIN,
            ],
            [
                'permission_key' => 'clients.toggle_service',
                'name' => 'Toggle Client Service',
                'description' => 'Can toggle SMS/WhatsApp services for clients',
                'group' => 'clients',
                'scope' => self::SCOPE_ADMIN,
            ],
            [
                'permission_key' => 'clients.subscription',
                'name' => 'Manage Client Subscription',
                'description' => 'Can manage client subscription and limits',
                'group' => 'clients',
                'scope' => self::SCOPE_ADMIN,
            ],

            // Approval Permissions (previously superadmin-only)
            [
                'permission_key' => 'approvals.view',
                'name' => 'View Approvals',
                'description' => 'Can view pending user approvals',
                'group' => 'approvals',
                'scope' => self::SCOPE_ADMIN,
            ],
            [
                'permission_key' => 'approvals.manage',
                'name' => 'Manage Approvals',
                'description' => 'Can approve/reject user registrations',
                'group' => 'approvals',
                'scope' => self::SCOPE_ADMIN,
            ],

            // Template Assignment (previously superadmin-only)
            [
                'permission_key' => 'template_assignment.view',
                'name' => 'View Template Assignments',
                'description' => 'Can view template assignment page',
                'group' => 'template_assignment',
                'scope' => self::SCOPE_ADMIN,
            ],
            [
                'permission_key' => 'template_assignment.manage',
                'name' => 'Manage Template Assignments',
                'description' => 'Can assign/unassign templates to users',
                'group' => 'template_assignment',
                'scope' => self::SCOPE_ADMIN,
            ],

            // Audit Logs (previously superadmin-only)
            [
                'permission_key' => 'audit_logs.view',
                'name' => 'View Audit Logs',
                'description' => 'Can view admin audit logs',
                'group' => 'audit_logs',
                'scope' => self::SCOPE_ADMIN,
            ],

            // Reports Permissions
            [
                'permission_key' => 'reports.view',
                'name' => 'View Reports',
                'description' => 'Can access reports dashboard',
                'group' => 'reports',
                'scope' => self::SCOPE_ADMIN,
            ],
            [
                'permission_key' => 'reports.user_activity',
                'name' => 'User Activity Report',
                'description' => 'Can view user activity reports',
                'group' => 'reports',
                'scope' => self::SCOPE_ADMIN,
            ],
            [
                'permission_key' => 'reports.sms_summary',
                'name' => 'SMS Summary Report',
                'description' => 'Can view SMS summary reports',
                'group' => 'reports',
                'scope' => self::SCOPE_ADMIN,
            ],
            [
                'permission_key' => 'reports.call_summary',
                'name' => 'Call Summary Report',
                'description' => 'Can view call summary reports',
                'group' => 'reports',
                'scope' => self::SCOPE_ADMIN,
            ],
            [
                'permission_key' => 'reports.promotion_summary',
                'name' => 'Promotion Summary Report',
                'description' => 'Can view promotion summary reports',
                'group' => 'reports',
                'scope' => self::SCOPE_ADMIN,
            ],
            [
                'permission_key' => 'reports.device_usage',
                'name' => 'Device Usage Report',
                'description' => 'Can view device usage reports',
                'group' => 'reports',
                'scope' => self::SCOPE_ADMIN,
            ],
            [
                'permission_key' => 'reports.daily_summary',
                'name' => 'Daily Summary Report',
                'description' => 'Can view daily summary reports',
                'group' => 'reports',
                'scope' => self::SCOPE_ADMIN,
            ],
            [
                'permission_key' => 'reports.export',
                'name' => 'Export Reports',
                'description' => 'Can export reports to Excel/CSV',
                'group' => 'reports',
                'scope' => self::SCOPE_ADMIN,
            ],
        ];
    }

    /**
     * Client-scope permissions (for client admin dashboard).
     */
    public static function getClientScopePermissions(): array
    {
        return [
            // Client User Management
            [
                'permission_key' => 'client.users.view',
                'name' => 'View Users',
                'description' => 'Can view user list and details',
                'group' => 'client_users',
                'scope' => self::SCOPE_CLIENT,
            ],
            [
                'permission_key' => 'client.users.create',
                'name' => 'Create Users',
                'description' => 'Can create new users',
                'group' => 'client_users',
                'scope' => self::SCOPE_CLIENT,
            ],
            [
                'permission_key' => 'client.users.edit',
                'name' => 'Edit Users',
                'description' => 'Can edit user details',
                'group' => 'client_users',
                'scope' => self::SCOPE_CLIENT,
            ],
            [
                'permission_key' => 'client.users.toggle_status',
                'name' => 'Toggle User Status',
                'description' => 'Can enable/disable users',
                'group' => 'client_users',
                'scope' => self::SCOPE_CLIENT,
            ],
            [
                'permission_key' => 'client.users.toggle_sms',
                'name' => 'Toggle SMS Service',
                'description' => 'Can enable/disable SMS service per user',
                'group' => 'client_users',
                'scope' => self::SCOPE_CLIENT,
            ],
            [
                'permission_key' => 'client.users.toggle_whatsapp',
                'name' => 'Toggle WhatsApp Service',
                'description' => 'Can enable/disable WhatsApp service per user',
                'group' => 'client_users',
                'scope' => self::SCOPE_CLIENT,
            ],
            [
                'permission_key' => 'client.users.force_logout',
                'name' => 'Force Logout User',
                'description' => 'Can force logout users',
                'group' => 'client_users',
                'scope' => self::SCOPE_CLIENT,
            ],
            [
                'permission_key' => 'client.users.reset_password',
                'name' => 'Reset User Password',
                'description' => 'Can reset user passwords',
                'group' => 'client_users',
                'scope' => self::SCOPE_CLIENT,
            ],

            // Client Template Management
            [
                'permission_key' => 'client.templates.view',
                'name' => 'View Templates',
                'description' => 'Can view message templates',
                'group' => 'client_templates',
                'scope' => self::SCOPE_CLIENT,
            ],
            [
                'permission_key' => 'client.templates.create',
                'name' => 'Create Templates',
                'description' => 'Can create new templates',
                'group' => 'client_templates',
                'scope' => self::SCOPE_CLIENT,
            ],
            [
                'permission_key' => 'client.templates.edit',
                'name' => 'Edit Templates',
                'description' => 'Can edit templates',
                'group' => 'client_templates',
                'scope' => self::SCOPE_CLIENT,
            ],
            [
                'permission_key' => 'client.templates.toggle_status',
                'name' => 'Toggle Template Status',
                'description' => 'Can enable/disable templates',
                'group' => 'client_templates',
                'scope' => self::SCOPE_CLIENT,
            ],

            // Client Reports
            [
                'permission_key' => 'client.reports.sms_logs',
                'name' => 'View SMS Logs',
                'description' => 'Can view SMS logs',
                'group' => 'client_reports',
                'scope' => self::SCOPE_CLIENT,
            ],
            [
                'permission_key' => 'client.reports.call_logs',
                'name' => 'View Call Logs',
                'description' => 'Can view call logs',
                'group' => 'client_reports',
                'scope' => self::SCOPE_CLIENT,
            ],
            [
                'permission_key' => 'client.reports.promotion_logs',
                'name' => 'View Promotion Logs',
                'description' => 'Can view promotion logs',
                'group' => 'client_reports',
                'scope' => self::SCOPE_CLIENT,
            ],

            // Client Admin Management
            [
                'permission_key' => 'client.admins.view',
                'name' => 'View Admin Members',
                'description' => 'Can view client admin members',
                'group' => 'client_admins',
                'scope' => self::SCOPE_CLIENT,
            ],
            [
                'permission_key' => 'client.admins.create',
                'name' => 'Create Admin Members',
                'description' => 'Can create new client admins',
                'group' => 'client_admins',
                'scope' => self::SCOPE_CLIENT,
            ],
            [
                'permission_key' => 'client.admins.edit',
                'name' => 'Edit Admin Members',
                'description' => 'Can edit client admin details and permissions',
                'group' => 'client_admins',
                'scope' => self::SCOPE_CLIENT,
            ],
            [
                'permission_key' => 'client.admins.toggle_status',
                'name' => 'Toggle Admin Status',
                'description' => 'Can enable/disable client admins',
                'group' => 'client_admins',
                'scope' => self::SCOPE_CLIENT,
            ],

            // Client Profile
            [
                'permission_key' => 'client.profile.view',
                'name' => 'View Profile',
                'description' => 'Can view client profile and subscription info',
                'group' => 'client_profile',
                'scope' => self::SCOPE_CLIENT,
            ],
            [
                'permission_key' => 'client.profile.edit',
                'name' => 'Edit Profile',
                'description' => 'Can edit client profile details',
                'group' => 'client_profile',
                'scope' => self::SCOPE_CLIENT,
            ],

            // Client Dashboard
            [
                'permission_key' => 'client.dashboard.view',
                'name' => 'View Dashboard',
                'description' => 'Can view client dashboard',
                'group' => 'client_dashboard',
                'scope' => self::SCOPE_CLIENT,
            ],
        ];
    }

    /**
     * Get all default permissions for SuperAdmin role (all admin-scope permissions).
     */
    public static function getSuperAdminPermissions(): array
    {
        return collect(self::getAdminScopePermissions())
            ->pluck('permission_key')
            ->toArray();
    }

    /**
     * Get default permissions for Admin role (limited permissions).
     */
    public static function getAdminPermissions(): array
    {
        return [
            'users.view',
            'users.create',
            'users.edit',
            'users.toggle_status',
            'templates.view',
            'logs.call.view',
            'logs.sms.view',
            'logs.promotion.view',
            'analytics.view',
            'reports.view',
            'reports.user_activity',
            'reports.sms_summary',
            'reports.call_summary',
        ];
    }

    /**
     * Get default permissions for Client Owner role (all client-scope permissions).
     */
    public static function getClientOwnerPermissions(): array
    {
        return collect(self::getClientScopePermissions())
            ->pluck('permission_key')
            ->toArray();
    }

    /**
     * Get default permissions for Client Manager role (limited client permissions).
     */
    public static function getClientManagerPermissions(): array
    {
        return [
            'client.dashboard.view',
            'client.users.view',
            'client.users.edit',
            'client.users.toggle_status',
            'client.templates.view',
            'client.reports.sms_logs',
            'client.reports.call_logs',
            'client.profile.view',
        ];
    }
}
