using MediatR;
using TakeoutSaaS.Application.App.Billings.Commands;
using TakeoutSaaS.Application.App.Billings.Dto;
using TakeoutSaaS.Domain.Tenants.Entities;
using TakeoutSaaS.Domain.Tenants.Enums;
using TakeoutSaaS.Domain.Tenants.Repositories;
using TakeoutSaaS.Shared.Abstractions.Constants;
using TakeoutSaaS.Shared.Abstractions.Exceptions;
using TakeoutSaaS.Shared.Abstractions.Ids;
using TakeoutSaaS.Shared.Abstractions.Security;

namespace TakeoutSaaS.Application.App.Billings.Handlers;

/// <summary>
/// 一键确认收款处理器（记录支付 + 立即审核通过 + 同步更新账单已收金额/状态）。
/// </summary>
public sealed class ConfirmPaymentCommandHandler(
    ITenantBillingRepository billingRepository,
    ITenantPaymentRepository paymentRepository,
    IIdGenerator idGenerator,
    ICurrentUserAccessor currentUserAccessor)
    : IRequestHandler<ConfirmPaymentCommand, PaymentRecordDto>
{
    /// <inheritdoc />
    public async Task<PaymentRecordDto> Handle(ConfirmPaymentCommand request, CancellationToken cancellationToken)
    {
        // 1. 校验操作者身份（用于写入 VerifiedBy）
        if (!currentUserAccessor.IsAuthenticated || currentUserAccessor.UserId <= 0)
        {
            throw new BusinessException(ErrorCodes.Unauthorized, "未登录或无效的操作者身份");
        }

        // 2. 查询账单
        var billing = await billingRepository.FindByIdAsync(request.BillingId, cancellationToken);
        if (billing is null)
        {
            throw new BusinessException(ErrorCodes.NotFound, "账单不存在");
        }

        // 3. 业务规则检查
        if (billing.Status == TenantBillingStatus.Paid)
        {
            throw new BusinessException(ErrorCodes.BusinessError, "已支付账单不允许重复收款");
        }

        if (billing.Status == TenantBillingStatus.Cancelled)
        {
            throw new BusinessException(ErrorCodes.BusinessError, "已取消账单不允许收款");
        }

        // 4. 金额边界：不允许超过剩余应收（与前端校验保持一致）
        var totalAmount = billing.CalculateTotalAmount();
        var remainingAmount = totalAmount - billing.AmountPaid;
        if (request.Amount > remainingAmount)
        {
            throw new BusinessException(ErrorCodes.BadRequest, "支付金额不能超过剩余应收");
        }

        // 5. 幂等校验：交易号唯一
        if (!string.IsNullOrWhiteSpace(request.TransactionNo))
        {
            var exists = await paymentRepository.GetByTransactionNoAsync(request.TransactionNo.Trim(), cancellationToken);
            if (exists is not null)
            {
                throw new BusinessException(ErrorCodes.Conflict, "交易号已存在，疑似重复提交");
            }
        }

        // 6. 构建支付记录并立即审核通过
        var now = DateTime.UtcNow;
        var payment = new TenantPayment
        {
            Id = idGenerator.NextId(),
            TenantId = billing.TenantId,
            BillingStatementId = request.BillingId,
            Amount = request.Amount,
            Method = request.Method,
            Status = TenantPaymentStatus.Pending,
            TransactionNo = string.IsNullOrWhiteSpace(request.TransactionNo) ? null : request.TransactionNo.Trim(),
            ProofUrl = request.ProofUrl,
            PaidAt = now,
            Notes = request.Notes
        };

        payment.Verify(currentUserAccessor.UserId);

        // 7. 同步更新账单已收金额/状态（支持分次收款）
        billing.MarkAsPaid(payment.Amount, payment.TransactionNo ?? string.Empty);

        // 8. 持久化变更（同一 DbContext 下单次 SaveChanges 可提交两张表）
        await paymentRepository.AddAsync(payment, cancellationToken);
        await billingRepository.UpdateAsync(billing, cancellationToken);
        await paymentRepository.SaveChangesAsync(cancellationToken);

        // 9. 返回 DTO
        return payment.ToPaymentRecordDto();
    }
}

