<?php

namespace FleekDash\Routes\Groups;

use FleekDash\Libs\API\Route;
use FleekDash\Libs\API\RouteAuthMiddleware;
use FleekDash\Controllers\AnalyticsController;
use FleekDash\Controllers\StatsController;

/**
 * Analytics Routes
 *
 * API routes for analytics and statistics:
 * - Visitors, pageviews, popular posts
 * - User registrations and engagement
 * - Referrers, devices, geographic data
 * - Real-time analytics
 * - KPI summary
 * - Cache management
 *
 * @package FleekDash\Routes\Groups
 * @since 2.2.0
 */
class AnalyticsRoutes
{
    /**
     * Register all analytics-related routes
     *
     * @param Route $route Route instance
     * @return void
     */
    public static function register(Route $route)
    {
        // Stats routes with prefix
        $route->prefix('stats', function (Route $route) {
            // Post statistics
            $route->get('/posts/by-date', [StatsController::class, 'postsByDate'], RouteAuthMiddleware::authenticated(), [
                'start_date' => [
                    'required' => false,
                    'type' => 'string',
                    'description' => 'Start date in YYYY-MM-DD format',
                ],
                'end_date' => [
                    'required' => false,
                    'type' => 'string',
                    'description' => 'End date in YYYY-MM-DD format',
                ],
                'post_type' => [
                    'required' => false,
                    'default' => 'post',
                    'type' => 'string',
                    'description' => 'Post type to filter by',
                ],
                'post_status' => [
                    'required' => false,
                    'default' => 'publish',
                    'type' => 'string',
                    'description' => 'Post status to filter by',
                ],
                'group_by' => [
                    'required' => false,
                    'default' => 'day',
                    'type' => 'string',
                    'description' => 'Group results by day, week, or month',
                    'enum' => ['day', 'week', 'month'],
                ],
            ]);

            $route->get('/posts/by-author', [StatsController::class, 'postsByAuthor'], RouteAuthMiddleware::authenticated(), [
                'limit' => [
                    'required' => false,
                    'default' => 10,
                    'type' => 'integer',
                    'description' => 'Maximum number of authors to return',
                ],
                'post_type' => [
                    'required' => false,
                    'default' => 'post',
                    'type' => 'string',
                    'description' => 'Post type to filter by',
                ],
                'post_status' => [
                    'required' => false,
                    'default' => 'publish',
                    'type' => 'string',
                    'description' => 'Post status to filter by',
                ],
            ]);
        });

        // Analytics routes with prefix
        $route->prefix('analytics', function (Route $route) {
            $analytics_controller = new AnalyticsController();

            // Analytics data endpoints (authenticated users only)
            $route->get('visitors', [$analytics_controller, 'get_visitors'], RouteAuthMiddleware::authenticated());
            $route->get('pageviews', [$analytics_controller, 'get_pageviews'], RouteAuthMiddleware::authenticated());
            $route->get('popular-posts', [$analytics_controller, 'get_popular_posts'], RouteAuthMiddleware::authenticated());
            $route->get('user-registrations', [$analytics_controller, 'get_user_registrations'], RouteAuthMiddleware::authenticated());
            $route->get('recent-users', [$analytics_controller, 'get_recent_users'], RouteAuthMiddleware::authenticated());
            $route->get('user-engagement', [$analytics_controller, 'get_user_engagement'], RouteAuthMiddleware::authenticated());
            $route->get('referrers', [$analytics_controller, 'get_referrers'], RouteAuthMiddleware::authenticated());
            $route->get('devices', [$analytics_controller, 'get_devices'], RouteAuthMiddleware::authenticated());
            $route->get('geographic', [$analytics_controller, 'get_geographic'], RouteAuthMiddleware::authenticated());
            $route->get('real-time', [$analytics_controller, 'get_real_time'], RouteAuthMiddleware::authenticated());
            $route->get('kpi-summary', [$analytics_controller, 'get_kpi_summary'], RouteAuthMiddleware::authenticated());

            // Export route (admin only - sensitive data)
            $route->get('export', [$analytics_controller, 'export_analytics'], RouteAuthMiddleware::admin());

            // Cache management routes (admin only)
            $route->get('cache/stats', [$analytics_controller, 'get_cache_stats'], RouteAuthMiddleware::admin());
            $route->post('cache/clear', [$analytics_controller, 'clear_cache'], RouteAuthMiddleware::admin());
            $route->post('cache/warm', [$analytics_controller, 'warm_cache'], RouteAuthMiddleware::admin());
        });
    }
}
