using GameList.Data;
using GameList.Infrastructure;
using GameList.Domain;

namespace GameList.Endpoints;

public static class ResultsEndpoints
{
    public static void MapResultsEndpoints(this IEndpointRouteBuilder app)
    {
        var group = app.MapGroup("/api/results")
            .RequireAuthorization()
            .AddEndpointFilter(new PhaseRequirementFilter(Phase.Results));

        group.MapGet("/", async (HttpContext ctx, AppDbContext db, ResultsWorkflowService service) =>
        {
            var player = await EndpointHelpers.GetAuthenticatedPlayer(ctx, db);
            if (player is null)
                return EndpointHelpers.UnauthorizedError();

            return await service.GetResultsAsync(player);
        });
    }
}

