<?php

namespace FleekDash\Controllers\Universal;

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

/**
 * Metrics Controller
 * 
 * Handles analytics metrics endpoints.
 * Part of Universal Analytics System.
 * 
 * @package FleekDash\Controllers\Universal
 * @since 2.2.0
 */
class MetricsController
{
    private $universal_service;

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

    /**
     * Get metrics by category
     * 
     * @param WP_REST_Request $request
     * @return WP_REST_Response|WP_Error
     */
    public function get_metrics_by_category(WP_REST_Request $request)
    {
        try {
            $category_slug = $request->get_param('category_slug');
            $active_only = $request->get_param('active_only') !== 'false';

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

            $metrics = $this->universal_service->getMetricsByCategory($category_slug, $active_only);

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

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

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

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

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

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

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

            $metric = $this->universal_service->getMetricBySlug($slug);

            if (!$metric) {
                return new WP_Error('metric_not_found', 'Metric not found', ['status' => 404]);
            }

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

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

