using System.ComponentModel.DataAnnotations;
using Asp.Versioning;
using Baya.Application.Features.Identity.Commands.AddNurseBankAccount;
using Baya.Application.Features.Identity.Commands.SetPrimaryBankAccount;
using Baya.Application.Features.Identity.Commands.TriggerBankAccountOwnershipInquiry;
using Baya.Application.Features.Identity.Queries.ListNurseBankAccounts;
using Baya.Application.Models.Identity;
using Baya.WebFramework.Attributes;
using Baya.WebFramework.BaseController;
using Baya.WebFramework.ServiceConfiguration;
using Mediator;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.RateLimiting;

namespace Baya.Web.Api.Controllers.V1;

[ApiVersion("1")]
[ApiController]
[Route("api/v{version:apiVersion}/[controller]")]
[Authorize]
[Display(Description = "The signed-in nurse's payout bank accounts")]
public sealed class NurseBankAccountsController(ISender sender) : BaseController
{
    // Rate-limited: adding an account triggers the استعلام شبا vendor inquiry.
    [HttpPost("[action]")]
    [EnableRateLimiting(RateLimitingServiceExtension.SensitivePolicy)]
    [ProducesOkApiResponseType<NurseBankAccountDto>]
    public async Task<IActionResult> Add(AddNurseBankAccountCommand command, CancellationToken cancellationToken)
        => OperationResult(await sender.Send(command, cancellationToken));

    [HttpPost("[action]/{id}")]
    [ProducesOkApiResponseType]
    public async Task<IActionResult> SetPrimary(long id, CancellationToken cancellationToken)
        => OperationResult(await sender.Send(new SetPrimaryBankAccountCommand(id), cancellationToken));

    [HttpGet("[action]")]
    [ProducesOkApiResponseType<IReadOnlyList<NurseBankAccountDto>>]
    public async Task<IActionResult> List(CancellationToken cancellationToken)
        => OperationResult(await sender.Send(new ListNurseBankAccountsQuery(), cancellationToken));

    // Rate-limited: re-runs the استعلام شبا vendor inquiry.
    [HttpPost("[action]/{id}")]
    [EnableRateLimiting(RateLimitingServiceExtension.SensitivePolicy)]
    [ProducesOkApiResponseType<NurseBankAccountDto>]
    public async Task<IActionResult> VerifyOwnership(long id, CancellationToken cancellationToken)
        => OperationResult(await sender.Send(new TriggerBankAccountOwnershipInquiryCommand(id), cancellationToken));
}
