/**
 * WalletPass 评估器使用示例
 */

import { WalletPassEvaluator } from "./evaluator";

// ============================================
// 准备测试数据
// ============================================

// 1. 商品列表
var products = [{
  product_id: 60734,
  price: 80,
  quantity: 1,
  name: '商品A'
}, {
  product_id: 60735,
  price: 50,
  quantity: 2,
  name: '商品B'
}];

// 订单总金额 = 80 + 50*2 = 180

// 2. Voucher 列表
var vouchers = [{
  id: 'VOUCHER_001',
  amount: 10,
  balance: 10,
  type: 'cash_voucher'
}, {
  id: 'VOUCHER_002',
  amount: 20,
  balance: 20,
  type: 'cash_voucher'
}, {
  id: 'VOUCHER_003',
  amount: 50,
  balance: 50,
  type: 'cash_voucher'
}];

// 3. 策略配置
var strategyConfigs = [
// 策略1: 满100减10，适用于 VOUCHER_001
{
  metadata: {
    id: 'STRATEGY_001',
    name: '满100减10',
    type: 'wallet_pass',
    custom: {
      maxDeductionAmount: 10,
      // 最大抵扣10元
      maxUsagePerOrder: 1,
      // 每单最多用1张
      deductTaxAndFee: true,
      // 抵扣税费
      applicableProductIds: [60734, 60735] // 适用于这两个商品
    }
  },
  conditions: {
    operator: 'and',
    rules: [
    // 匹配 voucher ID
    {
      type: 'operator',
      field: 'voucherId',
      operator: 'in',
      value: ['VOUCHER_001']
    },
    // 适用商品总额需 >= 100
    {
      type: 'operator',
      field: 'applicableProductTotal',
      operator: '>=',
      value: 100
    }],
    actionIds: ['deduct_10']
  },
  actions: [{
    id: 'deduct_10',
    type: 'DEDUCT_AMOUNT',
    value: 10,
    target: 'applicable_products',
    priority: 1
  }]
},
// 策略2: 满150减20，适用于 VOUCHER_002
{
  metadata: {
    id: 'STRATEGY_002',
    name: '满150减20',
    type: 'wallet_pass',
    custom: {
      maxDeductionAmount: 20,
      maxUsagePerOrder: 1,
      deductTaxAndFee: true,
      applicableProductIds: [60734, 60735]
    }
  },
  conditions: {
    operator: 'and',
    rules: [{
      type: 'operator',
      field: 'voucherId',
      operator: 'in',
      value: ['VOUCHER_002']
    }, {
      type: 'operator',
      field: 'applicableProductTotal',
      operator: '>=',
      value: 150
    }],
    actionIds: ['deduct_20']
  },
  actions: [{
    id: 'deduct_20',
    type: 'DEDUCT_AMOUNT',
    value: 20,
    target: 'applicable_products',
    priority: 1
  }]
},
// 策略3: 满200减50，适用于 VOUCHER_003
{
  metadata: {
    id: 'STRATEGY_003',
    name: '满200减50',
    type: 'wallet_pass',
    custom: {
      maxDeductionAmount: 50,
      maxUsagePerOrder: 2,
      deductTaxAndFee: false,
      applicableProductIds: [60734, 60735]
    }
  },
  conditions: {
    operator: 'and',
    rules: [{
      type: 'operator',
      field: 'voucherId',
      operator: 'in',
      value: ['VOUCHER_003']
    }, {
      type: 'operator',
      field: 'applicableProductTotal',
      operator: '>=',
      value: 200
    }],
    actionIds: ['deduct_50']
  },
  actions: [{
    id: 'deduct_50',
    type: 'DEDUCT_AMOUNT',
    value: 50,
    target: 'applicable_products',
    priority: 1
  }]
}];

// ============================================
// 执行评估
// ============================================

function runExample() {
  console.log('========== WalletPass 评估器示例 ==========\n');

  // 创建评估器实例
  var evaluator = new WalletPassEvaluator();

  // 执行评估
  var results = evaluator.evaluate({
    orderTotalAmount: 180,
    products: products,
    vouchers: vouchers,
    strategyConfigs: strategyConfigs
  });

  // 输出结果
  console.log('订单信息：');
  console.log("- \u603B\u91D1\u989D: ".concat(180, " \u5143"));
  console.log("- \u5546\u54C1: ".concat(products.map(function (p) {
    return "".concat(p.name, "(").concat(p.quantity, "\u4EF6)");
  }).join(', '), "\n"));
  console.log('评估结果：\n');
  results.forEach(function (result, index) {
    console.log("Voucher ".concat(index + 1, ": ").concat(result.voucher.id));
    console.log("  \u9762\u989D: ".concat(result.voucher.amount, " \u5143"));
    console.log("  \u53EF\u7528: ".concat(result.isApplicable ? '✅ 是' : '❌ 否'));
    if (result.isApplicable) {
      console.log("  \u6700\u5927\u62B5\u6263: ".concat(result.maxDeduction, " \u5143"));
      console.log("  \u6BCF\u5355\u53EF\u7528: ".concat(result.canUseCount, " \u5F20"));
      console.log("  \u62B5\u6263\u7A0E\u8D39: ".concat(result.deductTaxAndFee ? '是' : '否'));
      console.log("  \u9002\u7528\u5546\u54C1: ".concat(result.applicableProductIds.join(', ')));
    } else {
      console.log("  \u539F\u56E0: ".concat(result.reason));
      console.log("  \u539F\u56E0\u7801: ".concat(result.reasonCode));
    }
    console.log('');
  });

  // 筛选可用的 vouchers
  var availableVouchers = results.filter(function (r) {
    return r.isApplicable;
  });
  console.log("\n\u5171 ".concat(availableVouchers.length, " \u5F20\u53EF\u7528"));
  if (availableVouchers.length > 0) {
    console.log('可用 voucher ID:');
    availableVouchers.forEach(function (v) {
      console.log("  - ".concat(v.voucher.id, ": \u6700\u591A\u62B5\u6263 ").concat(v.maxDeduction, " \u5143"));
    });
  }
}

// 运行示例
runExample();

// ============================================
// 预期输出
// ============================================
/*
========== WalletPass 评估器示例 ==========

订单信息：
- 总金额: 180 元
- 商品: 商品A(1件), 商品B(2件)

评估结果：

Voucher 1: VOUCHER_001
  面额: 10 元
  可用: ✅ 是
  最大抵扣: 10 元
  每单可用: 1 张
  抵扣税费: 是
  适用商品: 60734, 60735

Voucher 2: VOUCHER_002
  面额: 20 元
  可用: ✅ 是
  最大抵扣: 20 元
  每单可用: 1 张
  抵扣税费: 是
  适用商品: 60734, 60735

Voucher 3: VOUCHER_003
  面额: 50 元
  可用: ❌ 否
  原因: Conditions not met
  原因码: CONDITION_NOT_MET


共 2 张可用
可用 voucher ID:
  - VOUCHER_001: 最多抵扣 10 元
  - VOUCHER_002: 最多抵扣 20 元
*/