using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;


/// <summary>
/// Work queue endpoints for each workflow stage.
/// Each queue returns batches filtered by status and sorted by submission time ASC
/// so the oldest pending item is always at the top.
/// </summary>
[ApiController]
[Route("api/v1/work-queue")]
[Produces("application/json")]
[Authorize]
public class WorkQueueController : ControllerBase
{
    private readonly IWorkQueueService _workQueue;

    public WorkQueueController(IWorkQueueService workQueue)
    {
        _workQueue = workQueue;
    }

    /// <summary>
    /// Returns batches in PendingVerification status, sorted by submittedAt ASC.
    /// Verifiers use this queue to pick the next batch to verify.
    /// </summary>
    [HttpGet("verification")]
    [Authorize(Roles = "VERIFIER,CLINICAL_APPROVER,ADMINISTRATOR")]
    [ProducesResponseType(typeof(ApiResponse<WorkQueueResponse>), StatusCodes.Status200OK)]
    public async Task<IActionResult> GetVerificationQueue(
        [FromQuery] int page = 1,
        [FromQuery] int pageSize = 20,
        [FromQuery] string sortBy = "updatedAt",
        [FromQuery] string sortDirection = "asc")
    {
        var result = await _workQueue.GetVerificationQueueAsync(page, pageSize, sortBy, sortDirection);
        return Ok(ApiResponse<WorkQueueResponse>.Ok(result));
    }

    /// <summary>
    /// Returns batches awaiting or currently in data entry.
    /// Includes batches in Uploaded (awaiting assignment), InEntry (being entered),
    /// and Rejected (returned for re-entry) statuses.
    /// Data entry clerks use this queue to find their next assignment.
    /// </summary>
    [HttpGet("entry")]
    [Authorize(Roles = "DATA_ENTRY_CLERK,ADMINISTRATOR")]
    [ProducesResponseType(typeof(ApiResponse<WorkQueueResponse>), StatusCodes.Status200OK)]
    public async Task<IActionResult> GetEntryQueue(
        [FromQuery] int page = 1,
        [FromQuery] int pageSize = 20,
        [FromQuery] string sortBy = "updatedAt",
        [FromQuery] string sortDirection = "asc")
    {
        var result = await _workQueue.GetEntryQueueAsync(page, pageSize, sortBy, sortDirection);
        return Ok(ApiResponse<WorkQueueResponse>.Ok(result));
    }

    /// <summary>
    /// Returns batches in AwaitingClinicalApproval status, sorted by submittedAt ASC.
    /// Clinical approvers use this queue to find batches that need clinical sign-off
    /// after verification. Only batch types configured to require clinical approval
    /// in site config will appear here.
    /// </summary>
    [HttpGet("clinical-approval")]
    [Authorize(Roles = "CLINICAL_APPROVER,ADMINISTRATOR")]
    [ProducesResponseType(typeof(ApiResponse<WorkQueueResponse>), StatusCodes.Status200OK)]
    public async Task<IActionResult> GetClinicalApprovalQueue(
        [FromQuery] int page = 1,
        [FromQuery] int pageSize = 20,
        [FromQuery] string sortBy = "updatedAt",
        [FromQuery] string sortDirection = "asc")
    {
        var result = await _workQueue.GetClinicalApprovalQueueAsync(page, pageSize, sortBy, sortDirection);
        return Ok(ApiResponse<WorkQueueResponse>.Ok(result));
    }

    /// <summary>
    /// Returns aggregate work queue health metrics for the supervisor dashboard.
    /// </summary>
    [HttpGet("overview")]
    [Authorize(Roles = "ADMINISTRATOR")]
    [ProducesResponseType(typeof(ApiResponse<WorkQueueOverviewResponse>), StatusCodes.Status200OK)]
    [ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status403Forbidden)]
    public async Task<IActionResult> GetOverview()
    {
        var overview = await _workQueue.GetOverviewAsync();
        return Ok(ApiResponse<WorkQueueOverviewResponse>.Ok(overview));
    }
}