using Microsoft.AspNetCore.Mvc;
using ControlCenter.Services;
namespace ControlCenter.Controllers;
///
/// REST API for querying agent fleet status.
/// Provides the initial data load for the Command Hub,
/// while real-time updates flow through the AgentStatus SignalR hub.
///
/// API contract for Rex (Frontend):
///
/// - GET /api/agents — Returns all known agents with current status
/// - GET /api/agents/{agentId} — Returns a specific agent's status
///
///
[ApiController]
[Route("api/[controller]")]
public class AgentsController : ControllerBase
{
private readonly ILogger _logger;
private readonly GatewayEventBridgeService _bridgeService;
public AgentsController(
ILogger logger,
GatewayEventBridgeService bridgeService)
{
_logger = logger;
_bridgeService = bridgeService;
}
///
/// Gets the current fleet status — all known agents with their latest state.
/// This is the initial load endpoint; subsequent updates arrive via SignalR.
///
/// An array of agent card data for the entire fleet.
/// Returns the fleet snapshot.
[HttpGet]
[ProducesResponseType(typeof(AgentCardData[]), StatusCodes.Status200OK)]
public IActionResult GetAgents()
{
var snapshot = _bridgeService.GetFleetSnapshot();
_logger.LogDebug("Fleet snapshot requested: {Count} agents", snapshot.Length);
return Ok(snapshot);
}
///
/// Gets the current status of a specific agent.
///
/// The agent identifier, e.g. "otto", "dex".
/// The agent's current card data.
/// Returns the agent's status.
/// Agent not found in the fleet state.
[HttpGet("{agentId}")]
[ProducesResponseType(typeof(AgentCardData), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public IActionResult GetAgent(string agentId)
{
var snapshot = _bridgeService.GetFleetSnapshot();
var agent = snapshot.FirstOrDefault(a =>
a.Id.Equals(agentId, StringComparison.OrdinalIgnoreCase));
if (agent is null)
{
_logger.LogWarning("Agent not found: {AgentId}", agentId);
return NotFound(new { error = $"Agent '{agentId}' not found" });
}
return Ok(agent);
}
}