using FastEndpoints;

namespace SpaceGame.Api.PlayerFaction.Api;

public sealed class DeletePlayerOrganizationRequest
{
    public string OrganizationId { get; set; } = string.Empty;
}

public sealed class DeletePlayerOrganizationHandler(WorldService worldService) : Endpoint<DeletePlayerOrganizationRequest, PlayerFactionSnapshot>
{
    public override void Configure()
    {
        Delete("/api/player-faction/organizations/{organizationId}");
    }

    public override async Task HandleAsync(DeletePlayerOrganizationRequest request, CancellationToken cancellationToken)
    {
        try
        {
            var snapshot = worldService.DeletePlayerOrganization(request.OrganizationId);
            if (snapshot is null)
            {
                await SendNotFoundAsync(cancellationToken);
                return;
            }

            await SendOkAsync(snapshot, cancellationToken);
        }
        catch (InvalidOperationException ex)
        {
            AddError(ex.Message);
            await SendErrorsAsync(cancellation: cancellationToken);
        }
    }
}
