using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using VigilCare.ClinicalContracts.Sync; [ApiController] [Produces("application/json")] public class ClinicalSyncController : ControllerBase { private readonly IClinicalSyncService _sync; public ClinicalSyncController(IClinicalSyncService sync) => _sync = sync; /// Upload a buffered sync batch from a ward gateway. [HttpPost("api/v1/sync/batches")] [Authorize(AuthenticationSchemes = GatewayApiKeyAuthenticationHandler.SchemeName)] [ProducesResponseType(typeof(ApiResponse), StatusCodes.Status201Created)] public async Task UploadBatch( [FromBody] ClinicalSyncBatchRequest request, CancellationToken ct) { var result = await _sync.UploadBatchAsync(request, ct); return StatusCode(201, ApiResponse.Created(result)); } /// Poll batch processing status and conflicts. [HttpGet("api/v1/sync/batches/{batchId:guid}")] [Authorize(AuthenticationSchemes = $"{JwtBearerDefaults.AuthenticationScheme},{GatewayApiKeyAuthenticationHandler.SchemeName}")] [ProducesResponseType(typeof(ApiResponse), StatusCodes.Status200OK)] [ProducesResponseType(typeof(ApiResponse), StatusCodes.Status404NotFound)] public async Task GetBatchStatus(Guid batchId, CancellationToken ct) { var result = await _sync.GetBatchStatusAsync(batchId, ct); return Ok(ApiResponse.Ok(result)); } /// Last 50 sync batches for a gateway (admin JWT). [HttpGet("api/v1/sites/{siteId:guid}/gateways/{gatewayId:guid}/sync-history")] [Authorize] [AuthorizePermission(ClinicalPermissions.UsersAdmin)] [ProducesResponseType(typeof(ApiResponse>), StatusCodes.Status200OK)] public async Task GetSyncHistory( Guid siteId, Guid gatewayId, CancellationToken ct) { var history = await _sync.GetSyncHistoryAsync(siteId, gatewayId, 50, ct); return Ok(ApiResponse>.Ok(history)); } }