<?php
/**
 * Permission Definitions
 *
 * PHP mirror of TypeScript permission definitions.
 * Registers all permissions for server-side validation.
 *
 * @package FleekDash\Core
 */

namespace FleekDash\Core;

class PermissionDefinitions {
    private const GROUP_AI_FEATURES = 'AI Features';

    /**
     * Register all permissions
     *
     * @return void
     */
    public static function register(): void {
        if (PermissionManager::isInitialized()) {
            return;
        }

        // Register all permission groups
        self::registerThemePermissions();
        self::registerPostPermissions();
        self::registerMenuPermissions();
        self::registerUserPermissions();
        self::registerAIPermissions();
        self::registerSettingsPermissions();

        // Mark as initialized
        PermissionManager::markInitialized();
    }

    /**
     * Register theme permissions
     *
     * @return void
     */
    private static function registerThemePermissions(): void {
        // Root: Theme Editor
        PermissionManager::register([
            'id' => 'theme_editor',
            'label' => 'Theme Editor',
            'description' => 'Manage themes, palettes, and appearance settings',
            'icon' => 'paint-brush',
            'group' => 'Appearance',
            'required_feature' => 'theme_live_editor',
            'required_capability' => 'edit_theme_options',
        ]);

        // Child permissions
        PermissionManager::registerMany([
            [
                'id' => 'theme_editor.create_palette',
                'parent' => 'theme_editor',
                'label' => 'Create Palette',
                'description' => 'Create new color palettes',
                'icon' => 'palette',
                'group' => 'Appearance',
                'required_capability' => 'edit_theme_options',
            ],
            [
                'id' => 'theme_editor.edit_palette',
                'parent' => 'theme_editor',
                'label' => 'Edit Palette',
                'description' => 'Edit existing color palettes',
                'icon' => 'edit',
                'group' => 'Appearance',
                'required_capability' => 'edit_theme_options',
            ],
            [
                'id' => 'theme_editor.delete_palette',
                'parent' => 'theme_editor',
                'label' => 'Delete Palette',
                'description' => 'Delete color palettes',
                'icon' => 'trash',
                'group' => 'Appearance',
                'required_capability' => 'edit_theme_options',
            ],
            [
                'id' => 'theme_editor.export_settings',
                'parent' => 'theme_editor',
                'label' => 'Export Settings',
                'description' => 'Export theme settings and palettes',
                'icon' => 'download',
                'group' => 'Appearance',
                'required_capability' => 'export',
            ],
            [
                'id' => 'theme_editor.import_settings',
                'parent' => 'theme_editor',
                'label' => 'Import Settings',
                'description' => 'Import theme settings and palettes',
                'icon' => 'upload',
                'group' => 'Appearance',
                'required_capability' => 'import',
            ],
            [
                'id' => 'theme_editor.delete_theme',
                'parent' => 'theme_editor',
                'label' => 'Delete Theme',
                'description' => 'Delete entire themes (destructive)',
                'icon' => 'trash',
                'group' => 'Appearance',
                'required_capability' => 'delete_themes',
                'admin_only' => true,
            ],
            [
                'id' => 'theme_editor.apply_theme',
                'parent' => 'theme_editor',
                'label' => 'Apply Theme',
                'description' => 'Apply and activate themes',
                'icon' => 'check',
                'group' => 'Appearance',
                'required_capability' => 'edit_theme_options',
            ],
            [
                'id' => 'theme_editor.customize_colors',
                'parent' => 'theme_editor',
                'label' => 'Customize Colors',
                'description' => 'Customize theme colors and styles',
                'icon' => 'color-palette',
                'group' => 'Appearance',
                'required_capability' => 'edit_theme_options',
            ],
        ]);

        // Appearance Settings
        PermissionManager::register([
            'id' => 'appearance_settings',
            'label' => 'Appearance Settings',
            'description' => 'Access appearance and theme settings page',
            'icon' => 'settings',
            'group' => 'Appearance',
            'required_capability' => 'edit_theme_options',
        ]);
    }

