<?php

namespace FleekDash\Controllers\Analytics\Traits;

/**
 * Date Range Helpers Trait
 *
 * Provides date range and comparison functionality for Analytics controllers.
 * Extracted from BaseController for V2.2 compatibility.
 *
 * @package FleekDash\Controllers\Analytics\Traits
 * @since 2.2.0
 */
trait HasDateRangeHelpers
{
    /**
     * Get date range information from request
     *
     * @param \WP_REST_Request $request
     * @return array
     */
    protected function get_date_range_info($request)
    {
        // Accept both 'from'/'to' (frontend) and 'start_date'/'end_date' (legacy)
        $start_date = $request->get_param('start_date') ?: $request->get_param('from');
        $end_date = $request->get_param('end_date') ?: $request->get_param('to');
        $time_range = $request->get_param('time_range');
        $days_param = $request->get_param('days'); // Support direct 'days' parameter

        // If specific dates are provided, use them
        if ($start_date && $end_date) {
            $start = new \DateTime($start_date);
            $end = new \DateTime($end_date);
            $interval = $start->diff($end);
            $days = $interval->days + 1; // +1 to include both start and end dates

            return [
                'days' => $days,
                'start_date' => $start_date,
                'end_date' => $end_date,
                'time_range' => 'custom'
            ];
        }

        // Check for direct 'days' parameter first, then time_range
        $days = $days_param ? (int) $days_param : ($time_range ? $this->get_days_from_range($time_range) : 30);

        return [
            'days' => $days,
            'start_date' => null,
            'end_date' => null,
            'time_range' => $time_range ?: null
        ];
    }

    /**
     * Get days from time range
     *
     * @param string $time_range
     * @return int
     */
    protected function get_days_from_range($time_range)
    {
        switch ($time_range) {
            case '7d':
                return 7;
            case '14d':
                return 14;
            case '30d':
                return 30;
            case '90d':
                return 90;
            case '180d':
                return 180;
            case '1y':
            case '365d':
                return 365;
            default:
                return 30;
        }
    }

    /**
     * Get comparison dates based on comparison type
     *
     * @param array $range_info
     * @param string $comparison_type
     * @return array
     */
    protected function get_comparison_dates($range_info, $comparison_type)
    {
        $days = $range_info['days'];
        $start_date = $range_info['start_date'];
        $end_date = $range_info['end_date'];

        if ($start_date && $end_date) {
            // Use specific dates
            $current_start = new \DateTime($start_date);
            $current_end = new \DateTime($end_date);
        } else {
            // Use relative dates
            $current_end = new \DateTime();
            $current_start = (new \DateTime())->modify("-{$days} days");
        }

        switch ($comparison_type) {
            case 'previous_period':
                $interval = $current_start->diff($current_end);
                $comparison_end = clone $current_start;
                $comparison_start = clone $comparison_end;
                $comparison_start->modify("-{$interval->days} days");
                break;

            case 'previous_year':
                $comparison_start = clone $current_start;
                $comparison_start->modify('-1 year');
                $comparison_end = clone $current_end;
                $comparison_end->modify('-1 year');
                break;

            default:
                // Default to previous period
                $interval = $current_start->diff($current_end);
                $comparison_end = clone $current_start;
                $comparison_start = clone $comparison_end;
                $comparison_start->modify("-{$interval->days} days");
        }

        return [
            'days' => $days,
            'start_date' => $comparison_start->format('Y-m-d'),
            'end_date' => $comparison_end->format('Y-m-d')
        ];
    }
}
