// ============================================================================
// Types partagés — Simulateur d'exonération d'accise sur l'électricité
// ============================================================================

/** Catégorie de secteur affichée comme tuile dans l'étape 1 */
export interface SectorCategory {
  id: string;
  label: string;
  icon: string; // nom d'icône Tabler (ex: "IconFlame")
  description: string;
  nafPrefixes: string[]; // préfixes NAF pour pré-filtrer
  popular?: boolean;
}

/** Code NAF/APE individuel */
export interface NafCode {
  code: string; // ex: "24.10Z"
  label: string;
  sectorCategoryId: string;
  searchTerms?: string[]; // termes supplémentaires pour Fuse.js
}

/** Condition d'éligibilité à un tier d'exonération */
export interface ExemptionCondition {
  type:
    | "naf_match"
    | "ratio_va"
    | "consumption_min"
    | "iso_50001"
    | "carbon_leakage"
    | "power_threshold"
    | "electro_intensity"
    | "electricity_cost_ratio";
  description: string;
  nafPrefixes?: string[];
  minRatioVA?: number; // pourcentage
  minConsumptionGWh?: number;
  requiresISO50001?: boolean;
  requiresCarbonLeakage?: boolean;
  maxPowerKW?: number;
  minElectroIntensity?: number; // pourcentage
  minElectricityCostRatio?: number; // pourcentage coût élec / valeur production (L.312-68)
}

/** Code d'exonération — codification officielle 2040-TIC-VA-E (impots.gouv.fr 2026) */
export type ExemptionCode =
  | "E01"
  | "E02"
  | "E03"
  | "E04"
  | "E05"
  | "E06"
  | "E07"
  | "E08"
  | "E09"
  | "E10"
  | "E11"
  | "E12"
  | "E13"
  | "E14"
  | "E14b" // Ultra électro-intensive (≥ 13,5 %) sans condition fuite carbone
  | "E15"
  | "E16"
  | "E17"
  | "E18"
  | "E19"
  | "E20"
  | "E21"
  | "E22"
  | "E23"
  | "E24"
  | "E25"
  | "E26" // Production très intensive (coût élec > 50 % coût produit)
  | "E27" // IRVE publique
  | "E28" // H2 bas-carbone
  | "E29" // Agriculture serriste
  | "E30"
  | "E31"; // Pêche / aquaculture

/** Tier d'exonération */
export interface ExemptionTier {
  code: ExemptionCode;
  label: string;
  shortLabel: string;
  rate: number; // €/MWh (0 = exonération totale)
  savingsPercent: number; // vs taux normal (indicatif, le calcul réel est dynamique)
  isFullExemption: boolean;
  conditions: ExemptionCondition[];
  legalReference: string;
}

/** Mapping NAF → exonérations potentielles */
export interface NafEligibility {
  nafPrefix: string;
  potentialExemptions: ExemptionCode[];
  autoQualify?: boolean; // NAF seul suffit
}

// ============================================================================
// Types du moteur de calcul
// ============================================================================

/** Fourchettes officielles d'électro-intensité 2026 (seuils CIBS) */
export type RatioVARange =
  | "<0.5%"
  | "0.5-2.25%"
  | "2.25-3.375%"
  | "3.375-6.75%"
  | "6.75-13.5%"
  | ">13.5%";

export interface SimulateurInput {
  nafCode: string;
  annualConsumptionMWh: number;
  currentAnnualCostEuros?: number;
  electricityCostEuros?: number; // optionnel — utilisé pour E26 uniquement
  valueAddedEuros?: number; // valeur ajoutée comptable
  productionValueEuros?: number; // valeur de la production (pour E26, L.312-68)
  hasISO50001: boolean;
  isOnCarbonLeakageList: boolean | null; // null = ne sait pas
  installedPowerKW?: number;
  ratioVARange?: RatioVARange; // mode simplifié
}

export type Confidence = "confirmed" | "likely" | "possible";

export interface ApplicableTier {
  tier: ExemptionTier;
  confidence: Confidence;
  missingConditions: string[];
}

export interface SimulateurResult {
  isEligible: boolean;
  applicableTiers: ApplicableTier[];
  bestTier: ApplicableTier | null;
  currentAnnualTax: number;
  optimizedAnnualTax: number;
  annualSavings: number;
  savingsPercent: number;
  twoYearRetroactiveSavings: number;
  /** Fourchette d'économies (min–max) pour le teaser */
  savingsRange: { min: number; max: number };
}

// ============================================================================
// Types du wizard
// ============================================================================

export type WizardStep = 1 | 2 | 3 | 4 | 5;

export interface WizardState {
  currentStep: WizardStep;
  // Étape 1
  selectedSectorCategory: string | null;
  selectedNafCode: string | null;
  // Étape 2
  annualConsumptionMWh: number | null;
  currentAnnualCostEuros: number | null;
  // Étape 3
  electricityCostEuros: number | null;
  valueAddedEuros: number | null;
  productionValueEuros: number | null;
  ratioVARange: RatioVARange | null;
  hasISO50001: boolean;
  isOnCarbonLeakageList: boolean | null;
  installedPowerKW: number | null;
  // Étapes 4-5
  result: SimulateurResult | null;
  leadSubmitted: boolean;
}

export type WizardAction =
  | { type: "SELECT_SECTOR"; categoryId: string }
  | { type: "SELECT_NAF"; code: string }
  | { type: "SET_CONSUMPTION"; mwh: number; cost?: number }
  | { type: "SET_QUALIFYING"; data: Partial<WizardState> }
  | { type: "CALCULATE"; result: SimulateurResult }
  | { type: "LEAD_SUBMITTED" }
  | { type: "GO_TO_STEP"; step: WizardStep }
  | { type: "GO_BACK" };
