import BigNumber from "bignumber.js";

import _ from "lodash";

export interface Goods {
  productId: string | number; // 商品ID（唯一标识商品，用于优惠券/活动匹配，必选）
  skuId: string | number; // 商品规格ID（唯一标识商品规格，如颜色/尺寸）
  id: string | number; // 购物车ID（唯一标识购物车中的条目，如购物车项主键）
  product_id: string | number; // 商品ID（唯一标识商品，用于优惠券/活动匹配，必选）
  salePrice: number; // 商品原价（元）
  number: number; // 商品数量
  product_type: string; // 商品类型
  is_temporary?: number; // 是否临时菜（默认false）
  is_gift?: number; // 是否赠菜（默认false）
  returnNum?: number; // 退货数量（历史订单用，默认0）
  memberPrice: number; // 商品会员价（元，优先级：商品会员价 > 会员折扣）
  discountSaleAmount?: number; // 商家改价后单价（元，优先级最高）
  packFee?: number; // 单份打包费（元，默认0）
  packNumber?: number; // 堂食打包数量（默认0）
  skuData?: {
    // SKU扩展数据（可选）
    id: string | number; // SKU ID（唯一标识商品规格，如颜色/尺寸）
    memberPrice?: number; // SKU会员价
    salePrice?: number; // SKU原价
  };
  discount_sale_amount: number; // 商家改价后单价（元，优先级最高）
  [property: string]: any;
}

export interface User {
  isVip: number; // 是否会员 1是会员
  [property: string]: any;
}

export interface ShopInfo {
  isMemberPrice: number; // 是否开启会员价 1是开启
  [property: string]: any;
}

export interface ThresholdFood {
  id: string | number; // 商品ID
  [property: string]: any;
}
export interface UseFood {
  id: string | number;
  [property: string]: any;
}
export interface Coupon {
  id: string | number;
  use: boolean;
  type: number;
  thresholdFoods: ThresholdFood[];
  useFoods: UseFood[];
  noUseRestrictions?: string;
  discountShare: number; // 是否与折扣优惠同享 1是同享
  vipPriceShare: number; // 是否与会员优惠同享 1是同享
  otherCouponShare: number; // 是否与其他优惠券同享 1是同享
  fullAmount: number; // 使用门槛金额
  discountRate: number; // 折扣率（满减券：折扣金额/门槛金额，折扣券：折扣率）
  maxDiscountAmount: number; // 最大折扣金额（满减券：折扣金额，折扣券：折扣金额）
  discountNum: number; // 抵扣商品数量（商品券：抵扣商品数量，折扣券：0）
  useRule: string; // 使用规则（price_asc：按商品单价升序，price_desc：按商品单价降序）
}

export interface couponDiscount {
  discountPrice: number;
  hasDiscountGoodsArr: Goods[];
}
export interface selCoupon extends Coupon {
  discount?: couponDiscount;
}

export interface couponCalcParams {
  canDikouGoodsArr: Goods[];
  coupon: Coupon;
  user: User;
  shopInfo: ShopInfo;
  selCoupon: selCoupon[];
  goodsOrderPrice: number; //商品订单总价
  isMemberPrice: number; // 是否开启会员价 1是开启
  limitTimeDiscount?: TimeLimitDiscountConfig | null | undefined;
}

//限时折扣配置
export interface TimeLimitDiscountConfig {
  /**
   * 折扣优先级 limit-time/vip-price
   */
  discountPriority: string;
  /**
   * 折扣% 范围1-99
   */
  discountRate: number;
  /**
   * 参与商品
   */
  foods: string;
  /**
   * 参与商品 1全部 2部分
   */
  foodType: number;
  /**
   * 自增主键
   */
  id: number;
  /**
   * 店铺ID
   */
  shopId: number;
  /**
   * 可使用类型：堂食 dine-in  外带 take-out 外卖 take-away 配送 post
   */
  useType: string;
  [property: string]: any;
}

export interface CanDikouGoodsArrArgs {
  canDikouGoodsArr: Goods[];
  selCoupon: selCoupon[];
  user: User;
  shopInfo: ShopInfo;
  limitTimeDiscount?: TimeLimitDiscountConfig | null | undefined;
}

/**
 * 判断商品是否可以使用限时折扣
 * @param goods 商品对象
 * @param limitTimeDiscountRes 限时折扣配置
 * @param shopInfo  店铺信息
 * @param shopUserInfo 店铺用户信息
 * @param idKey 商品ID键名，默认"id"
 * @returns 
 */
