using System.ComponentModel.DataAnnotations;
using Asp.Versioning;
using Baya.Application.Features.Payouts.Queries.GetNurseEarnings;
using Baya.Application.Features.Payouts.Queries.GetNurseEarningsBalance;
using Baya.Application.Features.Payouts.Queries.GetNursePayoutDetail;
using Baya.Application.Features.Payouts.Queries.GetNursePayoutHistory;
using Baya.Application.Models.Common;
using Baya.Application.Models.Payouts;
using Baya.WebFramework.Attributes;
using Baya.WebFramework.BaseController;
using Mediator;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace Baya.Web.Api.Controllers.V1;

/// <summary>The signed-in nurse's own payout history — tenancy-scoped to <c>ICurrentUser</c> (a nurse can never
/// read another nurse's payouts). Feeds f12's earnings screen: status, net, masked IBAN + transfer reference, and
/// any clawback applied.</summary>
[ApiVersion("1")]
[ApiController]
[Route("api/v{version:apiVersion}/nurse_payouts")]
[Authorize]
[Display(Description = "The signed-in nurse's payout history")]
public sealed class NursePayoutsController(ISender sender) : BaseController
{
    [HttpGet("history")]
    [ProducesOkApiResponseType<PagedResult<NursePayoutHistoryDto>>]
    public async Task<IActionResult> History([FromQuery] GetNursePayoutHistoryQuery query, CancellationToken cancellationToken)
        => OperationResult(await sender.Send(query, cancellationToken));

    // The four-bucket balance + ledger-derived signed net payable (REQ-025).
    [HttpGet("earnings_balance")]
    [ProducesOkApiResponseType<NurseEarningsBalanceDto>]
    public async Task<IActionResult> EarningsBalance(CancellationToken cancellationToken)
        => OperationResult(await sender.Send(new GetNurseEarningsBalanceQuery(), cancellationToken));

    // The per-booking earnings list with server-derived money-state, filterable by state (REQ-025).
    [HttpGet("earnings")]
    [ProducesOkApiResponseType<PagedResult<NurseEarningsItemDto>>]
    public async Task<IActionResult> Earnings([FromQuery] GetNurseEarningsQuery query, CancellationToken cancellationToken)
        => OperationResult(await sender.Send(query, cancellationToken));

    // The nurse's own payout detail — batch window + covered bookings for reconciliation (REQ-025).
    [HttpGet("{id:long}")]
    [ProducesOkApiResponseType<NursePayoutDetailDto>]
    public async Task<IActionResult> Detail(long id, CancellationToken cancellationToken)
        => OperationResult(await sender.Send(new GetNursePayoutDetailQuery(id), cancellationToken));
}
