using MediatR;
using TakeoutSaaS.Application.App.QuotaPackages.Commands;
using TakeoutSaaS.Domain.Tenants.Repositories;

namespace TakeoutSaaS.Application.App.QuotaPackages.Handlers;

/// <summary>
/// 更新配额包状态命令处理器。
/// </summary>
public sealed class UpdateQuotaPackageStatusCommandHandler(IQuotaPackageRepository quotaPackageRepository)
    : IRequestHandler<UpdateQuotaPackageStatusCommand, bool>
{
    /// <inheritdoc />
    public async Task<bool> Handle(UpdateQuotaPackageStatusCommand request, CancellationToken cancellationToken)
    {
        // 1. 查找配额包
        var quotaPackage = await quotaPackageRepository.FindByIdAsync(request.QuotaPackageId, cancellationToken);

        if (quotaPackage == null)
        {
            return false;
        }

        // 2. 更新状态
        quotaPackage.IsActive = request.IsActive;
        quotaPackage.UpdatedAt = DateTime.UtcNow;

        // 3. 保存到数据库
        await quotaPackageRepository.UpdateAsync(quotaPackage, cancellationToken);
        await quotaPackageRepository.SaveChangesAsync(cancellationToken);

        return true;
    }
}