export function canUseLimitTimeDiscount(
  goods: Goods,
  limitTimeDiscountRes: TimeLimitDiscountConfig | null | undefined,
  shopInfo: ShopInfo,
  shopUserInfo: User,
  idKey = "id"
) {
  shopInfo = shopInfo || {};
  shopUserInfo = shopUserInfo || {};
  if (!limitTimeDiscountRes || !limitTimeDiscountRes.id) {
    return false;
  }

  const canUseFoods = (limitTimeDiscountRes.foods || "").split(",");

  const goodsCanUse =
    limitTimeDiscountRes.foodType == 1 ||
    canUseFoods.includes(`${goods[idKey]}`);
  if (!goodsCanUse) {
    return false;
  }
  if (limitTimeDiscountRes.discountPriority == "limit-time") {
    return true;
  }
  if (limitTimeDiscountRes.discountPriority == "vip-price") {
    if (goods.id == 727) {
      console.log('goods', goods);
      console.log('shopUserInfo', shopUserInfo);
    }

    if (shopUserInfo.isVip != 1 || shopUserInfo.isMemberPrice != 1) {
      return true;
    }

    if (
      shopUserInfo.isVip == 1 &&
      shopUserInfo.isMemberPrice == 1 &&
      goods.memberPrice * 1 <= 0
    ) {
      return true;
    }
  }

  return false;
}
export interface CanDikouGoodsArrArgs {
  goods: Goods;
  shopInfo: ShopInfo;
  limitTimeDiscountRes: TimeLimitDiscountConfig | null | undefined;
  shopUserInfo: User;
  idKey?: string;
}
/**
 * 返回商品显示价格
 * @params {*} args 参数对象
 * @params {*} args.goods 商品对象
 * @params {*} args.shopInfo 店铺信息
 * @params {*} args.limitTimeDiscountRes 限时折扣信息
 * @params {*} args.shopUserInfo 店铺用户信息
 * @returns
 */
export function returnPrice(args: CanDikouGoodsArrArgs) {
  let {
    goods,
    shopInfo,
    limitTimeDiscountRes,
    shopUserInfo,
    idKey = "product_id",
  } = args;
  limitTimeDiscountRes = limitTimeDiscountRes || {
    foods: "",
    foodType: 2,
    discountPriority: "",
    discountRate: 0,
    id: 0,
    shopId: 0,
    useType: "",
  };
  const canUseFoods = (limitTimeDiscountRes.foods || "").split(",");
  const includesGoods =
    limitTimeDiscountRes.foodType == 1 ||
    canUseFoods.includes("" + goods[idKey]);

  shopInfo = shopInfo || {};
  shopUserInfo = shopUserInfo || {};
  if (
    shopUserInfo.isMemberPrice == 1 &&
    shopUserInfo.isVip == 1 &&
    shopInfo.isMemberPrice == 1
  ) {

    const memberPrice = goods.memberPrice || goods.salePrice;

    //是会员而且启用会员价
    if (limitTimeDiscountRes) {
      //使用限时折扣
      //限时折扣优先
      if (limitTimeDiscountRes.discountPriority == "limit-time") {
        if (includesGoods) {
          return returnLimitPrice({
            price: goods.salePrice,
            limitTimeDiscountRes,
          });
        } else {
          return memberPrice;
        }
      }
      if (
        limitTimeDiscountRes.discountPriority == "vip-price" &&
        includesGoods
      ) {

        if (goods.memberPrice * 1 > 0) {
          //会员优先
          return memberPrice;
        } else {
          const price = returnLimitPrice({
            price: goods.salePrice,
            limitTimeDiscountRes,
            goods: goods,
          });

          return price;
        }
      } else {
        return memberPrice;
      }
    } else {
      //是会员没有限时折扣
      return memberPrice;
    }
  } else {
    // console.log('不是会员或者没有启用会员价',goods,limitTimeDiscountRes);

    //不是会员或者没有启用会员价
    if (limitTimeDiscountRes && limitTimeDiscountRes.id && includesGoods) {
      const price = returnLimitPrice({
        price: goods.salePrice,
        limitTimeDiscountRes,
        goods: goods,
      });
      return price;
    } else {
      return goods.salePrice;
    }
  }
}

interface returnLimitPriceArgs {
  limitTimeDiscountRes: TimeLimitDiscountConfig | null | undefined;
  price: number;
  goods?: Goods;
}
/**
 * 返回限时折扣价格
 * @params {*} args 参数对象
 * @params {*} args.limitTimeDiscountRes 限时折扣信息
 * @params {*} args.price 商品价格
 * @param  {*} args.goods 商品对象
 * @returns
 */
export function returnLimitPrice(args: returnLimitPriceArgs) {
  const { limitTimeDiscountRes, price, goods } = args;
  const discountRate = new BigNumber(
    limitTimeDiscountRes ? limitTimeDiscountRes.discountRate : 100
  ).dividedBy(100);

  const result = BigNumber(price)
    .times(discountRate)
    .decimalPlaces(2, BigNumber.ROUND_UP)
    .toNumber();
  return result;
}

/**
 * 判断是否返回会员价
 * @param {*} args 参数对象
 * @param {*} args.shopInfo 店铺信息
 * @param {*} args.shopUserInfo 店铺用户信息
 * @returns
 */
interface CanReturnMemberPriceArgs {
  shopInfo?: ShopInfo;
  shopUserInfo: User;
}
export function canReturnMemberPrice(args: CanReturnMemberPriceArgs) {
  const { shopInfo, shopUserInfo } = args;
  if (shopUserInfo.isMemberPrice == 1 && shopUserInfo.isVip == 1) {
    return true;
  } else {
    return false;
  }
}

/**
 * 返回会员价格
 * @param {*} goods
 * @returns
 */
export function returnMemberPrice(goods: Goods) {
  return goods.memberPrice || goods.salePrice;
}

export const utils = {
  returnPrice,
  canUseLimitTimeDiscount,
  returnLimitPrice,
  canReturnMemberPrice,
  returnMemberPrice,
};

export default utils;
