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

namespace TakeoutSaaS.Application.App.TenantPackages.Handlers;

/// <summary>
/// 获取租户套餐详情查询处理器。
/// </summary>
public sealed class GetTenantPackageDetailQueryHandler(ITenantPackageRepository tenantPackageRepository)
    : IRequestHandler<GetTenantPackageDetailQuery, TenantPackageListDto?>
{
    /// <inheritdoc />
    public async Task<TenantPackageListDto?> Handle(
        GetTenantPackageDetailQuery request,
        CancellationToken cancellationToken)
    {
        // 1. 查询套餐详情
        var package = await tenantPackageRepository.GetByIdAsync(request.TenantPackageId, cancellationToken);

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

        // 3. 映射为 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
        };
    }
}
