<?php
/**
 * Data Sources Routes
 *
 * Consolidated API routes for External Data Sources, Data Source Plugins, and Dashboard Templates.
 * Migrated from 3 legacy wrappers to V2.2 RouteGroups.
 *
 * Total Routes: 27
 * - External Data Sources: 9 routes
 * - Data Source Plugins: 7 routes
 * - Dashboard Templates: 11 routes
 *
 * @package FleekDash V2.2
 * @since   V2.2 Refactoring - Phase 3
 */

namespace FleekDash\Routes\Groups;

use FleekDash\Libs\API\Route;
use FleekDash\Libs\API\RouteAuthMiddleware;
use FleekDash\Controllers\ExternalDataSourcesController;
use FleekDash\Controllers\DataSourcePluginsController;
use FleekDash\Controllers\DashboardTemplateController;

class DataSourcesRoutes
{
    /**
     * Register all data sources-related routes
     *
     * @param Route $route Route instance
     * @return void
     */
    public static function register(Route $route)
    {
        // ============================================
        // EXTERNAL DATA SOURCES (9 routes)
        // Migrated from: ExternalDataSourcesAPI.php
        // ============================================

        // Get all external data sources
        Route::get('external-data-sources', [ExternalDataSourcesController::class, 'get_external_data_sources'],
            RouteAuthMiddleware::authenticated());

        // Get external data sources by category
        Route::get('external-data-sources/category/{category}', [ExternalDataSourcesController::class, 'get_external_data_sources_by_category'],
            RouteAuthMiddleware::authenticated(), [
            'category' => ['required' => true, 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field']
        ]);

        // Get specific external data source
        Route::get('external-data-sources/{id}', [ExternalDataSourcesController::class, 'get_external_data_source'],
            RouteAuthMiddleware::authenticated(), [
            'id' => ['required' => true, 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field']
        ]);

        // Fetch data from external data source
        Route::post('external-data-sources/{source_id}/fetch', [ExternalDataSourcesController::class, 'fetch_external_data'],
            RouteAuthMiddleware::authenticated(), [
            'source_id' => ['required' => true, 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field'],
            'metric_id' => ['required' => true, 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field'],
            'filters' => ['required' => false, 'type' => 'object', 'default' => []],
            'time_range' => ['required' => false, 'type' => 'string', 'enum' => ['24h', '7d', '30d', '90d'], 'default' => '30d']
        ]);

        // Test connection to external data source
        Route::post('external-data-sources/{source_id}/test', [ExternalDataSourcesController::class, 'test_external_data_source_connection'],
            RouteAuthMiddleware::authenticated(), [
            'source_id' => ['required' => true, 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field']
        ]);

        // Get metrics for external data source
        Route::get('external-data-sources/{source_id}/metrics', [ExternalDataSourcesController::class, 'get_external_data_source_metrics'],
            RouteAuthMiddleware::authenticated(), [
            'source_id' => ['required' => true, 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field']
        ]);

        // Get config for external data source
        Route::get('external-data-sources/{source_id}/config', [ExternalDataSourcesController::class, 'get_external_data_source_config'],
            RouteAuthMiddleware::authenticated(), [
            'source_id' => ['required' => true, 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field']
        ]);

        // Update config for external data source
        Route::put('external-data-sources/{source_id}/config', [ExternalDataSourcesController::class, 'update_external_data_source_config'],
            RouteAuthMiddleware::admin(), [
            'source_id' => ['required' => true, 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field']
        ]);

        // Refresh external data sources
        Route::post('external-data-sources/refresh', [ExternalDataSourcesController::class, 'refresh_external_data_sources'],
            RouteAuthMiddleware::admin());

        // ============================================
        // DATA SOURCE PLUGINS (7 routes)
        // Migrated from: DataSourcePluginsAPI.php
        // ============================================

        // Get all data source plugins
        Route::get('data-source-plugins', [DataSourcePluginsController::class, 'get_data_source_plugins'],
            RouteAuthMiddleware::authenticated());

        // Get specific data source plugin
        Route::get('data-source-plugins/{plugin_id}', [DataSourcePluginsController::class, 'get_data_source_plugin'],
            RouteAuthMiddleware::authenticated(), [
            'plugin_id' => ['required' => true, 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field']
        ]);

        // Get data source plugin status
        Route::get('data-source-plugins/{plugin_id}/status', [DataSourcePluginsController::class, 'get_data_source_plugin_status'],
            RouteAuthMiddleware::authenticated(), [
            'plugin_id' => ['required' => true, 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field']
        ]);

        // Test data source plugin
        Route::post('data-source-plugins/{plugin_id}/test', [DataSourcePluginsController::class, 'test_data_source_plugin'],
            RouteAuthMiddleware::authenticated(), [
            'plugin_id' => ['required' => true, 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field']
        ]);

        // Get all data source types
        Route::get('data-source-types', [DataSourcePluginsController::class, 'get_data_source_types'],
            RouteAuthMiddleware::authenticated());

        // Get specific data source type
        Route::get('data-source-types/{type}', [DataSourcePluginsController::class, 'get_data_source_type'],
            RouteAuthMiddleware::authenticated(), [
            'type' => ['required' => true, 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field']
        ]);

        // Get data source plugins stats
        Route::get('data-source-plugins/stats', [DataSourcePluginsController::class, 'get_data_source_plugins_stats'],
            RouteAuthMiddleware::authenticated());

        // ============================================
        // DASHBOARD TEMPLATES (11 routes)
        // Migrated from: DashboardTemplatesAPI.php
        // ============================================

        // Get all dashboard templates
        Route::get('dashboard-templates', [DashboardTemplateController::class, 'get_templates'],
            RouteAuthMiddleware::authenticated());

        // Get specific dashboard template
        Route::get('dashboard-templates/{id}', [DashboardTemplateController::class, 'get_template'],
            RouteAuthMiddleware::authenticated(), [
            'id' => ['required' => true, 'type' => 'integer', 'sanitize_callback' => 'absint']
        ]);

        // Apply dashboard template
        Route::post('dashboard-templates/{id}/apply', [DashboardTemplateController::class, 'apply_template'],
            RouteAuthMiddleware::authenticated(), [
            'id' => ['required' => true, 'type' => 'integer', 'sanitize_callback' => 'absint']
        ]);

        // Create dashboard template
        Route::post('dashboard-templates', [DashboardTemplateController::class, 'create_template'],
            RouteAuthMiddleware::admin());

        // Update dashboard template
        Route::put('dashboard-templates/{id}', [DashboardTemplateController::class, 'update_template'],
            RouteAuthMiddleware::admin(), [
            'id' => ['required' => true, 'type' => 'integer', 'sanitize_callback' => 'absint']
        ]);

        // Delete dashboard template
        Route::delete('dashboard-templates/{id}', [DashboardTemplateController::class, 'delete_template'],
            RouteAuthMiddleware::admin(), [
            'id' => ['required' => true, 'type' => 'integer', 'sanitize_callback' => 'absint']
        ]);

        // Initialize default templates
        Route::post('dashboard-templates/initialize-defaults', [DashboardTemplateController::class, 'initialize_defaults'],
            RouteAuthMiddleware::admin());

        // Get template categories
        Route::get('dashboard-templates/categories', [DashboardTemplateController::class, 'get_categories'],
            RouteAuthMiddleware::authenticated());

        // Get user customization
        Route::get('dashboard-templates/user-customization', [DashboardTemplateController::class, 'get_user_customization'],
            RouteAuthMiddleware::authenticated());

        // Save user customization
        Route::post('dashboard-templates/user-customization', [DashboardTemplateController::class, 'save_user_customization'],
            RouteAuthMiddleware::authenticated());

        // Delete user customization
        Route::delete('dashboard-templates/user-customization', [DashboardTemplateController::class, 'delete_user_customization'],
            RouteAuthMiddleware::authenticated());
    }
}

