using Microsoft.AspNetCore.Mvc;
using ControlCenter.Services;

namespace ControlCenter.Controllers;

/// <summary>
/// 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.
///
/// <para>API contract for Rex (Frontend):</para>
/// <list type="bullet">
///   <item><c>GET /api/agents</c> — Returns all known agents with current status</item>
///   <item><c>GET /api/agents/{agentId}</c> — Returns a specific agent's status</item>
/// </list>
/// </summary>
[ApiController]
[Route("api/[controller]")]
public class AgentsController : ControllerBase
{
    private readonly ILogger<AgentsController> _logger;
    private readonly GatewayEventBridgeService _bridgeService;

    public AgentsController(
        ILogger<AgentsController> logger,
        GatewayEventBridgeService bridgeService)
    {
        _logger = logger;
        _bridgeService = bridgeService;
    }

    /// <summary>
    /// Gets the current fleet status — all known agents with their latest state.
    /// This is the initial load endpoint; subsequent updates arrive via SignalR.
    /// </summary>
    /// <returns>An array of agent card data for the entire fleet.</returns>
    /// <response code="200">Returns the fleet snapshot.</response>
    [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);
    }

    /// <summary>
    /// Gets the current status of a specific agent.
    /// </summary>
    /// <param name="agentId">The agent identifier, e.g. "otto", "dex".</param>
    /// <returns>The agent's current card data.</returns>
    /// <response code="200">Returns the agent's status.</response>
    /// <response code="404">Agent not found in the fleet state.</response>
    [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);
    }
}