using Heimdall.Application.DTOs; using Heimdall.Application.Services; using Heimdall.Infrastructure.Security; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.RateLimiting; namespace Heimdall.Api.Controllers; [ApiController] [Route("api/agents")] [EnableRateLimiting("Api")] public class AgentsController : ControllerBase { private readonly IAgentService _agentService; public AgentsController(IAgentService agentService) => _agentService = agentService; /// Register an agent using a site registration token. [AllowAnonymous] [HttpPost("register")] [ProducesResponseType(typeof(RegisterAgentResponse), StatusCodes.Status200OK)] [ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)] public async Task Register( [FromBody] RegisterAgentRequest request, CancellationToken ct) { // Extract API key from header (may be empty for registration) var apiKey = Request.Headers.Authorization.FirstOrDefault() ?.Replace("Bearer ", "", StringComparison.OrdinalIgnoreCase); var result = await _agentService.RegisterAsync(request, apiKey, ct); return result.IsSuccess ? Ok(result.Data) : StatusCode(result.StatusCode ?? 401, new { error = result.Error }); } /// Send heartbeat from agent (API Key auth). [Authorize(AuthenticationSchemes = "ApiKey")] [HttpPost("{id:guid}/heartbeat")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(typeof(void), StatusCodes.Status400BadRequest)] public async Task Heartbeat( Guid id, [FromBody] AgentHeartbeatRequest request, CancellationToken ct) { var result = await _agentService.HeartbeatAsync(id, request, ct); return result.IsSuccess ? Ok() : StatusCode(result.StatusCode ?? 400, new { error = result.Error }); } /// Get agent details (JWT auth). [Authorize(AuthenticationSchemes = "JwtBearer", Policy = AppPermissions.SiteRead)] [HttpGet("{id:guid}")] [ProducesResponseType(typeof(AgentResponse), StatusCodes.Status200OK)] [ProducesResponseType(typeof(void), StatusCodes.Status404NotFound)] public async Task GetById(Guid id, CancellationToken ct) { var result = await _agentService.GetByIdAsync(id, ct); return result.IsSuccess ? Ok(result.Data) : NotFound(new { error = result.Error }); } /// List agents for a site. [Authorize(AuthenticationSchemes = "JwtBearer", Policy = AppPermissions.SiteRead)] [HttpGet("by-site/{siteId:guid}")] [ProducesResponseType(typeof(PagedResult), StatusCodes.Status200OK)] [ProducesResponseType(typeof(void), StatusCodes.Status400BadRequest)] public async Task ListBySite( Guid siteId, [FromQuery] int page = 1, [FromQuery] int pageSize = 20, CancellationToken ct = default) { var result = await _agentService.ListBySiteAsync(siteId, page, pageSize, ct); if (!result.IsSuccess) return StatusCode(result.StatusCode ?? 400, new { error = result.Error }); return Ok(result.Data); } /// List all agents across all sites. [Authorize(AuthenticationSchemes = "JwtBearer", Policy = AppPermissions.SiteRead)] [HttpGet] [ProducesResponseType(typeof(PagedResult), StatusCodes.Status200OK)] [ProducesResponseType(typeof(void), StatusCodes.Status400BadRequest)] public async Task ListAll( [FromQuery] int page = 1, [FromQuery] int pageSize = 20, CancellationToken ct = default) { var result = await _agentService.ListAllAsync(page, pageSize, ct); if (!result.IsSuccess) return StatusCode(result.StatusCode ?? 400, new { error = result.Error }); return Ok(result.Data); } }