<?php

namespace FleekDash\Controllers\Universal;

use WP_REST_Request;
use WP_REST_Response;
use WP_Error;
use FleekDash\Services\UniversalAnalyticsService;

/**
 * Sources Controller
 * 
 * Handles analytics data sources endpoints.
 * Part of Universal Analytics System.
 * 
 * @package FleekDash\Controllers\Universal
 * @since 2.2.0
 */
class SourcesController
{
    private $universal_service;

    public function __construct()
    {
        $this->universal_service = UniversalAnalyticsService::getInstance();
    }

    /**
     * Get all data sources
     * 
     * @param WP_REST_Request $request
     * @return WP_REST_Response|WP_Error
     */
    public function get_data_sources(WP_REST_Request $request)
    {
        try {
            $active_only = $request->get_param('active_only') !== 'false';
            $sources = $this->universal_service->getDataSources($active_only);

            return new WP_REST_Response([
                'success' => true,
                'results' => $sources,
                'meta' => [
                    'total' => count($sources),
                    'active_only' => $active_only
                ]
            ], 200);

        } catch (\Exception $e) {
            return new WP_Error('universal_analytics_error', $e->getMessage(), ['status' => 500]);
        }
    }

    /**
     * Get data source by slug
     * 
     * @param WP_REST_Request $request
     * @return WP_REST_Response|WP_Error
     */
    public function get_data_source(WP_REST_Request $request)
    {
        try {
            $slug = $request->get_param('slug');

            if (!$slug) {
                return new WP_Error('missing_slug', 'Source slug is required', ['status' => 400]);
            }

            $source = $this->universal_service->getDataSourceBySlug($slug);

            if (!$source) {
                return new WP_Error('source_not_found', 'Data source not found', ['status' => 404]);
            }

            return new WP_REST_Response([
                'success' => true,
                'results' => $source
            ], 200);

        } catch (\Exception $e) {
            return new WP_Error('universal_analytics_error', $e->getMessage(), ['status' => 500]);
        }
    }

    /**
     * Get metrics available for a specific source
     * 
     * @param WP_REST_Request $request
     * @return WP_REST_Response|WP_Error
     */
    public function get_source_metrics(WP_REST_Request $request)
    {
        try {
            $source_slug = $request->get_param('source_slug');

            if (!$source_slug) {
                return new WP_Error('missing_source', 'Source slug is required', ['status' => 400]);
            }

            $metrics = $this->universal_service->getSourceMetrics($source_slug);

            return new WP_REST_Response([
                'success' => true,
                'results' => $metrics,
                'meta' => [
                    'source_slug' => $source_slug,
                    'total' => count($metrics)
                ]
            ], 200);

        } catch (\Exception $e) {
            return new WP_Error('universal_analytics_error', $e->getMessage(), ['status' => 500]);
        }
    }
}

