using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Nexus.Api.Services;

namespace Nexus.Api.Controllers;

/// <summary>
/// Health-check endpoint for the Gateway WebSocket connection.
/// </summary>
[ApiController]
[AllowAnonymous]
public class GatewayHealthController(IGatewayConnector connector) : ControllerBase
{
    /// <summary>
    /// Returns the current Gateway WebSocket connection health.
    /// </summary>
    [HttpGet("/api/health/gateway")]
    public IResult GetGatewayHealth()
    {
        return Results.Ok(new
        {
            status = connector.ConnectionState.ToString().ToLowerInvariant(),
            connected = connector.ConnectionState == GatewayConnectionState.Connected,
            gatewayVersion = connector.GatewayVersion ?? "unknown",
            requiredVersion = connector.RequiredVersion,
            versionPinned = connector.RequiredVersion is not null,
            protocolVersion = connector.ProtocolVersion,
            advertisedMethodCount = connector.AdvertisedMethods.Count,
            lastConnectedAt = connector.LastConnectedAt?.ToString("o"),
            lastEventAt = connector.LastEventAt?.ToString("o"),
            reconnectAttempts = connector.ReconnectAttempts,
            message = connector.StatusMessage,
            timestamp = DateTimeOffset.UtcNow.ToString("o")
        });
    }
}