    /**
     * Register post permissions
     *
     * @return void
     */
    private static function registerPostPermissions(): void {
        // Root: Posts
        PermissionManager::register([
            'id' => 'posts',
            'label' => 'Posts',
            'description' => 'Manage blog posts and content',
            'icon' => 'file-text',
            'group' => 'Content',
            'required_capability' => 'edit_posts',
        ]);

        // Child permissions
        PermissionManager::registerMany([
            [
                'id' => 'posts.view',
                'parent' => 'posts',
                'label' => 'View Posts',
                'description' => 'View posts list and details',
                'icon' => 'eye',
                'group' => 'Content',
                'required_capability' => 'edit_posts',
            ],
            [
                'id' => 'posts.create',
                'parent' => 'posts',
                'label' => 'Create Posts',
                'description' => 'Create new posts',
                'icon' => 'plus',
                'group' => 'Content',
                'required_capability' => 'edit_posts',
            ],
            [
                'id' => 'posts.edit',
                'parent' => 'posts',
                'label' => 'Edit Posts',
                'description' => 'Edit own posts',
                'icon' => 'edit',
                'group' => 'Content',
                'required_capability' => 'edit_posts',
            ],
            [
                'id' => 'posts.edit_others',
                'parent' => 'posts',
                'label' => 'Edit Others Posts',
                'description' => 'Edit posts created by other users',
                'icon' => 'edit',
                'group' => 'Content',
                'required_capability' => 'edit_others_posts',
            ],
            [
                'id' => 'posts.delete',
                'parent' => 'posts',
                'label' => 'Delete Posts',
                'description' => 'Delete own posts',
                'icon' => 'trash',
                'group' => 'Content',
                'required_capability' => 'delete_posts',
            ],
            [
                'id' => 'posts.delete_others',
                'parent' => 'posts',
                'label' => 'Delete Others Posts',
                'description' => 'Delete posts created by other users',
                'icon' => 'trash',
                'group' => 'Content',
                'required_capability' => 'delete_others_posts',
            ],
            [
                'id' => 'posts.publish',
                'parent' => 'posts',
                'label' => 'Publish Posts',
                'description' => 'Publish posts (make them public)',
                'icon' => 'check',
                'group' => 'Content',
                'required_capability' => 'publish_posts',
            ],
            [
                'id' => 'posts.read_private',
                'parent' => 'posts',
                'label' => 'Read Private Posts',
                'description' => 'View private posts',
                'icon' => 'eye',
                'group' => 'Content',
                'required_capability' => 'read_private_posts',
            ],
            [
                'id' => 'posts.edit_published',
                'parent' => 'posts',
                'label' => 'Edit Published Posts',
                'description' => 'Edit posts that are already published',
                'icon' => 'edit',
                'group' => 'Content',
                'required_capability' => 'edit_published_posts',
            ],
            [
                'id' => 'posts.delete_published',
                'parent' => 'posts',
                'label' => 'Delete Published Posts',
                'description' => 'Delete posts that are already published',
                'icon' => 'trash',
                'group' => 'Content',
                'required_capability' => 'delete_published_posts',
            ],
        ]);
    }

    /**
     * Register menu permissions
     *
     * @return void
     */
    private static function registerMenuPermissions(): void {
        // Root: Menu Management
        PermissionManager::register([
            'id' => 'menu_management',
            'label' => 'Menu Management',
            'description' => 'Manage sidebar menu items and customization',
            'icon' => 'menu',
            'group' => 'Appearance',
            'required_feature' => 'menu_management',
            'required_capability' => 'edit_theme_options',
        ]);

        // Child permissions
        PermissionManager::registerMany([
            [
                'id' => 'menu_management.view',
                'parent' => 'menu_management',
                'label' => 'View Menu Items',
                'description' => 'View menu items and configuration',
                'icon' => 'eye',
                'group' => 'Appearance',
                'required_capability' => 'edit_theme_options',
            ],
            [
                'id' => 'menu_management.add',
                'parent' => 'menu_management',
                'label' => 'Add Menu Items',
                'description' => 'Add new items to menus',
                'icon' => 'plus',
                'group' => 'Appearance',
                'required_capability' => 'edit_theme_options',
            ],
            [
                'id' => 'menu_management.edit',
                'parent' => 'menu_management',
                'label' => 'Edit Menu Items',
                'description' => 'Edit existing menu items',
                'icon' => 'edit',
                'group' => 'Appearance',
                'required_capability' => 'edit_theme_options',
            ],
            [
                'id' => 'menu_management.delete',
                'parent' => 'menu_management',
                'label' => 'Delete Menu Items',
                'description' => 'Remove items from menus',
                'icon' => 'trash',
                'group' => 'Appearance',
                'required_capability' => 'edit_theme_options',
            ],
            [
                'id' => 'menu_management.reorder',
                'parent' => 'menu_management',
                'label' => 'Reorder Menu Items',
                'description' => 'Change menu item order',
                'icon' => 'move',
                'group' => 'Appearance',
                'required_capability' => 'edit_theme_options',
            ],
            [
                'id' => 'menu_management.visibility',
                'parent' => 'menu_management',
                'label' => 'Customize Visibility',
                'description' => 'Control menu item visibility per role',
                'icon' => 'eye-off',
                'group' => 'Appearance',
                'required_capability' => 'edit_theme_options',
            ],
            [
                'id' => 'menu_management.pin',
                'parent' => 'menu_management',
                'label' => 'Pin Menu Items',
                'description' => 'Pin/unpin menu items',
                'icon' => 'pin',
                'group' => 'Appearance',
                'required_capability' => 'edit_theme_options',
            ],
        ]);
    }

