using MediatR;
using TakeoutSaaS.Application.App.TenantPackages.Commands;
using TakeoutSaaS.Application.App.TenantPackages.Contracts;
using TakeoutSaaS.Domain.Tenants.Repositories;

namespace TakeoutSaaS.Application.App.TenantPackages.Handlers;

/// <summary>
/// 更新租户套餐命令处理器。
/// </summary>
public sealed class UpdateTenantPackageCommandHandler(ITenantPackageRepository tenantPackageRepository)
    : IRequestHandler<UpdateTenantPackageCommand, TenantPackageListDto?>
{
    /// <inheritdoc />
    public async Task<TenantPackageListDto?> Handle(
        UpdateTenantPackageCommand request,
        CancellationToken cancellationToken)
    {
        // 1. 查询套餐（带跟踪用于更新）
        var package = await tenantPackageRepository.GetByIdForUpdateAsync(request.TenantPackageId, cancellationToken);

        // 2. 如果不存在，返回 null
        if (package is null)
        {
            return null;
        }

        // 3. 更新套餐属性
        package.Name = request.Name.Trim();
        package.Description = request.Description;
        package.PackageType = request.PackageType;
        package.MonthlyPrice = request.MonthlyPrice;
        package.YearlyPrice = request.YearlyPrice;
        package.MaxStoreCount = request.MaxStoreCount;
        package.MaxAccountCount = request.MaxAccountCount;
        package.MaxStorageGb = request.MaxStorageGb;
        package.MaxSmsCredits = request.MaxSmsCredits;
        package.MaxDeliveryOrders = request.MaxDeliveryOrders;
        package.FeaturePoliciesJson = request.FeaturePoliciesJson;
        package.IsActive = request.IsActive;
        package.IsPublicVisible = request.IsPublicVisible;
        package.IsAllowNewTenantPurchase = request.IsAllowNewTenantPurchase;
        package.PublishStatus = request.PublishStatus;
        package.IsRecommended = request.IsRecommended;
        package.Tags = request.Tags;
        package.SortOrder = request.SortOrder;

        // 4. 保存变更
        await tenantPackageRepository.SaveChangesAsync(cancellationToken);

        // 5. 返回 DTO
        return new TenantPackageListDto
        {
            Id = package.Id,
            Name = package.Name,
            Description = package.Description,
            PackageType = package.PackageType,
            MonthlyPrice = package.MonthlyPrice,
            YearlyPrice = package.YearlyPrice,
            MaxStoreCount = package.MaxStoreCount,
            MaxAccountCount = package.MaxAccountCount,
            MaxStorageGb = package.MaxStorageGb,
            MaxSmsCredits = package.MaxSmsCredits,
            MaxDeliveryOrders = package.MaxDeliveryOrders,
            FeaturePoliciesJson = package.FeaturePoliciesJson,
            IsActive = package.IsActive,
            IsPublicVisible = package.IsPublicVisible,
            IsAllowNewTenantPurchase = package.IsAllowNewTenantPurchase,
            PublishStatus = package.PublishStatus,
            IsRecommended = package.IsRecommended,
            Tags = package.Tags,
            SortOrder = package.SortOrder
        };
    }
}
