<?php

namespace FleekDash\Controllers\Universal;

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

/**
 * Dashboards Controller
 *
 * Handles dashboard templates and user dashboards.
 * Part of Universal Analytics System.
 *
 * @package FleekDash\Controllers\Universal
 * @since 2.2.0
 */
class DashboardsController
{
    private $universal_service;

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

    /**
     * Get dashboard templates
     *
     * @param WP_REST_Request $request
     * @return WP_REST_Response|WP_Error
     */
    public function get_dashboard_templates(WP_REST_Request $request)
    {
        try {
            $category_slug = $request->get_param('category_slug');
            $templates = $this->universal_service->getDashboardTemplates($category_slug);

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

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

    /**
     * Get user dashboards
     *
     * @param WP_REST_Request $request
     * @return WP_REST_Response|WP_Error
     */
    public function get_user_dashboards(WP_REST_Request $request)
    {
        try {
            $user_id = \get_current_user_id();

            if (!$user_id) {
                return new WP_Error('not_authenticated', 'Authentication required', ['status' => 401]);
            }

            $dashboards = $this->universal_service->getUserDashboards($user_id);

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

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

    /**
     * Save user dashboard
     *
     * @param WP_REST_Request $request
     * @return WP_REST_Response|WP_Error
     */
    public function save_user_dashboard(WP_REST_Request $request)
    {
        try {
            $user_id = \get_current_user_id();

            if (!$user_id) {
                return new WP_Error('not_authenticated', 'Authentication required', ['status' => 401]);
            }

            $dashboard_data = $request->get_json_params();

            if (!$dashboard_data) {
                return new WP_Error('missing_data', 'Dashboard data is required', ['status' => 400]);
            }

            $result = $this->universal_service->saveUserDashboard($user_id, $dashboard_data);

            if (!$result) {
                return new WP_Error('save_failed', 'Failed to save dashboard', ['status' => 500]);
            }

            return new WP_REST_Response([
                'success' => true,
                'message' => 'Dashboard saved successfully'
            ], 200);

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