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

namespace EntKube.Provisioning.Features.ProvisionService;

/// <summary>
/// Maps POST /api/services — creates a new provisioning request for a shared service.
/// </summary>
public static class ProvisionServiceEndpoint
{
    public static void Map(IEndpointRouteBuilder app)
    {
        app.MapPost("/api/services", async (
            [FromBody] ProvisionServiceRequest request,
            [FromServices] ProvisionServiceHandler 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/services/{result.Value}", ApiResponse<Guid>.Ok(result.Value!));
        });
    }
}
