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

namespace EntKube.Identity.Features.CreateTenant;

/// <summary>
/// Maps POST /api/tenants — creates a new tenant organization.
/// </summary>
public static class CreateTenantEndpoint
{
    public static void Map(IEndpointRouteBuilder app)
    {
        app.MapPost("/api/tenants", async (
            [FromBody] CreateTenantRequest request,
            [FromServices] CreateTenantHandler 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/tenants/{result.Value}", ApiResponse<Guid>.Ok(result.Value!));
        });
    }
}
