using IntelligenceApi.Engines; using IntelligenceApi.Models; using Microsoft.AspNetCore.Mvc; namespace IntelligenceApi.Controllers; /// /// Demographics analysis endpoint. /// /// Called exclusively by the Gateway's IntelligenceApiClient after it fetches /// raw census data from the database. This container derives audience /// recommendations from the raw data — age chips, income tiers, insights. /// /// POST /api/demographics/analyze /// [ApiController] [Route("api/demographics")] public sealed class DemographicsController : ControllerBase { private readonly DemographicsAnalyzer _analyzer; private readonly ILogger _log; public DemographicsController(DemographicsAnalyzer analyzer, ILogger log) { _analyzer = analyzer; _log = log; } [HttpPost("analyze")] public IActionResult Analyze([FromBody] DemographicAnalysisRequest? request) { if (request == null || string.IsNullOrWhiteSpace(request.Zcta)) return BadRequest(new { ok = false, error = "zcta and census data are required" }); _log.LogInformation("[Demographics] Analyze | ZCTA={Zcta}", request.Zcta); try { var result = _analyzer.Analyze(request); return Ok(result); } catch (Exception ex) { _log.LogError(ex, "[Demographics] Analysis error | ZCTA={Zcta}", request.Zcta); return StatusCode(500, new { ok = false, error = "Demographics analysis error" }); } } }