using FastEndpoints;

namespace SpaceGame.Api.Universe.Api;

public sealed class SpawnShipHandler(WorldService worldService) : Endpoint<SpawnShipCommandRequest, ShipSnapshot>
{
    public override void Configure()
    {
        Post("/api/gm/ships");
        Policies(AuthPolicyNames.GmAccess);
    }

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