using MediatR;
using TakeoutSaaS.Application.App.TenantPackages.Commands;
using TakeoutSaaS.Application.App.TenantPackages.Contracts;
using TakeoutSaaS.Domain.Tenants.Entities;
using TakeoutSaaS.Domain.Tenants.Repositories;
using TakeoutSaaS.Shared.Abstractions.Constants;
using TakeoutSaaS.Shared.Abstractions.Exceptions;

namespace TakeoutSaaS.Application.App.TenantPackages.Handlers;

/// <summary>
/// 创建租户套餐命令处理器。
/// </summary>
public sealed class CreateTenantPackageCommandHandler(ITenantPackageRepository tenantPackageRepository)
    : IRequestHandler<CreateTenantPackageCommand, TenantPackageListDto>
{
    /// <inheritdoc />
    public async Task<TenantPackageListDto> Handle(
        CreateTenantPackageCommand request,
        CancellationToken cancellationToken)
    {
        // 1. 校验必填字段
        if (string.IsNullOrWhiteSpace(request.Name))
        {
            throw new BusinessException(ErrorCodes.BadRequest, "套餐名称不能为空");
        }

        // 2. 构建套餐实体
        var package = new TenantPackage
        {
            Name = request.Name.Trim(),
            Description = request.Description,
            PackageType = request.PackageType,
            MonthlyPrice = request.MonthlyPrice,
            YearlyPrice = request.YearlyPrice,
            MaxStoreCount = request.MaxStoreCount,
            MaxAccountCount = request.MaxAccountCount,
            MaxStorageGb = request.MaxStorageGb,
            MaxSmsCredits = request.MaxSmsCredits,
            MaxDeliveryOrders = request.MaxDeliveryOrders,
            FeaturePoliciesJson = request.FeaturePoliciesJson,
            IsActive = request.IsActive,
            IsPublicVisible = request.IsPublicVisible,
            IsAllowNewTenantPurchase = request.IsAllowNewTenantPurchase,
            PublishStatus = request.PublishStatus,
            IsRecommended = request.IsRecommended,
            Tags = request.Tags,
            SortOrder = request.SortOrder
        };

        // 3. 持久化
        await tenantPackageRepository.AddAsync(package, cancellationToken);
        await tenantPackageRepository.SaveChangesAsync(cancellationToken);

        // 4. 返回 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
        };
    }
}
