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

namespace EntKube.Clusters.Features.GetClusters;

/// <summary>
/// Lists all registered clusters. The BFF (Web service) calls this endpoint
/// to populate the clusters dashboard. Returns a lightweight summary of each
/// cluster's name, status, and last health check time.
/// </summary>
public static class GetClustersEndpoint
{
    public static void Map(IEndpointRouteBuilder app)
    {
        app.MapGet("/api/clusters", async (
            [FromServices] IClusterRepository repository,
            CancellationToken ct) =>
        {
            IReadOnlyList<KubernetesCluster> clusters = await repository.GetAllAsync(ct);

            List<ClusterSummary> summaries = clusters.Select(c => new ClusterSummary(
                c.Id,
                c.Name,
                c.ApiServerUrl,
                c.Status.ToString(),
                c.LastHealthCheckAt)).ToList();

            return Results.Ok(ApiResponse<List<ClusterSummary>>.Ok(summaries));
        });
    }
}

public record ClusterSummary(
    Guid Id,
    string Name,
    string ApiServerUrl,
    string Status,
    DateTimeOffset? LastHealthCheckAt);
