using FastEndpoints;

namespace SpaceGame.Api.PlayerFaction.Api;

public sealed class UpsertPlayerAutomationPolicyHandler(WorldService worldService) : Endpoint<PlayerAutomationPolicyCommandRequest, PlayerFactionSnapshot>
{
    public override void Configure()
    {
        Post("/api/player-faction/automation-policies");
        Put("/api/player-faction/automation-policies/{automationPolicyId}");
    }

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

        await SendOkAsync(snapshot, cancellationToken);
    }
}
