import { DecimalUtils } from "../core/DecimalUtils";
import { OrderRules } from "../core/BusinessRules";
import { CostCalculator } from "./CostCalculator";

/**
 * 销售计算模块
 * 负责销售相关金额和价格计算
 */
export class SalesCalculator {
  constructor(
    private order: BusinessAPI.OrderVO,
    private rules: OrderRules,
  ) {}

  /**
   * 计算销售金额
   * 所有供应商的 净重 × 销售单价 之和
   */
  calculateSalesAmount(): number {
    return this.order.orderSupplierList.reduce((total, supplier) => {
      const amount = this.calculateSupplierSalesAmount(supplier);
      return DecimalUtils.add(total, amount);
    }, 0);
  }

  /**
   * 计算采购金额
   * 所有供应商的 净重 × 采购单价 之和
   */
  calculatePurchaseAmount(): number {
    return this.order.orderSupplierList.reduce((total, supplier) => {
      const amount = this.calculateSupplierPurchaseAmount(supplier);
      return DecimalUtils.add(total, amount);
    }, 0);
  }

  /**
   * 计算市场报价
   * = 销售金额 + (包装费，如果配置包含)
   */
  calculateMarketPrice(): number {
    const salesAmount = this.calculateSalesAmount();

    if (this.rules.shouldIncludePackingInMarketPrice()) {
      const costCalculator = new CostCalculator(this.order, this.rules);
      const packagingCosts = costCalculator.calculateTotalCosts();
      return DecimalUtils.add(salesAmount, packagingCosts);
    }

    return salesAmount;
  }

  /**
   * 计算平均采购价（老板看的收购单价）
   */
  calculateAveragePurchasePrice(): number {
    if (!this.order.orderSupplierList.length) {
      return 0;
    }

    const totalPurchasePrice = this.order.orderSupplierList.reduce(
      (total, supplier) => DecimalUtils.add(total, supplier.purchasePrice || 0),
      0,
    );

    return DecimalUtils.divide(
      totalPurchasePrice,
      this.order.orderSupplierList.length,
      0,
    );
  }

  /**
   * 计算平均销售单价（老板看的市场报价）
   */
  calculateAverageSalesPrice(): number {
    const totalSalesPrice = this.calculateSalesAmount();
    return DecimalUtils.divide(totalSalesPrice, this.calculateTotalWeight(), 0);
  }

  /**
   * 计算单斤成本
   * = 基础成本 ÷ 总重量，保留2位小数
   */
  calculateCostPerJin(): string {
    const costCalculator = new CostCalculator(this.order, this.rules);
    const baseCost = costCalculator.calculateBaseCost();
    const totalWeight = this.calculateTotalWeight();

    return DecimalUtils.toFixed(
      DecimalUtils.divide(baseCost, totalWeight, 0),
      2,
    );
  }

  /**
   * 计算单个供应商的销售金额
   */
  calculateSupplierSalesAmount(supplier: BusinessAPI.OrderSupplier): number {
    const weight = this.rules.getWeightByOrderPricingMethod(supplier);
    const salePrice = supplier.salePrice || 0;
    return DecimalUtils.multiply(weight, salePrice);
  }

  /**
   * 计算单个供应商的采购金额
   */
  calculateSupplierPurchaseAmount(supplier: BusinessAPI.OrderSupplier): number {
    const weight = this.rules.getWeightBySupplierPricingMethod(supplier);
    const salePrice = supplier.salePrice || 0;
    return DecimalUtils.multiply(weight, salePrice);
  }

  /**
   * 计算总重量（根据定价方式）
   */
  calculateTotalWeight(): number {
    return this.rules.getPricingMethod() === "BY_GROSS_WEIGHT"
      ? this.calculateTotalGrossWeight()
      : this.calculateTotalNetWeight();
  }

  /**
   * 计算总毛重
   */
  calculateTotalGrossWeight(): number {
    return this.order.orderSupplierList.reduce((total, supplier) => {
      return DecimalUtils.add(total, supplier.grossWeight || 0);
    }, 0);
  }

  /**
   * 计算总净重
   */
  calculateTotalNetWeight(): number {
    return this.order.orderSupplierList.reduce((total, supplier) => {
      return DecimalUtils.add(total, supplier.netWeight || 0);
    }, 0);
  }
}
