using RpgRoller.Contracts;
using RpgRoller.Services;

namespace RpgRoller.Api;

internal static class CharacterEndpoints
{
    public static RouteGroupBuilder MapCharacterEndpoints(this RouteGroupBuilder group)
    {
        group.MapPost("/characters", (CreateCharacterRequest request, HttpContext context, IGameService game) =>
        {
            var result = game.CreateCharacter(context.GetRequiredSessionToken(), request.Name, request.CampaignId);
            return ApiResultMapper.ToApiResult(result);
        });

        group.MapPut("/characters/{characterId:guid}", (Guid characterId, UpdateCharacterRequest request, HttpContext context, IGameService game) =>
        {
            var result = game.UpdateCharacter(context.GetRequiredSessionToken(), characterId, request.Name, request.CampaignId, request.OwnerUsername);
            return ApiResultMapper.ToApiResult(result);
        });

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

        group.MapPost("/characters/{characterId:guid}/activate", (Guid characterId, HttpContext context, IGameService game) =>
        {
            var result = game.ActivateCharacter(context.GetRequiredSessionToken(), characterId);
            return ApiResultMapper.ToApiResult(result);
        });

        group.MapGet("/characters/{characterId:guid}/sheet", (Guid characterId, HttpContext context, IGameService game) =>
        {
            var result = game.GetCharacterSheet(context.GetRequiredSessionToken(), characterId);
            return ApiResultMapper.ToApiResult(result);
        });

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

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

        return group;
    }
}