using TakeoutSaaS.Application.App.Products.Dto;
using TakeoutSaaS.Domain.Products.Entities;

namespace TakeoutSaaS.Application.App.Products;

/// <summary>
/// 商品映射辅助。
/// </summary>
public static class ProductMapping
{
    /// <summary>
    /// 映射 SPU DTO。
    /// </summary>
    /// <param name="product">商品实体。</param>
    /// <returns>DTO。</returns>
    public static ProductDto ToDto(Product product) => new()
    {
        Id = product.Id,
        TenantId = product.TenantId,
        StoreId = product.StoreId,
        CategoryId = product.CategoryId,
        SpuCode = product.SpuCode,
        Name = product.Name,
        Subtitle = product.Subtitle,
        Unit = product.Unit,
        Price = product.Price,
        OriginalPrice = product.OriginalPrice,
        StockQuantity = product.StockQuantity,
        MaxQuantityPerOrder = product.MaxQuantityPerOrder,
        Status = product.Status,
        Kind = product.Kind,
        SalesMonthly = product.SalesMonthly,
        TagsJson = product.TagsJson,
        SoldoutMode = product.SoldoutMode,
        RecoverAt = product.RecoverAt,
        RemainStock = product.RemainStock,
        SoldoutReason = product.SoldoutReason,
        SyncToPlatform = product.SyncToPlatform,
        NotifyManager = product.NotifyManager,
        TimedOnShelfAt = product.TimedOnShelfAt,
        CoverImage = product.CoverImage,
        GalleryImages = product.GalleryImages,
        Description = product.Description,
        SortWeight = product.SortWeight,
        WarningStock = product.WarningStock,
        PackingFee = product.PackingFee,
        EnableDineIn = product.EnableDineIn,
        EnablePickup = product.EnablePickup,
        EnableDelivery = product.EnableDelivery,
        IsFeatured = product.IsFeatured,
        CreatedAt = product.CreatedAt
    };

    /// <summary>
    /// 映射 SKU DTO。
    /// </summary>
    public static ProductSkuDto ToDto(ProductSku sku) => new()
    {
        Id = sku.Id,
        ProductId = sku.ProductId,
        SkuCode = sku.SkuCode,
        Barcode = sku.Barcode,
        Price = sku.Price,
        OriginalPrice = sku.OriginalPrice,
        StockQuantity = sku.StockQuantity,
        Weight = sku.Weight,
        AttributesJson = sku.AttributesJson,
        SortOrder = sku.SortOrder,
        IsEnabled = sku.IsEnabled
    };

    /// <summary>
    /// 映射规格组 DTO。
    /// </summary>
    public static ProductAttributeGroupDto ToDto(ProductAttributeGroup group, IReadOnlyList<ProductAttributeOption> options) => new()
    {
        Id = group.Id,
        ProductId = group.ProductId,
        Name = group.Name,
        SelectionType = (int)group.SelectionType,
        SortOrder = group.SortOrder,
        Options = options.Select(ToDto).ToList()
    };

    /// <summary>
    /// 映射规格选项 DTO。
    /// </summary>
    public static ProductAttributeOptionDto ToDto(ProductAttributeOption option) => new()
    {
        Id = option.Id,
        AttributeGroupId = option.AttributeGroupId,
        Name = option.Name,
        SortOrder = option.SortOrder
    };

    /// <summary>
    /// 映射加料组 DTO。
    /// </summary>
    public static ProductAddonGroupDto ToDto(ProductAddonGroup group, IReadOnlyList<ProductAddonOption> options) => new()
    {
        Id = group.Id,
        ProductId = group.ProductId,
        Name = group.Name,
        MinSelect = group.MinSelect ?? 0,
        MaxSelect = group.MaxSelect ?? 0,
        SortOrder = group.SortOrder,
        Options = options.Select(ToDto).ToList()
    };

    /// <summary>
    /// 映射加料选项 DTO。
    /// </summary>
    public static ProductAddonOptionDto ToDto(ProductAddonOption option) => new()
    {
        Id = option.Id,
        AddonGroupId = option.AddonGroupId,
        Name = option.Name,
        ExtraPrice = option.ExtraPrice,
        SortOrder = option.SortOrder
    };

    /// <summary>
    /// 映射媒资 DTO。
    /// </summary>
    public static ProductMediaAssetDto ToDto(ProductMediaAsset asset) => new()
    {
        Id = asset.Id,
        ProductId = asset.ProductId,
        MediaType = asset.MediaType,
        Url = asset.Url,
        Caption = asset.Caption,
        SortOrder = asset.SortOrder
    };

    /// <summary>
    /// 映射价格策略 DTO。
    /// </summary>
    public static ProductPricingRuleDto ToDto(ProductPricingRule rule) => new()
    {
        Id = rule.Id,
        ProductId = rule.ProductId,
        RuleType = rule.RuleType,
        Price = rule.Price,
        ConditionsJson = rule.ConditionsJson,
        WeekdaysJson = rule.WeekdaysJson
    };
}
