<?php
/**
 * Plugins Routes
 *
 * Consolidated API routes for Plugins, Themes, Updates, and Rollback management.
 * Migrated from 6 legacy wrappers to V2.2 RouteGroups.
 *
 * Total Routes: 36
 * - Plugins Management: 8 routes (PluginsAPI + PluginDetectionAPI)
 * - Plugin Directory: 9 routes
 * - Theme Directory: 7 routes
 * - Updates Management: 6 routes
 * - Rollback Management: 6 routes
 *
 * @package FleekDash V2.2
 * @since   V2.2 Refactoring - Phase 2
 */

namespace FleekDash\Routes\Groups;

use FleekDash\Libs\API\Route;
use FleekDash\Libs\API\RouteAuthMiddleware;
use FleekDash\Controllers\PackageUploadController;
use FleekDash\Controllers\PluginsController;
use FleekDash\Controllers\PluginDirectoryController;
use FleekDash\Controllers\ThemeDirectoryController;
use FleekDash\Controllers\UpdatesController;
use FleekDash\Controllers\RollbackController;

class PluginsRoutes
{
    /**
     * Register all plugins-related routes
     *
     * @param Route $route Route instance
     * @return void
     */
    public static function register(Route $route)
    {
        // ============================================
        // PLUGINS MANAGEMENT (8 routes)
        // Migrated from: PluginsAPI.php + PluginDetectionAPI.php
        // ============================================

        // Get all plugins
        $route->get('plugins', [PluginsController::class, 'get_plugins'],
            RouteAuthMiddleware::authenticated());

        // Activate plugin
        $route->post('plugins/activate', [PluginsController::class, 'activate_plugin'],
            RouteAuthMiddleware::admin(), [
            'file' => ['required' => true, 'type' => 'string']
        ]);

        // Deactivate plugin
        $route->post('plugins/deactivate', [PluginsController::class, 'deactivate_plugin'],
            RouteAuthMiddleware::admin(), [
            'file' => ['required' => true, 'type' => 'string']
        ]);

        // Delete plugin
        $route->delete('plugins/delete', [PluginsController::class, 'delete_plugin'],
            RouteAuthMiddleware::admin(), [
            'file' => ['required' => true, 'type' => 'string']
        ]);

        // Update a single plugin (frontend sends one request per plugin for reliable bulk updates)
        $route->post('plugins/update', [PluginsController::class, 'update_plugin'],
            RouteAuthMiddleware::admin(), [
            'file' => ['required' => false, 'type' => 'string'],
            'plugins' => ['required' => false]
        ]);

        // Get plugins statistics
        $route->get('plugins/stats', [PluginsController::class, 'get_plugins_stats'],
            RouteAuthMiddleware::authenticated());

        // Get plugin changelog
        $route->get('plugins/changelog', [PluginsController::class, 'get_changelog'],
            RouteAuthMiddleware::authenticated(), [
            'file' => ['required' => true, 'type' => 'string']
        ]);

        // Get lightweight plugin file signature (mtime/size/version) for upload verification.
        // Accepts either the plugin main file path or just the folder slug.
        $route->get('plugins/file-signature', [PluginsController::class, 'get_plugin_signature'],
            RouteAuthMiddleware::authenticated(), [
            'file' => ['required' => true, 'type' => 'string']
        ]);

        // ZIP upload from plugin-install.php (clean JSON — no wp-admin HTML noise)
        $route->post('plugins/upload', [PackageUploadController::class, 'uploadPlugin'],
            RouteAuthMiddleware::admin());

        // Plugin detection - Get role patterns
        $route->get('plugin-roles', [PluginsController::class, 'get_plugin_role_patterns'],
            RouteAuthMiddleware::admin());

        // Plugin detection - Get active plugins with roles
        $route->get('active-plugins', [PluginsController::class, 'get_active_plugins'],
            RouteAuthMiddleware::admin());

        // ============================================
        // PLUGIN DIRECTORY (9 routes)
        // ============================================

        // Search plugins
        $route->get('plugin-directory/search', [PluginDirectoryController::class, 'search_plugins'],
            RouteAuthMiddleware::authenticated());

        // Get plugin details
        $route->get('plugin-directory/plugin/{slug}', [PluginDirectoryController::class, 'get_plugin_details'],
            RouteAuthMiddleware::authenticated(), [
            'slug' => ['required' => true, 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field']
        ]);

        // Get categories
        $route->get('plugin-directory/categories', [PluginDirectoryController::class, 'get_categories'],
            RouteAuthMiddleware::authenticated());

        // Get tags
        $route->get('plugin-directory/tags', [PluginDirectoryController::class, 'get_tags'],
            RouteAuthMiddleware::authenticated());

        // Get featured plugins
        $route->get('plugin-directory/featured', [PluginDirectoryController::class, 'get_featured'],
            RouteAuthMiddleware::authenticated());

        // Install plugin
        $route->post('plugin-directory/install', [PluginDirectoryController::class, 'install_plugin'],
            RouteAuthMiddleware::admin(), [
            'slug' => ['required' => true, 'type' => 'string'],
            'activate' => ['required' => false, 'type' => 'boolean', 'default' => false]
        ]);

        // Get install status
        $route->get('plugin-directory/install-status/{job_id}', [PluginDirectoryController::class, 'get_install_status'],
            RouteAuthMiddleware::admin(), [
            'job_id' => ['required' => true, 'type' => 'string']
        ]);

        // Cancel install
        $route->delete('plugin-directory/cancel-install/{job_id}', [PluginDirectoryController::class, 'cancel_install'],
            RouteAuthMiddleware::admin(), [
            'job_id' => ['required' => true, 'type' => 'string']
        ]);

        // Bulk action
        $route->post('plugin-directory/bulk', [PluginDirectoryController::class, 'bulk_action'],
            RouteAuthMiddleware::admin());

        // ============================================
        // THEME DIRECTORY (7 routes)
        // ============================================

        // Search themes
        $route->get('theme-directory/search', [ThemeDirectoryController::class, 'search_themes'],
            RouteAuthMiddleware::authenticated());

        // Get theme details
        $route->get('theme-directory/theme/{slug}', [ThemeDirectoryController::class, 'get_theme_details'],
            RouteAuthMiddleware::authenticated(), [
            'slug' => ['required' => true, 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field']
        ]);

        // Get tags
        $route->get('theme-directory/tags', [ThemeDirectoryController::class, 'get_tags'],
            RouteAuthMiddleware::authenticated());

        // Get featured themes
        $route->get('theme-directory/featured', [ThemeDirectoryController::class, 'get_featured'],
            RouteAuthMiddleware::authenticated());

        // Install theme
        $route->post('theme-directory/install', [ThemeDirectoryController::class, 'install_theme'],
            RouteAuthMiddleware::admin(), [
            'slug' => ['required' => true, 'type' => 'string'],
            'activate' => ['required' => false, 'type' => 'boolean', 'default' => false]
        ]);

        // Get install status
        $route->get('theme-directory/install-status/{job_id}', [ThemeDirectoryController::class, 'get_install_status'],
            RouteAuthMiddleware::admin(), [
            'job_id' => ['required' => true, 'type' => 'string']
        ]);

        // Cancel install
        $route->delete('theme-directory/cancel-install/{job_id}', [ThemeDirectoryController::class, 'cancel_install'],
            RouteAuthMiddleware::admin(), [
            'job_id' => ['required' => true, 'type' => 'string']
        ]);

        // ============================================
        // UPDATES MANAGEMENT (6 routes)
        // ============================================

        // Check for updates
        $route->get('updates/check', [UpdatesController::class, 'checkUpdates'],
            RouteAuthMiddleware::authenticated());

        // Force check for updates
        $route->post('updates/check/force', [UpdatesController::class, 'forceCheckUpdates'],
            RouteAuthMiddleware::admin());

        // Start update
        $route->post('updates/start', [UpdatesController::class, 'startUpdate'],
            RouteAuthMiddleware::admin(), [
            'type' => ['required' => true, 'type' => 'string', 'enum' => ['core', 'plugin', 'theme', 'translation']],
            'item' => ['required' => false, 'type' => 'string']
        ]);

        // Get update status
        $route->get('updates/status/{job_id}', [UpdatesController::class, 'getUpdateStatus'],
            RouteAuthMiddleware::admin(), [
            'job_id' => ['required' => true, 'type' => 'string']
        ]);

        // Cancel update
        $route->delete('updates/cancel/{job_id}', [UpdatesController::class, 'cancelUpdate'],
            RouteAuthMiddleware::admin(), [
            'job_id' => ['required' => true, 'type' => 'string']
        ]);

        // Bulk updates
        $route->post('updates/bulk', [UpdatesController::class, 'bulkUpdate'],
            RouteAuthMiddleware::admin(), [
            'items' => ['required' => true, 'type' => 'array']
        ]);

        // ============================================
        // ROLLBACK MANAGEMENT (6 routes)
        // ============================================

        // Get available versions
        $route->get('rollback/versions/{type}/{slug}', [RollbackController::class, 'getVersions'],
            RouteAuthMiddleware::authenticated(), [
            'type' => ['required' => true, 'type' => 'string', 'enum' => ['plugin', 'theme']],
            'slug' => ['required' => true, 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field']
        ]);

        // Start rollback
        $route->post('rollback/start', [RollbackController::class, 'startRollback'],
            RouteAuthMiddleware::admin(), [
            'type' => ['required' => true, 'type' => 'string', 'enum' => ['plugin', 'theme']],
            'item' => ['required' => true, 'type' => 'string'],
            'version' => ['required' => true, 'type' => 'string'],
            'backup' => ['required' => false, 'type' => 'boolean', 'default' => true],
            'dry_run' => ['required' => false, 'type' => 'boolean', 'default' => false]
        ]);

        // Get rollback status
        $route->get('rollback/status/{job_id}', [RollbackController::class, 'getRollbackStatus'],
            RouteAuthMiddleware::admin(), [
            'job_id' => ['required' => true, 'type' => 'string']
        ]);

        // Cancel rollback
        $route->delete('rollback/cancel/{job_id}', [RollbackController::class, 'cancelRollback'],
            RouteAuthMiddleware::admin(), [
            'job_id' => ['required' => true, 'type' => 'string']
        ]);

        // List backups
        $route->get('rollback/backups/{type}/{slug}', [RollbackController::class, 'listBackups'],
            RouteAuthMiddleware::authenticated(), [
            'type' => ['required' => true, 'type' => 'string', 'enum' => ['plugin', 'theme']],
            'slug' => ['required' => true, 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field']
        ]);

        // Restore from backup
        $route->post('rollback/restore-backup', [RollbackController::class, 'restoreBackup'],
            RouteAuthMiddleware::admin(), [
            'backup_file' => ['required' => true, 'type' => 'string']
        ]);
    }
}
