<?php

namespace Modules\Routing\Http\Controllers;

use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Cache;
use Modules\Routing\Http\Resources\PopularRouteResource;
use Modules\Routing\Models\PopularRoute;

class PopularRouteController extends Controller
{
    public function index(): AnonymousResourceCollection
    {
        $popularRoutes = Cache::tags('routes')->remember(
            'routes:popular',
            now()->addMinutes(5),
            fn () => PopularRoute::query()
                ->where('is_active', true)
                ->with(['fromDestination', 'toDestination'])
                ->get(),
        );

        return PopularRouteResource::collection($popularRoutes);
    }
}
