using System;
using System.Reflection;

namespace Yi.Framework.AiHub.Domain.Shared.Enums;

/// <summary>
/// 价格特性
/// </summary>
[AttributeUsage(AttributeTargets.Field)]
public class PriceAttribute : Attribute
{
    public decimal Price { get; }

    public decimal ReferencePrice { get; }

    public int ValidMonths { get; }

    public PriceAttribute(double price, int validMonths, double referencePrice)
    {
        Price = (decimal)price;
        ValidMonths = validMonths;
        ReferencePrice = (decimal)referencePrice;
    }
}

/// <summary>
/// 显示名称特性
/// </summary>
[AttributeUsage(AttributeTargets.Field)]
public class DisplayNameAttribute : Attribute
{
    public string DisplayName { get; }
    public string ChineseName { get; }
    public string Remark { get; }

    public DisplayNameAttribute(string displayName, string chineseName = "", string remark = "")
    {
        DisplayName = displayName;
        ChineseName = chineseName;
        Remark = remark;
    }
}

/// <summary>
/// 商品类型特性
/// 用于标识商品是VIP服务还是尊享包服务
/// </summary>
[AttributeUsage(AttributeTargets.Field)]
public class GoodsCategoryAttribute : Attribute
{
    public GoodsCategoryType Category { get; }

    public GoodsCategoryAttribute(GoodsCategoryType category)
    {
        Category = category;
    }
}

/// <summary>
/// 商品类别类型
/// </summary>
public enum GoodsCategoryType
{
    /// <summary>
    /// VIP服务
    /// </summary>
    Vip = 1,

    /// <summary>
    /// 尊享包服务
    /// </summary>
    PremiumPackage = 2
}

/// <summary>
/// Token数量特性
/// 用于标识尊享包的token数量
/// </summary>
[AttributeUsage(AttributeTargets.Field)]
public class TokenAmountAttribute : Attribute
{
    public long TokenAmount { get; }

    public TokenAmountAttribute(long tokenAmount)
    {
        TokenAmount = tokenAmount;
    }
}

/// <summary>
/// 商品枚举
/// </summary>
public enum GoodsTypeEnum
{
    // VIP服务
    [Price(29.9, 1, 29.9)] [DisplayName("YiXinVip 1 month", "1个月", "灵活选择")] [GoodsCategory(GoodsCategoryType.Vip)]
    YiXinVip1 = 1,

    [Price(83.7, 3, 27.9)] [DisplayName("YiXinVip 3 month", "3个月", "短期体验")] [GoodsCategory(GoodsCategoryType.Vip)]
    YiXinVip3 = 3,

    [Price(114.5, 5, 22.9)] [DisplayName("YiXinVip 5 month", "5个月", "年度热销")] [GoodsCategory(GoodsCategoryType.Vip)]
    YiXinVip5 = 15,

    // 尊享包服务 - 需要VIP资格才能购买
    [Price(188.9, 0, 1750)]
    [DisplayName("YiXinPremiumPackage 5000W Tokens", "5000万Tokens", "简单尝试")]
    [GoodsCategory(GoodsCategoryType.PremiumPackage)]
    [TokenAmount(50000000)]
    PremiumPackage5000W = 101,

    [Price(248.9, 0, 3500)]
    [DisplayName("YiXinPremiumPackage 10000W Tokens", "1亿Tokens", "极致性价比")]
    [GoodsCategory(GoodsCategoryType.PremiumPackage)]
    [TokenAmount(100000000)]
    PremiumPackage10000W = 102,
    
    //
    // [Price(238.9, 0, 3500)]
    // [DisplayName("YiXinPremiumPackage 10000W Tokens", "1亿Tokens（2026元旦限购）", "活动9.5折特价")]
    // [GoodsCategory(GoodsCategoryType.PremiumPackage)]
    // [TokenAmount(100000000)]
    // PremiumPackage10000W_2026 = 103,
    //
    // [Price(398.9, 0, 7000)]
    // [DisplayName("YiXinPremiumPackage 20000W Tokens", "2亿Tokens（2026元旦限购）", "史上最低8.8折")]
    // [GoodsCategory(GoodsCategoryType.PremiumPackage)]
    // [TokenAmount(200000000)]
    // PremiumPackage20000W_2026 = 104,
}

public static class GoodsTypeEnumExtensions
{
    /// <summary>
    /// 获取商品总金额
    /// </summary>
    /// <param name="goodsType">商品类型</param>
    /// <returns>总金额</returns>
    public static decimal GetTotalAmount(this GoodsTypeEnum goodsType)
    {
        var fieldInfo = goodsType.GetType().GetField(goodsType.ToString());
        var priceAttribute = fieldInfo?.GetCustomAttribute<PriceAttribute>();
        return priceAttribute?.Price ?? 0m;
    }

    /// <summary>
    /// 获取商品价格描述
    /// </summary>
    /// <param name="goodsType">商品类型</param>
    /// <returns>价格描述</returns>
    public static string GetPriceDescription(this GoodsTypeEnum goodsType)
    {
        var price = goodsType.GetTotalAmount();
        return $"¥{price:F1}";
    }

    /// <summary>
    /// 获取商品名称
    /// </summary>
    /// <param name="goodsType">商品类型</param>
    /// <returns>商品名称</returns>
    public static string GetDisplayName(this GoodsTypeEnum goodsType)
    {
        var fieldInfo = goodsType.GetType().GetField(goodsType.ToString());
        var displayNameAttribute = fieldInfo?.GetCustomAttribute<DisplayNameAttribute>();
        return displayNameAttribute?.DisplayName ?? goodsType.ToString();
    }

