package com.erwan.revwatch.model

enum class VehicleBrand(val key: String, val displayName: String) {
    ALFA_ROMEO("alfa_romeo", "Alfa Romeo"),
    AUDI("audi", "Audi"),
    BMW("bmw", "BMW"),
    CHEVROLET("chevrolet", "Chevrolet"),
    CITROEN("citroen", "Citroen"),
    DACIA("dacia", "Dacia"),
    FIAT("fiat", "Fiat"),
    FORD("ford", "Ford"),
    HONDA("honda", "Honda"),
    HYUNDAI("hyundai", "Hyundai"),
    JAGUAR("jaguar", "Jaguar"),
    KIA("kia", "Kia"),
    LEXUS("lexus", "Lexus"),
    MAZDA("mazda", "Mazda"),
    MERCEDES("mercedes", "Mercedes"),
    MINI("mini", "Mini"),
    MITSUBISHI("mitsubishi", "Mitsubishi"),
    NISSAN("nissan", "Nissan"),
    OPEL("opel", "Opel"),
    PEUGEOT("peugeot", "Peugeot"),
    PORSCHE("porsche", "Porsche"),
    RENAULT("renault", "Renault"),
    SEAT("seat", "Seat"),
    SKODA("skoda", "Skoda"),
    SUBARU("subaru", "Subaru"),
    SUZUKI("suzuki", "Suzuki"),
    TOYOTA("toyota", "Toyota"),
    VOLKSWAGEN("volkswagen", "Volkswagen"),
    VOLVO("volvo", "Volvo");

    companion object {
        fun fromKey(key: String?): VehicleBrand? = entries.firstOrNull { it.key == key }
    }
}

/**
 * PID capabilities vary by vehicle.
 * Tous les profils utilisent actuellement Mode 01 standard OBD-II (SAE J1979).
 * Les PIDs Mode 22 Ford pour temp huile/boost ne sont pas documentés publiquement ;
 * ils seront ajoutés par profil une fois vérifiés sur véhicule réel.
 * Les Honda OBD-1 (avant 1996) utilisent un protocole propriétaire série — non supportés via ELM327.
 */
