using FastEndpoints;

namespace SpaceGame.Api.Universe.Api;

public sealed class SpawnStationHandler(WorldService worldService) : Endpoint<SpawnStationCommandRequest, StationSnapshot>
{
    public override void Configure()
    {
        Post("/api/gm/stations");
        Policies(AuthPolicyNames.GmAccess);
    }

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