using RpgRoller.Contracts;
using RpgRoller.Services;

namespace RpgRoller.Api;

internal static class CampaignEndpoints
{
    public static RouteGroupBuilder MapCampaignEndpoints(this RouteGroupBuilder group)
    {
        group.MapPost("/campaigns", (CreateCampaignRequest request, HttpContext context, IGameService game) =>
        {
            var result = game.CreateCampaign(context.GetRequiredSessionToken(), request.Name, request.RulesetId);
            return ApiResultMapper.ToApiResult(result);
        });

        group.MapGet("/campaigns", (HttpContext context, IGameService game) =>
        {
            var result = game.GetCampaigns(context.GetRequiredSessionToken());
            return ApiResultMapper.ToApiResult(result);
        });

        group.MapGet("/campaigns/options", (HttpContext context, IGameService game) =>
        {
            var result = game.GetCharacterCampaignOptions(context.GetRequiredSessionToken());
            return ApiResultMapper.ToApiResult(result);
        });

        group.MapGet("/campaigns/{campaignId:guid}", (Guid campaignId, HttpContext context, IGameService game) =>
        {
            var result = game.GetCampaign(context.GetRequiredSessionToken(), campaignId);
            return ApiResultMapper.ToApiResult(result);
        });

        group.MapGet("/campaigns/{campaignId:guid}/log", (Guid campaignId, HttpContext context, IGameService game) =>
        {
            var result = game.GetCampaignLog(context.GetRequiredSessionToken(), campaignId);
            return ApiResultMapper.ToApiResult(result);
        });

        group.MapGet("/campaigns/{campaignId:guid}/log/page", (Guid campaignId, Guid? afterRollId, int? limit, HttpContext context, IGameService game) =>
        {
            var result = game.GetCampaignLogPage(context.GetRequiredSessionToken(), campaignId, afterRollId, limit);
            return ApiResultMapper.ToApiResult(result);
        });

        group.MapDelete("/campaigns/{campaignId:guid}", (Guid campaignId, HttpContext context, IGameService game) =>
        {
            var result = game.DeleteCampaign(context.GetRequiredSessionToken(), campaignId);
            return ApiResultMapper.ToApiResult(result);
        });

        return group;
    }
}