using Predictalytics.Application.Interfaces;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;

namespace Predictalytics.Api.Endpoints;

public static class MarketEndpoints
{
    public static void MapMarketEndpoints(this IEndpointRouteBuilder routes)
    {
        var group = routes.MapGroup("/api/markets").WithTags("Markets");

        group.MapGet("/", async (IAnalyticsService svc, int? skip, int? take, string? platform, string? category, string? query, CancellationToken ct) =>
        {
            var result = await svc.GetMarketsAsync(skip ?? 0, take ?? 50, platform, category, query, ct);
            return Results.Ok(result);
        });
        
        group.MapGet("/{id:int}", async (int id, IAnalyticsService svc, CancellationToken ct) =>
        {
            var detail = await svc.GetMarketDetailAsync(id, ct);
            return detail is not null ? Results.Ok(detail) : Results.NotFound();
        });
    }
}
