using AVSCartonShipmentVerifier.DTOs; using AVSCartonShipmentVerifier.Services; using Microsoft.AspNetCore.Mvc; namespace AVSCartonShipmentVerifier.Controllers; [Route("api/verification")] public sealed class VerificationController( IShipmentVerificationService shipmentVerificationService, IScanHistoryStore scanHistoryStore, ILogger logger) : Controller { private readonly IShipmentVerificationService _shipmentVerificationService = shipmentVerificationService; private readonly IScanHistoryStore _scanHistoryStore = scanHistoryStore; private readonly ILogger _logger = logger; [HttpPost("scan")] [ValidateAntiForgeryToken] public async Task Scan([FromBody] QrScanRequestDto request, CancellationToken cancellationToken) { if (request is null) { return BadRequest(new { message = "Request payload is required." }); } try { var verificationResult = await _shipmentVerificationService.VerifyQrAsync(request.QrValue, cancellationToken); return Ok(new { result = verificationResult, recentScans = await _scanHistoryStore.GetLastFiveAsync(cancellationToken) }); } catch (OperationCanceledException) { return StatusCode(StatusCodes.Status499ClientClosedRequest); } catch (Exception exception) { _logger.LogError(exception, "Unexpected error while verifying QR scan."); return StatusCode(StatusCodes.Status500InternalServerError, new { message = "An unexpected error occurred during verification." }); } } }