using FastEndpoints;

namespace SpaceGame.Api.PlayerFaction.Api;

public sealed class UpsertPlayerDirectiveHandler(WorldService worldService) : Endpoint<PlayerDirectiveCommandRequest, PlayerFactionSnapshot>
{
    public override void Configure()
    {
        Post("/api/player-faction/directives");
        Put("/api/player-faction/directives/{directiveId}");
    }

    public override async Task HandleAsync(PlayerDirectiveCommandRequest request, CancellationToken cancellationToken)
    {
        var directiveId = Route<string?>("directiveId");
        var snapshot = worldService.UpsertPlayerDirective(string.IsNullOrWhiteSpace(directiveId) ? null : directiveId, request);
        if (snapshot is null)
        {
            await SendNotFoundAsync(cancellationToken);
            return;
        }

        await SendOkAsync(snapshot, cancellationToken);
    }
}
