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

/**
 * 利润计算模块
 * 负责所有利润相关的计算
 */
export class ProfitCalculator {
  private costCalculator: CostCalculator;
  private salesCalculator: SalesCalculator;

  constructor(
    private order: BusinessAPI.OrderVO,
    private rules: OrderRules,
  ) {
    this.costCalculator = new CostCalculator(order, rules);
    this.salesCalculator = new SalesCalculator(order, rules);
  }

  /**
   * 计算西瓜毛利润
   * = 市场报价 - 税费补贴 - 西瓜成本 - 计提税金 - 产地亏损金额
   */
  calculateMelonGrossProfit(): number {
    const marketPrice = this.salesCalculator.calculateMarketPrice();
    const baseCost = this.costCalculator.calculateBaseCost();
    const taxSubsidy = this.costCalculator.calculateTaxSubsidy();
    const taxProvision = this.costCalculator.calculateTaxProvision();
    const lossAmount = this.costCalculator.calculateLossAmount();
    return DecimalUtils.subtract(
      marketPrice,
      taxSubsidy,
      baseCost,
      taxProvision,
      lossAmount,
    );
  }

  /**
   * 计算西瓜净利润
   * = 毛利润 - 个人返点
   */
  calculateMelonNetProfit(): number {
    const grossProfit = this.calculateMelonGrossProfit();
    const personalRebate = this.getPersonalRebate();
    return DecimalUtils.subtract(grossProfit, personalRebate);
  }

  /**
   * 计算个人利润
   * = 净利润 - 利润 + 成本差异
   */
  calculatePersonalProfit(): number {
    const netProfit = this.calculateMelonNetProfit();
    const shareProfit = this.calculateShareProfit();
    const costDifference = this.costCalculator.getCostDifference();

    return DecimalUtils.add(
      DecimalUtils.subtract(netProfit, shareProfit),
      costDifference,
    );
  }

  /**
   * 计算诚信志远利润
   */
  calculateShareProfit(): number {
    if (!this.rules.shouldEnableShareProfit()) {
      return 0;
    }

    const netProfit = this.calculateMelonNetProfit();
    const shareRatio = this.rules.getShareRatio();
    const costDifference = this.costCalculator.getCostDifference();

    // 公式：净利润 × 分成比例 ÷ 100 - 成本差异
    const shareAmount = DecimalUtils.percentage(netProfit, shareRatio);
    return DecimalUtils.subtract(shareAmount, costDifference);
  }

  /**
   * 计算分成后净利润
   * 如果有手动设置的利润则使用，否则计算利润
   */
  calculateNetProfit(): number {
    // 优先使用手动设置的分成后净利润
    const manualNetProfit = this.order.orderDealer?.profitSharing;
    if (manualNetProfit) {
      return manualNetProfit;
    }

    return this.calculateShareProfit();
  }

  /**
   * 计算诚信志远看到的利润
   * = 市场报价 - 成本合计（是否含运费）
   */
  calculateShareProfitView(): number {
    let marketPrice = this.salesCalculator.calculateMarketPrice();

    // 判断采购成本是否包含运费
    if (this.rules.shouldIncludeFreightCost()) {
      const totalPurchaseCost =
        this.costCalculator.calculateTotalPurchaseCost(true);
      marketPrice = DecimalUtils.subtract(marketPrice, totalPurchaseCost);
    } else {
      // 计算采购成本
      const totalPurchaseCost =
        this.costCalculator.calculateTotalPurchaseCost();
      marketPrice = DecimalUtils.subtract(marketPrice, totalPurchaseCost);
    }

    const costDifference = this.costCalculator.getCostDifference();
    return DecimalUtils.subtract(marketPrice, costDifference);
  }

  /**
   * 获取个人返点金额
   */
  getPersonalRebate(): number {
    return this.order.orderRebate?.amount || 0;
  }
}