enum class VehicleProfile(
    val key: String,
    val displayName: String,
    val brand: VehicleBrand,
    val pidSet: PidSet
) {
    // ──────── Ford — EcoBoost (Mode 22 support) ────────

    FIESTA_ST_MK7(
        "fiesta_st_mk7", "Fiesta ST Mk7 — 1.6 EcoBoost (2013–2017)",
        VehicleBrand.FORD, PidSet.FORD_ECOBOOST
    ),
    FIESTA_ST_MK8(
        "fiesta_st_mk8", "Fiesta ST Mk8 — 1.5 EcoBoost 3cyl (2018–2023)",
        VehicleBrand.FORD, PidSet.FORD_ECOBOOST
    ),
    FOCUS_ST_MK3(
        "focus_st_mk3", "Focus ST Mk3 — 2.0 EcoBoost (2012–2018)",
        VehicleBrand.FORD, PidSet.FORD_ECOBOOST
    ),
    FOCUS_ST_MK4(
        "focus_st_mk4", "Focus ST Mk4 — 2.3 EcoBoost (2019+)",
        VehicleBrand.FORD, PidSet.FORD_ECOBOOST
    ),
    FOCUS_RS_MK3(
        "focus_rs_mk3", "Focus RS Mk3 — 2.3 EcoBoost AWD (2016–2018)",
        VehicleBrand.FORD, PidSet.FORD_ECOBOOST_RS
    ),
    PUMA_ST(
        "puma_st", "Puma ST — 1.5 EcoBoost 3cyl (2020+)",
        VehicleBrand.FORD, PidSet.FORD_ECOBOOST
    ),
    MUSTANG_ECOBOOST(
        "mustang_ecoboost", "Mustang EcoBoost — 2.3 (2015+)",
        VehicleBrand.FORD, PidSet.FORD_ECOBOOST
    ),

    // ──────── Ford — Volvo 2.5 I5 (limited Mode 22) ────────

    FOCUS_ST_MK2(
        "focus_st_mk2", "Focus ST Mk2 — 2.5 Duratec ST (2005–2010)",
        VehicleBrand.FORD, PidSet.FORD_DURATEC_25
    ),
    FOCUS_RS_MK2(
        "focus_rs_mk2", "Focus RS Mk2 — 2.5 Duratec RS (2009–2011)",
        VehicleBrand.FORD, PidSet.FORD_DURATEC_25
    ),

    // ──────── Ford — Duratec NA (Mode 01 only) ────────

    FIESTA_ST_MK6(
        "fiesta_st_mk6", "Fiesta ST Mk6 — 2.0 Duratec (2004–2008)",
        VehicleBrand.FORD, PidSet.OBD2_STANDARD
    ),

    // ──────── Honda — OBD-2 (Mode 01 only) ────────

    CRX_DELSOL_D16(
        "crx_delsol_d16", "CRX Del Sol — 1.6 D16 VTEC (1996–1997)",
        VehicleBrand.HONDA, PidSet.OBD2_STANDARD_NA
    ),
    CRX_DELSOL_B16(
        "crx_delsol_b16", "CRX Del Sol VTI — 1.6 B16A (1996–1997)",
        VehicleBrand.HONDA, PidSet.OBD2_STANDARD_NA
    ),
    CIVIC_EJ9_D14(
        "civic_ej9_d14", "Civic EJ9 — 1.4 D14 (1996–2000)",
        VehicleBrand.HONDA, PidSet.OBD2_STANDARD_NA
    ),
    CIVIC_EJ9_D15(
        "civic_ej9_d15", "Civic EJ9 — 1.5 D15 (1996–2000)",
        VehicleBrand.HONDA, PidSet.OBD2_STANDARD_NA
    ),

    // ──────── Generiques — OBD-II standard ────────

    ALFA_ROMEO_TURBO(
        "alfa_romeo_turbo", "Alfa Romeo — Turbo (OBD-II standard)",
        VehicleBrand.ALFA_ROMEO, PidSet.OBD2_STANDARD
    ),
    ALFA_ROMEO_NA(
        "alfa_romeo_na", "Alfa Romeo — Atmospherique (OBD-II standard)",
        VehicleBrand.ALFA_ROMEO, PidSet.OBD2_STANDARD_NA
    ),
    AUDI_TURBO(
        "audi_turbo", "Audi — Turbo (OBD-II standard)",
        VehicleBrand.AUDI, PidSet.OBD2_STANDARD
    ),
    AUDI_NA(
        "audi_na", "Audi — Atmospherique (OBD-II standard)",
        VehicleBrand.AUDI, PidSet.OBD2_STANDARD_NA
    ),
    BMW_TURBO(
        "bmw_turbo", "BMW — Turbo (OBD-II standard)",
        VehicleBrand.BMW, PidSet.OBD2_STANDARD
    ),
    BMW_NA(
        "bmw_na", "BMW — Atmospherique (OBD-II standard)",
        VehicleBrand.BMW, PidSet.OBD2_STANDARD_NA
    ),
    CHEVROLET_TURBO(
        "chevrolet_turbo", "Chevrolet — Turbo (OBD-II standard)",
        VehicleBrand.CHEVROLET, PidSet.OBD2_STANDARD
    ),
    CHEVROLET_NA(
        "chevrolet_na", "Chevrolet — Atmospherique (OBD-II standard)",
        VehicleBrand.CHEVROLET, PidSet.OBD2_STANDARD_NA
    ),
    CITROEN_TURBO(
        "citroen_turbo", "Citroen — Turbo (OBD-II standard)",
        VehicleBrand.CITROEN, PidSet.OBD2_STANDARD
    ),
    CITROEN_NA(
        "citroen_na", "Citroen — Atmospherique (OBD-II standard)",
        VehicleBrand.CITROEN, PidSet.OBD2_STANDARD_NA
    ),
    DACIA_TURBO(
        "dacia_turbo", "Dacia — Turbo (OBD-II standard)",
        VehicleBrand.DACIA, PidSet.OBD2_STANDARD
    ),
    DACIA_NA(
        "dacia_na", "Dacia — Atmospherique (OBD-II standard)",
        VehicleBrand.DACIA, PidSet.OBD2_STANDARD_NA
    ),
    FIAT_TURBO(
        "fiat_turbo", "Fiat — Turbo (OBD-II standard)",
        VehicleBrand.FIAT, PidSet.OBD2_STANDARD
    ),
    FIAT_NA(
        "fiat_na", "Fiat — Atmospherique (OBD-II standard)",
        VehicleBrand.FIAT, PidSet.OBD2_STANDARD_NA
    ),
    FORD_TURBO(
        "ford_turbo", "Ford — Autre Turbo (OBD-II standard)",
        VehicleBrand.FORD, PidSet.OBD2_STANDARD
    ),
    FORD_NA(
        "ford_na", "Ford — Autre Atmospherique (OBD-II standard)",
        VehicleBrand.FORD, PidSet.OBD2_STANDARD_NA
    ),
    HONDA_TURBO(
        "honda_turbo", "Honda — Turbo (OBD-II standard)",
        VehicleBrand.HONDA, PidSet.OBD2_STANDARD
    ),
    HONDA_NA(
        "honda_na", "Honda — Autre Atmospherique (OBD-II standard)",
        VehicleBrand.HONDA, PidSet.OBD2_STANDARD_NA
    ),
    HYUNDAI_TURBO(
        "hyundai_turbo", "Hyundai — Turbo (OBD-II standard)",
        VehicleBrand.HYUNDAI, PidSet.OBD2_STANDARD
    ),
    HYUNDAI_NA(
        "hyundai_na", "Hyundai — Atmospherique (OBD-II standard)",
        VehicleBrand.HYUNDAI, PidSet.OBD2_STANDARD_NA
    ),
    JAGUAR_TURBO(
        "jaguar_turbo", "Jaguar — Turbo (OBD-II standard)",
        VehicleBrand.JAGUAR, PidSet.OBD2_STANDARD
    ),
    JAGUAR_NA(
        "jaguar_na", "Jaguar — Atmospherique (OBD-II standard)",
        VehicleBrand.JAGUAR, PidSet.OBD2_STANDARD_NA
    ),
    KIA_TURBO(
        "kia_turbo", "Kia — Turbo (OBD-II standard)",
        VehicleBrand.KIA, PidSet.OBD2_STANDARD
    ),
    KIA_NA(
        "kia_na", "Kia — Atmospherique (OBD-II standard)",
        VehicleBrand.KIA, PidSet.OBD2_STANDARD_NA
    ),
    LEXUS_TURBO(
        "lexus_turbo", "Lexus — Turbo (OBD-II standard)",
        VehicleBrand.LEXUS, PidSet.OBD2_STANDARD
    ),
    LEXUS_NA(
        "lexus_na", "Lexus — Atmospherique (OBD-II standard)",
        VehicleBrand.LEXUS, PidSet.OBD2_STANDARD_NA
    ),
    MAZDA_TURBO(
        "mazda_turbo", "Mazda — Turbo (OBD-II standard)",
        VehicleBrand.MAZDA, PidSet.OBD2_STANDARD
    ),
    MAZDA_NA(
        "mazda_na", "Mazda — Atmospherique (OBD-II standard)",
        VehicleBrand.MAZDA, PidSet.OBD2_STANDARD_NA
    ),
    MERCEDES_TURBO(
        "mercedes_turbo", "Mercedes — Turbo (OBD-II standard)",
        VehicleBrand.MERCEDES, PidSet.OBD2_STANDARD
    ),
    MERCEDES_NA(
        "mercedes_na", "Mercedes — Atmospherique (OBD-II standard)",
        VehicleBrand.MERCEDES, PidSet.OBD2_STANDARD_NA
    ),
    MINI_TURBO(
        "mini_turbo", "Mini — Turbo (OBD-II standard)",
        VehicleBrand.MINI, PidSet.OBD2_STANDARD
    ),
    MINI_NA(
        "mini_na", "Mini — Atmospherique (OBD-II standard)",
        VehicleBrand.MINI, PidSet.OBD2_STANDARD_NA
    ),
    MITSUBISHI_TURBO(
        "mitsubishi_turbo", "Mitsubishi — Turbo (OBD-II standard)",
        VehicleBrand.MITSUBISHI, PidSet.OBD2_STANDARD
    ),
    MITSUBISHI_NA(
        "mitsubishi_na", "Mitsubishi — Atmospherique (OBD-II standard)",
        VehicleBrand.MITSUBISHI, PidSet.OBD2_STANDARD_NA
    ),
    NISSAN_TURBO(
        "nissan_turbo", "Nissan — Turbo (OBD-II standard)",
        VehicleBrand.NISSAN, PidSet.OBD2_STANDARD
    ),
    NISSAN_NA(
        "nissan_na", "Nissan — Atmospherique (OBD-II standard)",
        VehicleBrand.NISSAN, PidSet.OBD2_STANDARD_NA
    ),
    OPEL_TURBO(
        "opel_turbo", "Opel — Turbo (OBD-II standard)",
        VehicleBrand.OPEL, PidSet.OBD2_STANDARD
    ),
    OPEL_NA(
        "opel_na", "Opel — Atmospherique (OBD-II standard)",
        VehicleBrand.OPEL, PidSet.OBD2_STANDARD_NA
    ),
    PEUGEOT_TURBO(
        "peugeot_turbo", "Peugeot — Turbo (OBD-II standard)",
        VehicleBrand.PEUGEOT, PidSet.OBD2_STANDARD
    ),
    PEUGEOT_NA(
        "peugeot_na", "Peugeot — Atmospherique (OBD-II standard)",
        VehicleBrand.PEUGEOT, PidSet.OBD2_STANDARD_NA
    ),
    PORSCHE_TURBO(
        "porsche_turbo", "Porsche — Turbo (OBD-II standard)",
        VehicleBrand.PORSCHE, PidSet.OBD2_STANDARD
    ),
    PORSCHE_NA(
        "porsche_na", "Porsche — Atmospherique (OBD-II standard)",
        VehicleBrand.PORSCHE, PidSet.OBD2_STANDARD_NA
    ),
    RENAULT_TURBO(
        "renault_turbo", "Renault — Turbo (OBD-II standard)",
        VehicleBrand.RENAULT, PidSet.OBD2_STANDARD
    ),
    RENAULT_NA(
        "renault_na", "Renault — Atmospherique (OBD-II standard)",
        VehicleBrand.RENAULT, PidSet.OBD2_STANDARD_NA
    ),
    SEAT_TURBO(
        "seat_turbo", "Seat — Turbo (OBD-II standard)",
        VehicleBrand.SEAT, PidSet.OBD2_STANDARD
    ),
    SEAT_NA(
        "seat_na", "Seat — Atmospherique (OBD-II standard)",
        VehicleBrand.SEAT, PidSet.OBD2_STANDARD_NA
    ),
    SKODA_TURBO(
        "skoda_turbo", "Skoda — Turbo (OBD-II standard)",
        VehicleBrand.SKODA, PidSet.OBD2_STANDARD
    ),
    SKODA_NA(
        "skoda_na", "Skoda — Atmospherique (OBD-II standard)",
        VehicleBrand.SKODA, PidSet.OBD2_STANDARD_NA
    ),
    SUBARU_TURBO(
        "subaru_turbo", "Subaru — Turbo (OBD-II standard)",
        VehicleBrand.SUBARU, PidSet.OBD2_STANDARD
    ),
    SUBARU_NA(
        "subaru_na", "Subaru — Atmospherique (OBD-II standard)",
        VehicleBrand.SUBARU, PidSet.OBD2_STANDARD_NA
    ),
    SUZUKI_TURBO(
        "suzuki_turbo", "Suzuki — Turbo (OBD-II standard)",
        VehicleBrand.SUZUKI, PidSet.OBD2_STANDARD
    ),
    SUZUKI_NA(
        "suzuki_na", "Suzuki — Atmospherique (OBD-II standard)",
        VehicleBrand.SUZUKI, PidSet.OBD2_STANDARD_NA
    ),
    TOYOTA_TURBO(
        "toyota_turbo", "Toyota — Turbo (OBD-II standard)",
        VehicleBrand.TOYOTA, PidSet.OBD2_STANDARD
    ),
    TOYOTA_NA(
        "toyota_na", "Toyota — Atmospherique (OBD-II standard)",
        VehicleBrand.TOYOTA, PidSet.OBD2_STANDARD_NA
    ),
    VOLKSWAGEN_TURBO(
        "volkswagen_turbo", "Volkswagen — Turbo (OBD-II standard)",
        VehicleBrand.VOLKSWAGEN, PidSet.OBD2_STANDARD
    ),
    VOLKSWAGEN_NA(
        "volkswagen_na", "Volkswagen — Atmospherique (OBD-II standard)",
        VehicleBrand.VOLKSWAGEN, PidSet.OBD2_STANDARD_NA
    ),
    VOLVO_TURBO(
        "volvo_turbo", "Volvo — Turbo (OBD-II standard)",
        VehicleBrand.VOLVO, PidSet.OBD2_STANDARD
    ),
    VOLVO_NA(
        "volvo_na", "Volvo — Atmospherique (OBD-II standard)",
        VehicleBrand.VOLVO, PidSet.OBD2_STANDARD_NA
    );

    /** Vehicle-specific gauge limits. Falls back to generic defaults for unknown profiles. */
    fun limitsFor(widget: WidgetType): GaugeLimits = when (this) {
        FIESTA_ST_MK7 -> when (widget) {
            WidgetType.RPM -> GaugeLimits(warningHigh = 5800.0, dangerHigh = 6500.0)
            WidgetType.SPEED -> GaugeLimits(warningHigh = 210.0, dangerHigh = 232.0)
            WidgetType.BOOST -> GaugeLimits(warningHigh = 120.0, dangerHigh = 145.0)
            WidgetType.OIL_TEMP -> GaugeLimits(warningLow = 60.0, warningHigh = 130.0, dangerHigh = 145.0)
            WidgetType.COOLANT -> GaugeLimits(warningLow = 60.0, warningHigh = 105.0, dangerHigh = 115.0)
            else -> GaugeLimits.forWidget(widget)
        }
        FIESTA_ST_MK8, PUMA_ST -> when (widget) {
            WidgetType.RPM -> GaugeLimits(warningHigh = 6000.0, dangerHigh = 6500.0)
            WidgetType.SPEED -> GaugeLimits(warningHigh = 215.0, dangerHigh = 232.0)
            WidgetType.BOOST -> GaugeLimits(warningHigh = 125.0, dangerHigh = 150.0)
            WidgetType.OIL_TEMP -> GaugeLimits(warningLow = 60.0, warningHigh = 130.0, dangerHigh = 145.0)
            WidgetType.COOLANT -> GaugeLimits(warningLow = 60.0, warningHigh = 105.0, dangerHigh = 115.0)
            else -> GaugeLimits.forWidget(widget)
        }
        FOCUS_ST_MK3, MUSTANG_ECOBOOST -> when (widget) {
            WidgetType.RPM -> GaugeLimits(warningHigh = 6000.0, dangerHigh = 6800.0)
            WidgetType.SPEED -> GaugeLimits(warningHigh = 230.0, dangerHigh = 248.0)
            WidgetType.BOOST -> GaugeLimits(warningHigh = 130.0, dangerHigh = 155.0)
            WidgetType.OIL_TEMP -> GaugeLimits(warningLow = 60.0, warningHigh = 130.0, dangerHigh = 145.0)
            WidgetType.COOLANT -> GaugeLimits(warningLow = 60.0, warningHigh = 105.0, dangerHigh = 115.0)
            else -> GaugeLimits.forWidget(widget)
        }
        FOCUS_ST_MK4 -> when (widget) {
            WidgetType.RPM -> GaugeLimits(warningHigh = 6000.0, dangerHigh = 6800.0)
            WidgetType.SPEED -> GaugeLimits(warningHigh = 240.0, dangerHigh = 250.0)
            WidgetType.BOOST -> GaugeLimits(warningHigh = 135.0, dangerHigh = 160.0)
            WidgetType.OIL_TEMP -> GaugeLimits(warningLow = 60.0, warningHigh = 130.0, dangerHigh = 145.0)
            WidgetType.COOLANT -> GaugeLimits(warningLow = 60.0, warningHigh = 105.0, dangerHigh = 115.0)
            else -> GaugeLimits.forWidget(widget)
        }
        FOCUS_RS_MK3 -> when (widget) {
            WidgetType.RPM -> GaugeLimits(warningHigh = 6000.0, dangerHigh = 6800.0)
            WidgetType.SPEED -> GaugeLimits(warningHigh = 250.0, dangerHigh = 266.0)
            WidgetType.BOOST -> GaugeLimits(warningHigh = 150.0, dangerHigh = 175.0)
            WidgetType.OIL_TEMP -> GaugeLimits(warningLow = 60.0, warningHigh = 135.0, dangerHigh = 150.0)
            WidgetType.COOLANT -> GaugeLimits(warningLow = 60.0, warningHigh = 108.0, dangerHigh = 118.0)
            else -> GaugeLimits.forWidget(widget)
        }
        FOCUS_ST_MK2, FOCUS_RS_MK2 -> when (widget) {
            WidgetType.RPM -> GaugeLimits(warningHigh = 6200.0, dangerHigh = 7000.0)
            WidgetType.SPEED -> GaugeLimits(warningHigh = 230.0, dangerHigh = 241.0)
            WidgetType.BOOST -> GaugeLimits(warningHigh = 110.0, dangerHigh = 135.0)
            WidgetType.OIL_TEMP -> GaugeLimits(warningLow = 60.0, warningHigh = 130.0, dangerHigh = 145.0)
            WidgetType.COOLANT -> GaugeLimits(warningLow = 60.0, warningHigh = 105.0, dangerHigh = 115.0)
            else -> GaugeLimits.forWidget(widget)
        }
        FIESTA_ST_MK6 -> when (widget) {
            WidgetType.RPM -> GaugeLimits(warningHigh = 6200.0, dangerHigh = 7000.0)
            WidgetType.SPEED -> GaugeLimits(warningHigh = 190.0, dangerHigh = 200.0)
            WidgetType.OIL_TEMP -> GaugeLimits(warningLow = 60.0, warningHigh = 120.0, dangerHigh = 140.0)
            WidgetType.COOLANT -> GaugeLimits(warningLow = 60.0, warningHigh = 105.0, dangerHigh = 115.0)
            else -> GaugeLimits.forWidget(widget)
        }
        CRX_DELSOL_D16, CRX_DELSOL_B16 -> when (widget) {
            WidgetType.RPM -> GaugeLimits(warningHigh = 7200.0, dangerHigh = 8200.0)
            WidgetType.SPEED -> GaugeLimits(warningHigh = 190.0, dangerHigh = 200.0)
            WidgetType.COOLANT -> GaugeLimits(warningLow = 60.0, warningHigh = 100.0, dangerHigh = 110.0)
            else -> GaugeLimits.forWidget(widget)
        }
        CIVIC_EJ9_D14, CIVIC_EJ9_D15 -> when (widget) {
            WidgetType.RPM -> GaugeLimits(warningHigh = 6200.0, dangerHigh = 7000.0)
            WidgetType.SPEED -> GaugeLimits(warningHigh = 170.0, dangerHigh = 185.0)
            WidgetType.COOLANT -> GaugeLimits(warningLow = 60.0, warningHigh = 100.0, dangerHigh = 110.0)
            else -> GaugeLimits.forWidget(widget)
        }
        else -> GaugeLimits.forWidget(widget)
    }

    /** Vehicle-specific gauge ranges (min/max scale). */
    fun rangeFor(widget: WidgetType): GaugeRange {
        val limits = limitsFor(widget)
        val base = GaugeRange(widget.minValue, widget.maxValue, limits.warningLow, limits.dangerLow, limits.warningHigh, limits.dangerHigh)
        return when (this) {
            FIESTA_ST_MK7 -> when (widget) {
                WidgetType.RPM -> base.copy(maxValue = 7000.0)
                WidgetType.SPEED -> base.copy(maxValue = 240.0)
                else -> base
            }
            FIESTA_ST_MK8, PUMA_ST -> when (widget) {
                WidgetType.RPM -> base.copy(maxValue = 7000.0)
                WidgetType.SPEED -> base.copy(maxValue = 240.0)
                else -> base
            }
            FOCUS_ST_MK3, MUSTANG_ECOBOOST -> when (widget) {
                WidgetType.RPM -> base.copy(maxValue = 7200.0)
                WidgetType.SPEED -> base.copy(maxValue = 260.0)
                else -> base
            }
            FOCUS_ST_MK4 -> when (widget) {
                WidgetType.RPM -> base.copy(maxValue = 7200.0)
                WidgetType.SPEED -> base.copy(maxValue = 270.0)
                else -> base
            }
            FOCUS_RS_MK3 -> when (widget) {
                WidgetType.RPM -> base.copy(maxValue = 7200.0)
                WidgetType.SPEED -> base.copy(maxValue = 280.0)
                else -> base
            }
            FOCUS_ST_MK2, FOCUS_RS_MK2 -> when (widget) {
                WidgetType.RPM -> base.copy(maxValue = 7500.0)
                WidgetType.SPEED -> base.copy(maxValue = 260.0)
                else -> base
            }
            FIESTA_ST_MK6 -> when (widget) {
                WidgetType.RPM -> base.copy(maxValue = 7500.0)
                WidgetType.SPEED -> base.copy(maxValue = 220.0)
                else -> base
            }
            CRX_DELSOL_D16, CRX_DELSOL_B16 -> when (widget) {
                WidgetType.RPM -> base.copy(maxValue = 8500.0)
                WidgetType.SPEED -> base.copy(maxValue = 220.0)
                else -> base
            }
            CIVIC_EJ9_D14, CIVIC_EJ9_D15 -> when (widget) {
                WidgetType.RPM -> base.copy(maxValue = 7500.0)
                WidgetType.SPEED -> base.copy(maxValue = 200.0)
                else -> base
            }
            else -> base
        }
    }

    companion object {
        fun fromKey(key: String?): VehicleProfile? = entries.firstOrNull { it.key == key }

        fun forBrand(brand: VehicleBrand): List<VehicleProfile> =
            entries.filter { it.brand == brand }

        /** Fallback default */
        val DEFAULT = FIESTA_ST_MK7
    }
}

