<?php
/**
 * External Data Sources API Registration
 *
 * Registers REST API routes for external data sources management
 */

namespace FleekDash\API;

defined('ABSPATH') || exit;

use FleekDash\Controllers\ExternalDataSourcesController;

class ExternalDataSourcesAPI
{
    private $controller;

    public function __construct()
    {
        $this->controller = new ExternalDataSourcesController();
        $this->register_routes();
    }

    /**
     * Register REST API routes
     */
    public function register_routes()
    {
        // Get all external data sources
        \register_rest_route('fleekdash/v1', '/external-data-sources', [
            'methods' => 'GET',
            'callback' => [$this->controller, 'get_external_data_sources'],
            'permission_callback' => [$this, 'check_permissions'],
        ]);

        // Get external data sources by category
        \register_rest_route('fleekdash/v1', '/external-data-sources/category/(?P<category>[a-zA-Z0-9-_]+)', [
            'methods' => 'GET',
            'callback' => [$this->controller, 'get_external_data_sources_by_category'],
            'permission_callback' => [$this, 'check_permissions'],
        ]);

        // Get specific external data source
        \register_rest_route('fleekdash/v1', '/external-data-sources/(?P<id>[a-zA-Z0-9-_]+)', [
            'methods' => 'GET',
            'callback' => [$this->controller, 'get_external_data_source'],
            'permission_callback' => [$this, 'check_permissions'],
        ]);

        // Fetch data from external data source
        \register_rest_route('fleekdash/v1', '/external-data-sources/(?P<source_id>[a-zA-Z0-9-_]+)/fetch', [
            'methods' => 'POST',
            'callback' => [$this->controller, 'fetch_external_data'],
            'permission_callback' => [$this, 'check_permissions'],
            'args' => [
                '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
        \register_rest_route('fleekdash/v1', '/external-data-sources/(?P<source_id>[a-zA-Z0-9-_]+)/test', [
            'methods' => 'POST',
            'callback' => [$this->controller, 'test_external_data_source_connection'],
            'permission_callback' => [$this, 'check_permissions'],
        ]);

        // Get external data source metrics
        \register_rest_route('fleekdash/v1', '/external-data-sources/(?P<source_id>[a-zA-Z0-9-_]+)/metrics', [
            'methods' => 'GET',
            'callback' => [$this->controller, 'get_external_data_source_metrics'],
            'permission_callback' => [$this, 'check_permissions'],
        ]);

        // Get external data source configuration
        \register_rest_route('fleekdash/v1', '/external-data-sources/(?P<source_id>[a-zA-Z0-9-_]+)/config', [
            'methods' => 'GET',
            'callback' => [$this->controller, 'get_external_data_source_config'],
            'permission_callback' => [$this, 'check_permissions'],
        ]);

        // Update external data source configuration
        \register_rest_route('fleekdash/v1', '/external-data-sources/(?P<source_id>[a-zA-Z0-9-_]+)/config', [
            'methods' => 'PUT',
            'callback' => [$this->controller, 'update_external_data_source_config'],
            'permission_callback' => [$this, 'check_permissions'],
        ]);

        // Refresh external data sources
        \register_rest_route('fleekdash/v1', '/external-data-sources/refresh', [
            'methods' => 'POST',
            'callback' => [$this->controller, 'refresh_external_data_sources'],
            'permission_callback' => [$this, 'check_permissions'],
        ]);
    }

    /**
     * Check permissions for external data sources endpoints
     *
     * @return bool|\WP_Error
     */
    public function check_permissions()
    {
        // Check if user is logged in
        if (!\is_user_logged_in()) {
            return new \WP_Error(
                'rest_forbidden',
                __('You must be logged in to access this endpoint.', 'fleekdash'),
                ['status' => 401],
            );
        }

        // Check if user has manage_options capability
        if (!\current_user_can('manage_options')) {
            return new \WP_Error(
                'rest_forbidden',
                __('You do not have permission to access this endpoint.', 'fleekdash'),
                ['status' => 403],
            );
        }

        return true;
    }
}
