<?php

declare(strict_types=1);

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Services\Recommendations\RecommendationFeedResolver;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

final class FeedController extends Controller
{
    public function __construct(private readonly RecommendationFeedResolver $feedResolver)
    {
    }

    public function index(Request $request): JsonResponse
    {
        $payload = $request->validate([
            'limit' => ['nullable', 'integer', 'min:1', 'max:50'],
            'cursor' => ['nullable', 'string', 'max:512'],
            'algo_version' => ['nullable', 'string', 'max:64'],
        ]);

        $result = $this->feedResolver->getFeed(
            userId: (int) $request->user()->id,
            limit: isset($payload['limit']) ? (int) $payload['limit'] : 24,
            cursor: isset($payload['cursor']) ? (string) $payload['cursor'] : null,
            algoVersion: isset($payload['algo_version']) ? (string) $payload['algo_version'] : null
        );

        return response()->json($result);
    }
}
