using System.Security.Claims;
using System.Text.Json;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
///
/// Approval and promotion of verified digitization batches to VigilCareClinical live tables.
///
[ApiController]
[Route("api/v1/digitization-batches")]
[Produces("application/json")]
[Authorize]
public class ApprovalController : ControllerBase
{
private readonly IPromotionService _promotion;
private readonly AppDbContext _db;
private readonly PromotionRetryOptions _retryOptions;
private readonly ILogger _logger;
public ApprovalController(
IPromotionService promotion,
AppDbContext db,
IOptions retryOptions,
ILogger logger)
{
_promotion = promotion;
_db = db;
_retryOptions = retryOptions.Value;
_logger = logger;
}
///
/// Approves a verified or awaiting-clinical-approval batch and promotes its draft data
/// into live VigilCareClinical tables (Patient, Encounter, Observations).
///
/// Requires the Idempotency-Key header for safe retries. If the same key is resubmitted,
/// the original response is returned without re-executing the promotion.
///
/// If promotion fails due to infrastructure issues, the batch transitions to Approved status
/// and automatic retry is scheduled. Returns 202 Accepted with PROMOTION_DEFERRED.
///
/// Separation of duties: the approver cannot be the entry clerk or verifier of the same batch.
///
/// The batch ID to approve.
/// Optional request body with alert configuration.
/// Promotion result with live IDs for patient, encounter, and observations.
[HttpPost("{id:guid}/approve")]
[Authorize(Roles = "CLINICAL_APPROVER,ADMINISTRATOR")]
[ProducesResponseType(typeof(ApiResponse), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ApiResponse