/**
 * Defines which PIDs and reading strategy to use for a vehicle.
 */
enum class PidSet {
    /**
     * Ford EcoBoost (1.5T 3cyl, 1.6T, 2.0T, 2.3T) : Mode 01 standard uniquement.
     * Boost via MAP(0x0B) - baro(0x33) — méthode confirmée.
     * Temp huile (PID 0x5C Mode 01) : optionnel SAE J1979, non supporté sur EcoBoost.
     * Aucun PID Mode 22 Ford pour temp huile n'est documenté publiquement — désactivé
     * jusqu'à vérification terrain sur véhicule réel.
     */
    FORD_ECOBOOST,

    /**
     * Ford Focus RS Mk3 (2.3 EcoBoost) : identique à FORD_ECOBOOST pour l'instant.
     * Note : certaines sources mentionnent des PIDs via header CAN non standard (0x310),
     * mais ces adresses ne sont pas confirmées — le header 784/0x6017 précédemment codé
     * n'a été trouvé dans aucune source vérifiable et a été retiré.
     */
    FORD_ECOBOOST_RS,

    /**
     * Ford Duratec 2.5 I5 / Volvo B5254 (Focus ST Mk2, Focus RS Mk2, 2005-2011).
     * ECU Siemens/Continental — architecture différente de CGEA 1.2.
     * Mode 01 standard uniquement. Boost via MAP(0x0B) - baro(0x33).
     * Temp huile non confirmée via OBD sur cette plateforme.
     */
    FORD_DURATEC_25,

    /** Standard OBD-II Mode 01 uniquement, véhicules turbo. */
    OBD2_STANDARD,

    /**
     * Standard OBD-II Mode 01, véhicules atmosphériques (Honda D/B-series 1996-2000, etc.).
     * PID 0x5C (temp huile) non polled : pas de capteur ECU raccordé sur ces moteurs Honda.
     * PID 0x2F (carburant) non polled : câblé directement au tableau de bord, hors ECU.
     * PID 0x5E (débit carburant) non polled : non implémenté sur ces ECU.
     */
    OBD2_STANDARD_NA
}
