using FastEndpoints;

namespace SpaceGame.Api.PlayerFaction.Api;

public sealed class UpsertPlayerProductionProgramHandler(WorldService worldService) : Endpoint<PlayerProductionProgramCommandRequest, PlayerFactionSnapshot>
{
    public override void Configure()
    {
        Post("/api/player-faction/production-programs");
        Put("/api/player-faction/production-programs/{productionProgramId}");
    }

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

        await SendOkAsync(snapshot, cancellationToken);
    }
}
