import type { EquityResult } from '../types';

export interface EquityInput {
  incomeA: number;
  incomeB: number;
  commonCharges: number;
  domesticHoursA: number;
  domesticHoursB: number;
  domesticHourlyRate: number;
}

const DEFAULT_HOURLY_RATE = 18; // CAD/hour — Quebec median

/**
 * Calculates the "Quotient Équitable" — a fair contribution ratio
 * based on income AND domestic work.
 *
 * Philosophy: no "verdict", only a "suggested distribution" with explanation.
 *
 * Steps:
 * 1. Calculate raw income ratio
 * 2. Monetize domestic work
 * 3. Compute "adjusted income" = monetary income - domestic contribution value
 * 4. Derive adjusted contribution ratio
 */
export function calculateEquity(input: EquityInput): EquityResult {
  const {
    incomeA,
    incomeB,
    commonCharges,
    domesticHoursA,
    domesticHoursB,
    domesticHourlyRate = DEFAULT_HOURLY_RATE,
  } = input;

  const totalIncome = incomeA + incomeB;

  // Edge case: no income
  if (totalIncome === 0) {
    return {
      ratioA: 50,
      ratioB: 50,
      adjustedRatioA: 50,
      adjustedRatioB: 50,
      domesticValueA: 0,
      domesticValueB: 0,
      explanation: 'equal_no_income',
    };
  }

  // Step 1: Raw income-based ratio
  const rawRatioA = (incomeA / totalIncome) * 100;
  const rawRatioB = (incomeB / totalIncome) * 100;

  // Step 2: Monetize domestic work (monthly value)
  const domesticValueA = domesticHoursA * domesticHourlyRate * 4.33; // weeks/month
  const domesticValueB = domesticHoursB * domesticHourlyRate * 4.33;

  // Step 3: Adjusted income = income - domestic contribution credit
  // The idea: if you do more unpaid domestic work, your financial burden should be lighter
  const adjustedIncomeA = Math.max(0, incomeA - domesticValueA);
  const adjustedIncomeB = Math.max(0, incomeB - domesticValueB);
  const adjustedTotal = adjustedIncomeA + adjustedIncomeB;

  let adjustedRatioA: number;
  let adjustedRatioB: number;

  if (adjustedTotal === 0) {
    adjustedRatioA = 50;
    adjustedRatioB = 50;
  } else {
    adjustedRatioA = (adjustedIncomeA / adjustedTotal) * 100;
    adjustedRatioB = (adjustedIncomeB / adjustedTotal) * 100;
  }

  // Round to 1 decimal
  const roundedAdjA = Math.round(adjustedRatioA * 10) / 10;
  const roundedAdjB = Math.round(adjustedRatioB * 10) / 10;

  // Determine explanation key
  let explanation: string;
  const diff = Math.abs(roundedAdjA - roundedAdjB);

  if (diff < 5) {
    explanation = 'near_equal';
  } else if (roundedAdjA > roundedAdjB) {
    explanation = 'a_contributes_more';
  } else {
    explanation = 'b_contributes_more';
  }

  return {
    ratioA: Math.round(rawRatioA * 10) / 10,
    ratioB: Math.round(rawRatioB * 10) / 10,
    adjustedRatioA: roundedAdjA,
    adjustedRatioB: roundedAdjB,
    domesticValueA: Math.round(domesticValueA),
    domesticValueB: Math.round(domesticValueB),
    explanation,
  };
}

/**
 * Formats ratio as a readable string: "60 / 40"
 */
export function formatRatio(ratioA: number, ratioB: number): string {
  return `${Math.round(ratioA)} / ${Math.round(ratioB)}`;
}

/**
 * Gets the contribution amounts based on the ratio
 */
export function getContributions(
  commonCharges: number,
  ratioA: number,
  ratioB: number
): { contributionA: number; contributionB: number } {
  const total = ratioA + ratioB;
  return {
    contributionA: Math.round((commonCharges * ratioA) / total),
    contributionB: Math.round((commonCharges * ratioB) / total),
  };
}
