using MediatR;
using Microsoft.Extensions.Logging;
using TakeoutSaaS.Application.App.Merchants.Commands;
using TakeoutSaaS.Domain.Merchants.Entities;
using TakeoutSaaS.Domain.Merchants.Enums;
using TakeoutSaaS.Domain.Merchants.Repositories;
using TakeoutSaaS.Shared.Abstractions.Constants;
using TakeoutSaaS.Shared.Abstractions.Exceptions;
using TakeoutSaaS.Shared.Abstractions.Security;

namespace TakeoutSaaS.Application.App.Merchants.Handlers;

/// <summary>
/// 冻结商户命令处理器。
/// </summary>
public sealed class FreezeMerchantCommandHandler(
    IMerchantRepository merchantRepository,
    ICurrentUserAccessor currentUserAccessor,
    ILogger<FreezeMerchantCommandHandler> logger)
    : IRequestHandler<FreezeMerchantCommand, bool>
{
    /// <inheritdoc />
    public async Task<bool> Handle(FreezeMerchantCommand request, CancellationToken cancellationToken)
    {
        // 1. 验证 RowVersion
        if (request.RowVersion == 0)
        {
            throw new BusinessException(ErrorCodes.ValidationFailed, "RowVersion 不能为空");
        }

        // 2. 读取商户信息
        var merchant = await merchantRepository.FindByIdAsync(request.MerchantId, cancellationToken);
        if (merchant == null)
        {
            return false;
        }

        // 3. 检查是否已冻结
        if (merchant.IsFrozen)
        {
            throw new BusinessException(ErrorCodes.ValidationFailed, "商户已处于冻结状态");
        }

        // 4. 执行冻结
        var now = DateTime.UtcNow;
        var actorId = currentUserAccessor.UserId == 0 ? (long?)null : currentUserAccessor.UserId;
        var actorName = currentUserAccessor.IsAuthenticated ? $"user:{currentUserAccessor.UserId}" : "system";

        merchant.IsFrozen = true;
        merchant.FrozenReason = request.Reason?.Trim();
        merchant.FrozenAt = now;
        merchant.RowVersion = request.RowVersion;

        // 5. 记录审核日志
        await merchantRepository.AddAuditLogAsync(new MerchantAuditLog
        {
            TenantId = merchant.TenantId,
            MerchantId = merchant.Id,
            Action = MerchantAuditAction.Frozen,
            Title = "商户冻结",
            Description = string.IsNullOrWhiteSpace(request.Reason) ? "管理员冻结商户" : $"冻结原因：{request.Reason.Trim()}",
            OperatorId = actorId,
            OperatorName = actorName
        }, cancellationToken);

        // 6. 持久化
        await merchantRepository.UpdateMerchantAsync(merchant, cancellationToken);

        try
        {
            await merchantRepository.SaveChangesAsync(cancellationToken);
        }
        catch (Exception exception) when (IsConcurrencyException(exception))
        {
            throw new BusinessException(ErrorCodes.Conflict, "商户信息已被修改，请刷新后重试");
        }

        logger.LogInformation("冻结商户 {MerchantId}，原因：{Reason}", merchant.Id, request.Reason);
        return true;
    }

    private static bool IsConcurrencyException(Exception exception)
        => string.Equals(exception.GetType().Name, "DbUpdateConcurrencyException", StringComparison.Ordinal);
}
