/**
 * Carbon Equivalencies - EPA/DEFRA/IMO 2024 Verified Conversion Factors
 *
 * All factors convert metric tons CO2e to equivalents.
 * Sources are authoritative government and international organization data.
 */

/**
 * Carbon equivalency conversion factors
 * All values represent units per metric ton of CO2e
 */
export const CARBON_EQUIVALENCIES = {
  // ===== TRANSPORTATION (EPA 2024) =====
  // EPA Greenhouse Gas Equivalencies Calculator (eGRID 2022)

  /** Miles driven by average passenger car (0.00039 t CO2/mile) */
  MILES_DRIVEN_AVG_CAR: 2560,

  /** Gallons of gasoline consumed (0.008887 t CO2/gallon) */
  GALLONS_GASOLINE: 113,

  // ===== AVIATION (DEFRA 2024 with Radiative Forcing) =====
  // UK DESNZ/DEFRA 2024 GHG Conversion Factors

  /** Passenger-kilometers on economy short-haul flights (0.15 kg CO2e/p-km) */
  FLIGHT_KM_ECONOMY_SHORT: 6667,

  /** Passenger-kilometers on economy long-haul flights (0.13 kg CO2e/p-km) */
  FLIGHT_KM_ECONOMY_LONG: 7692,

  /** Passenger-kilometers on business class flights (0.27 kg CO2e/p-km) */
  FLIGHT_KM_BUSINESS: 3704,

  // ===== ENERGY (EPA 2024, eGRID 2022) =====

  /** Kilowatt-hours of electricity (US grid avg: 3.94e-4 t CO2/kWh) */
  ELECTRICITY_KWH: 2540,

  /** Home electricity use for one year (7.45 t CO2/home/year) */
  HOME_ENERGY_YEARS: 0.134,

  /** Barrels of oil consumed (0.43 t CO2/barrel) */
  BARRELS_OIL: 2.33,

  // ===== LIFESTYLE =====

  /** Smartphone charges (0.022 kWh per charge) */
  SMARTPHONE_CHARGES: 115000,

  /** Pounds of coal burned (0.001 t CO2/lb) */
  POUNDS_COAL_BURNED: 1000,

  // ===== NATURE (EPA 2024) =====

  /** Tree seedlings grown for 10 years (0.060 t CO2/tree over 10 years) */
  TREE_SEEDLINGS_10YR: 16.7,

  /** Acres of US forest preserved for one year (1.00 t CO2/acre/year) */
  FOREST_ACRES_1YR: 1.0,

  /** Acres of US forest carbon sequestration over 10 years (0.76 t CO2/acre/year avg) */
  FOREST_ACRES_10YR: 1.32,

  // ===== YACHT-SPECIFIC (IMO 2024) =====
  // International Maritime Organization MEPC.391(81)

  /** Liters of Marine Gas Oil (MGO) avoided (2.68 kg CO2/L for MGO) */
  MGO_LITERS_AVOIDED: 373,

  /** Metric tonnes of Marine Gas Oil avoided (3.206 t CO2/tonne fuel) */
  MGO_TONNES_AVOIDED: 0.312,

  /** Hours of 60-meter yacht cruising avoided (300 L/h fuel consumption @ 2.68 kg/L) */
  CRUISING_HOURS_60M: 1.24,

  /** Nautical miles of superyacht voyaging avoided (~2.5 L/nm @ 2.68 kg/L) */
  SUPERYACHT_NAUTICAL_MILES: 149,

  /** Days of shore power for large yacht (assuming 100 kWh/day) */
  SHORE_POWER_DAYS: 25.4,
};

/**
 * Source citations for conversion factors
 */
export const EQUIVALENCY_SOURCES = {
  EPA_2024: 'US EPA Greenhouse Gas Equivalencies Calculator (eGRID 2022)',
  DEFRA_2024: 'UK DESNZ/DEFRA 2024 GHG Conversion Factors (with Radiative Forcing)',
  IMO_2024: 'International Maritime Organization MEPC.391(81) - Fourth IMO GHG Study',
};

/**
 * Helper function to calculate equivalency value
 * @param tons - Metric tons of CO2e
 * @param factor - Conversion factor from CARBON_EQUIVALENCIES
 * @returns Calculated equivalency value
 */
export function calculateEquivalency(tons: number, factor: number): number {
  return tons * factor;
}

/**
 * Helper function to format large numbers with appropriate precision
 * @param value - Number to format
 * @returns Formatted string with appropriate decimals and comma separators
 */
export function formatEquivalencyValue(value: number): string {
  if (value < 0.01) {
    return value.toExponential(2);
  } else if (value < 1) {
    return value.toFixed(2);
  } else if (value < 100) {
    return value.toFixed(1);
  } else if (value < 1000) {
    return Math.round(value).toLocaleString();
  } else {
    return Math.round(value).toLocaleString();
  }
}