    /**
     * Register user and role permissions
     *
     * @return void
     */
    private static function registerUserPermissions(): void {
        // User Management
        PermissionManager::register([
            'id' => 'user_management',
            'label' => 'User Management',
            'description' => 'Manage users and their accounts',
            'icon' => 'users',
            'group' => 'Users',
            'required_capability' => 'edit_users',
        ]);

        PermissionManager::registerMany([
            [
                'id' => 'user_management.view',
                'parent' => 'user_management',
                'label' => 'View Users',
                'description' => 'View users list and profiles',
                'icon' => 'eye',
                'group' => 'Users',
                'required_capability' => 'list_users',
            ],
            [
                'id' => 'user_management.create',
                'parent' => 'user_management',
                'label' => 'Create Users',
                'description' => 'Create new user accounts',
                'icon' => 'user-plus',
                'group' => 'Users',
                'required_capability' => 'create_users',
            ],
            [
                'id' => 'user_management.edit',
                'parent' => 'user_management',
                'label' => 'Edit Users',
                'description' => 'Edit user profiles and settings',
                'icon' => 'edit',
                'group' => 'Users',
                'required_capability' => 'edit_users',
            ],
            [
                'id' => 'user_management.delete',
                'parent' => 'user_management',
                'label' => 'Delete Users',
                'description' => 'Delete user accounts',
                'icon' => 'trash',
                'group' => 'Users',
                'required_capability' => 'delete_users',
            ],
            [
                'id' => 'user_management.promote',
                'parent' => 'user_management',
                'label' => 'Promote Users',
                'description' => 'Change user roles',
                'icon' => 'arrow-up',
                'group' => 'Users',
                'required_capability' => 'promote_users',
            ],
        ]);

        // Role Management
        PermissionManager::register([
            'id' => 'role_management',
            'label' => 'Role Management',
            'description' => 'Manage roles and permissions',
            'icon' => 'shield',
            'group' => 'Users',
            'required_capability' => 'manage_options',
        ]);

        PermissionManager::registerMany([
            [
                'id' => 'role_management.view',
                'parent' => 'role_management',
                'label' => 'View Roles',
                'description' => 'View roles and their capabilities',
                'icon' => 'eye',
                'group' => 'Users',
                'required_capability' => 'manage_options',
            ],
            [
                'id' => 'role_management.create',
                'parent' => 'role_management',
                'label' => 'Create Roles',
                'description' => 'Create new custom roles',
                'icon' => 'plus',
                'group' => 'Users',
                'required_capability' => 'manage_options',
            ],
            [
                'id' => 'role_management.edit',
                'parent' => 'role_management',
                'label' => 'Edit Roles',
                'description' => 'Edit role capabilities',
                'icon' => 'edit',
                'group' => 'Users',
                'required_capability' => 'manage_options',
            ],
            [
                'id' => 'role_management.delete',
                'parent' => 'role_management',
                'label' => 'Delete Roles',
                'description' => 'Delete custom roles',
                'icon' => 'trash',
                'group' => 'Users',
                'required_capability' => 'manage_options',
            ],
            [
                'id' => 'role_management.assign_capabilities',
                'parent' => 'role_management',
                'label' => 'Assign Capabilities',
                'description' => 'Add/remove capabilities from roles',
                'icon' => 'check-square',
                'group' => 'Users',
                'required_capability' => 'manage_options',
            ],
        ]);
    }

