using System.Security.Claims; using Microsoft.AspNetCore.Mvc; using Nuuru.Server.Services; namespace Nuuru.Server.Controllers { [ApiController] [Route("api/[controller]")] public class IntegrityController : ControllerBase { private readonly IIntegrityService _integrityService; private readonly ILogger _logger; public IntegrityController(IIntegrityService integrityService, ILogger logger) { _integrityService = integrityService; _logger = logger; } [HttpPost("report-error")] public async Task ReportError([FromBody] ReportErrorRequest request) { if (!_integrityService.IsEnabled) { return BadRequest(new { message = "Integrity service is not enabled." }); } if (string.IsNullOrEmpty(request.Error)) { return BadRequest(new { message = "Error message is required." }); } var ipAddress = HttpContext.Connection.RemoteIpAddress?.ToString(); var userId = User.Identity?.IsAuthenticated == true ? User.FindFirst(ClaimTypes.NameIdentifier)?.Value : null; try { var success = await _integrityService.ReportErrorAsync(request.Error, ipAddress, userId); if (!success) { return StatusCode(500, new { message = "Failed to report error to integrity service." }); } } catch (Exception ex) { _logger.LogError(ex, "Error reporting integrity error"); return StatusCode(500, new { message = "An error occurred while reporting the error." }); } return Ok(new { message = "Error reported successfully." }); } } public class ReportErrorRequest { public string Error { get; set; } = string.Empty; } }