<?php

declare(strict_types=1);

namespace App\Application\SmartLink\Api;

use App\Domain\SmartLink\Services\ClickStatisticsService;
use App\Framework\Attributes\Route;
use App\Framework\Http\HttpRequest;
use App\Framework\Http\Method;
use App\Framework\Router\Result\JsonResult;

/**
 * Get Click Time-Series Data for Charts
 *
 * Returns hourly click data for visualization
 */
final readonly class GetClickTimeSeries
{
    public function __construct(
        private ClickStatisticsService $statisticsService
    ) {
    }

    #[Route(path: '/api/analytics/time-series', method: Method::GET)]
    public function __invoke(HttpRequest $request): JsonResult
    {
        $hours = (int) $request->queryParameters->get('hours', '24');
        $hours = min(max($hours, 1), 168); // Limit to 1-168 hours (7 days)

        $timeSeries = $this->statisticsService->getClickTimeSeries($hours);

        return new JsonResult([
            'success' => true,
            'data' => $timeSeries,
            'meta' => [
                'hours' => $hours,
                'data_points' => count($timeSeries),
            ],
        ]);
    }
}