    /// <summary>
    /// 获取商品有效月份
    /// </summary>
    /// <param name="goodsType">商品类型</param>
    /// <returns>有效月份</returns>
    public static int GetValidMonths(this GoodsTypeEnum goodsType)
    {
        var fieldInfo = goodsType.GetType().GetField(goodsType.ToString());
        var priceAttribute = fieldInfo?.GetCustomAttribute<PriceAttribute>();
        return priceAttribute?.ValidMonths ?? 1;
    }

    /// <summary>
    /// 获取商品月均价格
    /// </summary>
    /// <param name="goodsType">商品类型</param>
    /// <returns>月均价格</returns>
    public static decimal GetMonthlyPrice(this GoodsTypeEnum goodsType)
    {
        var totalPrice = goodsType.GetTotalAmount();
        var validMonths = goodsType.GetValidMonths();
        return validMonths > 0 ? totalPrice / validMonths : 0m;
    }

    /// <summary>
    /// 获取商品参考价格
    /// </summary>
    /// <param name="goodsType">商品类型</param>
    /// <returns>参考价格</returns>
    public static decimal GetReferencePrice(this GoodsTypeEnum goodsType)
    {
        var fieldInfo = goodsType.GetType().GetField(goodsType.ToString());
        var priceAttribute = fieldInfo?.GetCustomAttribute<PriceAttribute>();
        return priceAttribute?.ReferencePrice ?? 0m;
    }

    /// <summary>
    /// 获取商品类别
    /// </summary>
    /// <param name="goodsType">商品类型</param>
    /// <returns>商品类别</returns>
    public static GoodsCategoryType GetGoodsCategory(this GoodsTypeEnum goodsType)
    {
        var fieldInfo = goodsType.GetType().GetField(goodsType.ToString());
        var categoryAttribute = fieldInfo?.GetCustomAttribute<GoodsCategoryAttribute>();
        return categoryAttribute?.Category ?? GoodsCategoryType.Vip;
    }

    /// <summary>
    /// 是否为尊享包商品
    /// </summary>
    /// <param name="goodsType">商品类型</param>
    /// <returns>是否为尊享包</returns>
    public static bool IsPremiumPackage(this GoodsTypeEnum goodsType)
    {
        return goodsType.GetGoodsCategory() == GoodsCategoryType.PremiumPackage;
    }

    /// <summary>
    /// 是否为VIP服务商品
    /// </summary>
    /// <param name="goodsType">商品类型</param>
    /// <returns>是否为VIP服务</returns>
    public static bool IsVipService(this GoodsTypeEnum goodsType)
    {
        return goodsType.GetGoodsCategory() == GoodsCategoryType.Vip;
    }

    /// <summary>
    /// 获取尊享包Token数量
    /// </summary>
    /// <param name="goodsType">商品类型</param>
    /// <returns>Token数量</returns>
    public static long GetTokenAmount(this GoodsTypeEnum goodsType)
    {
        var fieldInfo = goodsType.GetType().GetField(goodsType.ToString());
        var tokenAttribute = fieldInfo?.GetCustomAttribute<TokenAmountAttribute>();
        return tokenAttribute?.TokenAmount ?? 0;
    }

    /// <summary>
    /// 获取商品中文名称
    /// </summary>
    /// <param name="goodsType">商品类型</param>
    /// <returns>中文名称</returns>
    public static string GetChineseName(this GoodsTypeEnum goodsType)
    {
        var fieldInfo = goodsType.GetType().GetField(goodsType.ToString());
        var displayNameAttribute = fieldInfo?.GetCustomAttribute<DisplayNameAttribute>();
        return displayNameAttribute?.ChineseName ?? goodsType.ToString();
    }

    /// <summary>
    /// 获取商品备注
    /// </summary>
    /// <param name="goodsType">商品类型</param>
    /// <returns>备注信息</returns>
    public static string GetRemark(this GoodsTypeEnum goodsType)
    {
        var fieldInfo = goodsType.GetType().GetField(goodsType.ToString());
        var displayNameAttribute = fieldInfo?.GetCustomAttribute<DisplayNameAttribute>();
        return displayNameAttribute?.Remark ?? string.Empty;
    }

    /// <summary>
    /// 计算折扣金额（仅用于尊享包）
    /// 规则：每累加充值10元，减少10元，最多减少50元
    /// </summary>
    /// <param name="goodsType">商品类型</param>
    /// <param name="totalRechargeAmount">用户累加充值金额</param>
    /// <returns>折扣金额</returns>
    public static decimal CalculateDiscount(this GoodsTypeEnum goodsType, decimal totalRechargeAmount)
    {
        // 只有尊享包才有折扣
        if (!goodsType.IsPremiumPackage())
        {
            return 0m;
        }
        // 每满 10 元减 10 元
        var discountTimes = Math.Floor(totalRechargeAmount / 10m);
        var discountAmount = discountTimes * 10m;
        // 最多减少 50 元
        return Math.Min(discountAmount, 50m);
    }

    /// <summary>
    /// 获取折扣后的价格（仅用于尊享包）
    /// </summary>
    /// <param name="goodsType">商品类型</param>
    /// <param name="totalRechargeAmount">用户累加充值金额</param>
    /// <returns>折扣后的价格</returns>
    public static decimal GetDiscountedPrice(this GoodsTypeEnum goodsType, decimal totalRechargeAmount)
    {
        var originalPrice = goodsType.GetTotalAmount();
        var discount = goodsType.CalculateDiscount(totalRechargeAmount);
        var discountedPrice = originalPrice - discount;

        // 确保价格不为负数，至少为0.01元
        return Math.Round(discountedPrice, 2);
    }
}