using System.ComponentModel.DataAnnotations;
using Asp.Versioning;
using Baya.Application.Features.Verification.Commands.ReviewStep;
using Baya.Application.Features.Verification.Commands.ScanExpiringCredentials;
using Baya.Application.Features.Verification.Commands.SuspendVerification;
using Baya.Application.Features.Verification.Queries.GetVerificationDetail;
using Baya.Application.Features.Verification.Queries.ListPendingSteps;
using Baya.Application.Models.Common;
using Baya.Application.Models.Verification;
using Baya.Infrastructure.Identity.Identity.PermissionManager;
using Baya.WebFramework.Attributes;
using Baya.WebFramework.BaseController;
using Mediator;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.RateLimiting;
using Baya.WebFramework.ServiceConfiguration;

namespace Baya.Web.Api.Controllers.V1;

[ApiVersion("1")]
[ApiController]
[Route("api/v{version:apiVersion}/[controller]")]
[Authorize(ConstantPolicies.DynamicPermission)]
[EnableRateLimiting(RateLimitingServiceExtension.SensitivePolicy)]
[Display(Description = "Admin verification review queue, decisions, suspension and expiry scan")]
public sealed class AdminVerificationsController(ISender sender) : BaseController
{
    [HttpGet]
    [ProducesOkApiResponseType<PagedResult<AdminPendingStepDto>>]
    public async Task<IActionResult> List([FromQuery] AdminListPendingStepsQuery query, CancellationToken cancellationToken)
        => OperationResult(await sender.Send(query, cancellationToken));

    [HttpGet("{nurseVerificationId}")]
    [ProducesOkApiResponseType<AdminVerificationDetailDto>]
    public async Task<IActionResult> Get(long nurseVerificationId, CancellationToken cancellationToken)
        => OperationResult(await sender.Send(new AdminGetVerificationDetailQuery(nurseVerificationId), cancellationToken));

    [HttpPost("steps/{stepId}/decide")]
    [ProducesOkApiResponseType<ReviewStepResult>]
    public async Task<IActionResult> Decide(long stepId, AdminReviewStepCommand command, CancellationToken cancellationToken)
        => OperationResult(await sender.Send(command with { StepId = stepId }, cancellationToken));

    [HttpPost("{nurseVerificationId}/suspend")]
    [ProducesOkApiResponseType]
    public async Task<IActionResult> Suspend(long nurseVerificationId, AdminSuspendVerificationCommand command, CancellationToken cancellationToken)
        => OperationResult(await sender.Send(command with { NurseVerificationId = nurseVerificationId }, cancellationToken));

    [HttpPost("scan_expiring")]
    [ProducesOkApiResponseType<ScanExpiringResult>]
    public async Task<IActionResult> ScanExpiring(ScanExpiringCredentialsCommand command, CancellationToken cancellationToken)
        => OperationResult(await sender.Send(command, cancellationToken));
}
