using TakeoutSaaS.Domain.Products.Enums;

namespace TakeoutSaaS.Application.App.Products;

/// <summary>
/// 规格做法模板映射辅助。
/// </summary>
internal static class ProductSpecTemplateMapping
{
    /// <summary>
    /// 解析状态字符串。
    /// </summary>
    public static bool TryParseStatus(string? status, out bool isEnabled)
    {
        var normalized = NormalizeStatus(status);
        if (normalized is null)
        {
            isEnabled = true;
            return false;
        }

        isEnabled = normalized == "enabled";
        return true;
    }

    /// <summary>
    /// 状态转字符串。
    /// </summary>
    public static string ToStatusText(bool isEnabled)
    {
        return isEnabled ? "enabled" : "disabled";
    }

    /// <summary>
    /// 解析模板类型。
    /// </summary>
    public static bool TryParseTemplateType(string? templateType, out ProductSpecTemplateType parsed)
    {
        var normalized = templateType?.Trim().ToLowerInvariant();
        switch (normalized)
        {
            case "spec":
                parsed = ProductSpecTemplateType.Spec;
                return true;
            case "method":
                parsed = ProductSpecTemplateType.Method;
                return true;
            default:
                parsed = ProductSpecTemplateType.Spec;
                return false;
        }
    }

    /// <summary>
    /// 模板类型转字符串。
    /// </summary>
    public static string ToTemplateTypeText(ProductSpecTemplateType templateType)
    {
        return templateType switch
        {
            ProductSpecTemplateType.Method => "method",
            ProductSpecTemplateType.Addon => "addon",
            _ => "spec"
        };
    }

    /// <summary>
    /// 解析选择类型。
    /// </summary>
    public static bool TryParseSelectionType(string? selectionType, out AttributeSelectionType parsed)
    {
        var normalized = selectionType?.Trim().ToLowerInvariant();
        switch (normalized)
        {
            case "single":
                parsed = AttributeSelectionType.Single;
                return true;
            case "multi":
                parsed = AttributeSelectionType.Multiple;
                return true;
            default:
                parsed = AttributeSelectionType.Single;
                return false;
        }
    }

    /// <summary>
    /// 选择类型转字符串。
    /// </summary>
    public static string ToSelectionTypeText(AttributeSelectionType selectionType)
    {
        return selectionType == AttributeSelectionType.Multiple ? "multi" : "single";
    }

    private static string? NormalizeStatus(string? status)
    {
        var normalized = status?.Trim().ToLowerInvariant();
        return normalized is "enabled" or "disabled" ? normalized : null;
    }
}
