using FastEndpoints;

namespace SpaceGame.Api.Universe.Api;

public sealed class CreateFactionHandler(WorldService worldService) : Endpoint<CreateFactionCommandRequest, FactionSnapshot>
{
    public override void Configure()
    {
        Post("/api/gm/factions");
        Policies(AuthPolicyNames.GmAccess);
    }

    public override async Task HandleAsync(CreateFactionCommandRequest request, CancellationToken cancellationToken)
    {
        try
        {
            await SendOkAsync(worldService.CreateFaction(request.FactionId), cancellationToken);
        }
        catch (InvalidOperationException ex)
        {
            AddError(ex.Message);
            await SendErrorsAsync(cancellation: cancellationToken);
        }
    }
}
