<?php

namespace FleekDash\Controllers\Analytics;

use WP_REST_Request;

/**
 * Analytics Helpers
 *
 * Utility methods for analytics operations
 *
 * @package FleekDash\Controllers\Analytics
 */
class AnalyticsHelpers
{
    /**
     * Get common time range arguments for endpoints
     *
     * @return array Time range arguments configuration
     */
    public static function getTimeRangeArgs()
    {
        return [
            'range' => [
                'default' => '30d',
                'sanitize_callback' => 'sanitize_text_field',
                'validate_callback' => function ($param) {
                    return in_array($param, ['24h', '7d', '30d', '90d', '1y']);
                }
            ],
            'start_date' => [
                'sanitize_callback' => 'sanitize_text_field',
                'validate_callback' => function ($param) {
                    return empty($param) || strtotime($param) !== false;
                }
            ],
            'end_date' => [
                'sanitize_callback' => 'sanitize_text_field',
                'validate_callback' => function ($param) {
                    return empty($param) || strtotime($param) !== false;
                }
            ],
            'aggregation' => [
                'default' => 'daily',
                'sanitize_callback' => 'sanitize_text_field',
                'validate_callback' => function ($param) {
                    return in_array($param, ['hourly', 'daily', 'weekly', 'monthly']);
                }
            ]
        ];
    }

    /**
     * Get date range from request parameters
     *
     * @param WP_REST_Request $request Request object
     * @return array Date range with start and end DateTime objects
     */
    public static function getDateRange(WP_REST_Request $request)
    {
        $range = $request->get_param('range');
        $start_date = $request->get_param('start_date');
        $end_date = $request->get_param('end_date');

        if ($start_date && $end_date) {
            return [
                'start' => new \DateTime($start_date),
                'end' => new \DateTime($end_date)
            ];
        }

        // Use current date as end of range; start is derived from range parameter
        $end = new \DateTime();
        $start = clone $end;

        switch ($range) {
            case '24h':
                $start->modify('-1 day');
                break;
            case '7d':
                $start->modify('-7 days');
                break;
            case '30d':
                $start->modify('-30 days');
                break;
            case '90d':
                $start->modify('-90 days');
                break;
            case '1y':
                $start->modify('-1 year');
                break;
            default:
                $start->modify('-30 days');
        }

        return ['start' => $start, 'end' => $end];
    }

    /**
     * Calculate percentage change between two values
     *
     * @param int|float $current Current value
     * @param int|float $previous Previous value
     * @return float Percentage change
     */
    public static function calculatePercentageChange($current, $previous)
    {
        if ($previous == 0) {
            return $current > 0 ? 100 : 0;
        }
        return (($current - $previous) / $previous) * 100;
    }

    /**
     * Check permissions - always return true
     * Let AuthMiddleware handle authentication
     *
     * @return bool
     */
    public static function checkPermissions()
    {
        return true;
    }
}
