using FastEndpoints;

namespace SpaceGame.Api.PlayerFaction.Api;

public sealed class UpsertPlayerReinforcementPolicyHandler(WorldService worldService) : Endpoint<PlayerReinforcementPolicyCommandRequest, PlayerFactionSnapshot>
{
    public override void Configure()
    {
        Post("/api/player-faction/reinforcement-policies");
        Put("/api/player-faction/reinforcement-policies/{reinforcementPolicyId}");
    }

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

        await SendOkAsync(snapshot, cancellationToken);
    }
}
