using EntKube.Clusters.Domain;
using EntKube.SharedKernel.Contracts;
using Microsoft.AspNetCore.Mvc;

namespace EntKube.Clusters.Features.RegisterCluster;

/// <summary>
/// Maps the HTTP POST /api/clusters endpoint. Receives a registration request,
/// delegates to the handler, and returns the new cluster's ID on success.
/// </summary>
public static class RegisterClusterEndpoint
{
    public static void Map(IEndpointRouteBuilder app)
    {
        app.MapPost("/api/clusters", async (
            [FromBody] RegisterClusterRequest request,
            [FromServices] RegisterClusterHandler handler,
            CancellationToken ct) =>
        {
            EntKube.SharedKernel.Domain.Result<Guid> result = await handler.HandleAsync(request, ct);

            if (result.IsFailure)
            {
                return Results.BadRequest(ApiResponse<Guid>.Fail(result.Error!));
            }

            return Results.Created($"/api/clusters/{result.Value}", ApiResponse<Guid>.Ok(result.Value!));
        });
    }
}