    /**
     * Register AI permissions
     *
     * @return void
     */
    private static function registerAIPermissions(): void {
        // Root: AI Assistant
        PermissionManager::register([
            'id' => 'ai_assistant',
            'label' => 'AI Assistant',
            'description' => 'Access AI-powered features and assistance',
            'icon' => 'sparkles',
            'group' => self::GROUP_AI_FEATURES,
            'required_feature' => 'ai_assistant',
            'required_capability' => 'edit_posts',
        ]);

        // Child permissions
        PermissionManager::registerMany([
            [
                'id' => 'ai_assistant.chat',
                'parent' => 'ai_assistant',
                'label' => 'Chat with AI',
                'description' => 'Use AI chat interface',
                'icon' => 'message-circle',
                'group' => self::GROUP_AI_FEATURES,
                'required_capability' => 'edit_posts',
            ],
            [
                'id' => 'ai_assistant.generate_content',
                'parent' => 'ai_assistant',
                'label' => 'Generate Content',
                'description' => 'Generate content using AI',
                'icon' => 'file-text',
                'group' => self::GROUP_AI_FEATURES,
                'required_capability' => 'edit_posts',
            ],
            [
                'id' => 'ai_assistant.generate_images',
                'parent' => 'ai_assistant',
                'label' => 'Generate Images',
                'description' => 'Generate images using AI',
                'icon' => 'image',
                'group' => self::GROUP_AI_FEATURES,
                'required_feature' => 'ai_image_generation',
                'required_capability' => 'upload_files',
            ],
            [
                'id' => 'ai_assistant.code_help',
                'parent' => 'ai_assistant',
                'label' => 'Code Assistant',
                'description' => 'Get AI help with code',
                'icon' => 'code',
                'group' => self::GROUP_AI_FEATURES,
                'required_capability' => 'edit_posts',
            ],
            [
                'id' => 'ai_assistant.view_history',
                'parent' => 'ai_assistant',
                'label' => 'View History',
                'description' => 'View AI chat history',
                'icon' => 'clock',
                'group' => self::GROUP_AI_FEATURES,
                'required_capability' => 'read',
            ],
            [
                'id' => 'ai_assistant.delete_history',
                'parent' => 'ai_assistant',
                'label' => 'Delete History',
                'description' => 'Delete AI chat history',
                'icon' => 'trash',
                'group' => self::GROUP_AI_FEATURES,
                'required_capability' => 'delete_posts',
            ],
        ]);
    }

    /**
     * Register settings permissions
     *
     * @return void
     */
    private static function registerSettingsPermissions(): void {
        // Root: Settings
        PermissionManager::register([
            'id' => 'settings',
            'label' => 'Settings',
            'description' => 'Access plugin settings',
            'icon' => 'settings',
            'group' => 'Settings',
            'required_capability' => 'manage_options',
        ]);

        PermissionManager::registerMany([
            [
                'id' => 'settings.general',
                'parent' => 'settings',
                'label' => 'General Settings',
                'description' => 'Configure general plugin settings',
                'icon' => 'settings',
                'group' => 'Settings',
                'required_capability' => 'manage_options',
            ],
            [
                'id' => 'settings.license',
                'parent' => 'settings',
                'label' => 'License Settings',
                'description' => 'Manage license activation and subscription',
                'icon' => 'key',
                'group' => 'Settings',
                'required_capability' => 'manage_options',
            ],
            [
                'id' => 'settings.labs',
                'parent' => 'settings',
                'label' => 'Labs Settings',
                'description' => 'Access experimental features',
                'icon' => 'flask',
                'group' => 'Settings',
                'required_capability' => 'manage_options',
            ],
            [
                'id' => 'settings.import_export',
                'parent' => 'settings',
                'label' => 'Import/Export',
                'description' => 'Import and export plugin settings',
                'icon' => 'download',
                'group' => 'Settings',
                'required_capability' => 'manage_options',
            ],
            [
                'id' => 'settings.reset',
                'parent' => 'settings',
                'label' => 'Reset Settings',
                'description' => 'Reset all settings to defaults',
                'icon' => 'rotate-ccw',
                'group' => 'Settings',
                'required_capability' => 'manage_options',
                'admin_only' => true,
            ],
        ]);

        // Analytics
        PermissionManager::register([
            'id' => 'analytics',
            'label' => 'Analytics',
            'description' => 'View analytics and reports',
            'icon' => 'bar-chart',
            'group' => 'Analytics',
            'required_feature' => 'integration_analytics',
            'required_capability' => 'manage_options',
        ]);

        PermissionManager::registerMany([
            [
                'id' => 'analytics.view',
                'parent' => 'analytics',
                'label' => 'View Analytics',
                'description' => 'View analytics dashboards',
                'icon' => 'eye',
                'group' => 'Analytics',
                'required_capability' => 'manage_options',
            ],
            [
                'id' => 'analytics.export',
                'parent' => 'analytics',
                'label' => 'Export Analytics',
                'description' => 'Export analytics data',
                'icon' => 'download',
                'group' => 'Analytics',
                'required_capability' => 'export',
            ],
        ]);
    }
}
