use crate::inspect::smp::special_conditions::*;

pub(in crate::inspect::smp) fn parse_special_conditions_probe(
    bytes: &[u8],
    file_extension_hint: Option<&str>,
    container_profile: Option<&SmpContainerProfile>,
) -> Option<SmpSpecialConditionsProbe> {
    let table_len = SPECIAL_CONDITION_COUNT.checked_mul(4)?;
    let table_end = SPECIAL_CONDITIONS_OFFSET.checked_add(table_len)?;
    if table_end > bytes.len() {
        return None;
    }

    let mut entries = Vec::with_capacity(SPECIAL_CONDITION_COUNT);
    for definition in KNOWN_SPECIAL_CONDITION_DEFINITIONS {
        let value = read_u32_at(
            bytes,
            SPECIAL_CONDITIONS_OFFSET + (definition.slot_index as usize) * 4,
        )?;
        if value > 1 {
            return None;
        }
        entries.push(SmpSpecialConditionEntry {
            slot_index: definition.slot_index,
            hidden: definition.hidden,
            label_id: definition.label_id,
            help_id: definition.help_id,
            label: definition.label.to_string(),
            value,
            value_hex: format!("0x{value:08x}"),
        });
    }

    let hidden_sentinel = entries
        .iter()
        .find(|entry| entry.slot_index == SPECIAL_CONDITION_HIDDEN_SENTINEL_SLOT as u8)?;
    if hidden_sentinel.value != 1 {
        return None;
    }

    let enabled_visible_labels = entries
        .iter()
        .filter(|entry| !entry.hidden && entry.value != 0)
        .map(|entry| entry.label.clone())
        .collect::<Vec<_>>();
    let source_kind = match file_extension_hint.unwrap_or("") {
        "gmp" => "map-fixed-special-conditions-range",
        "gms" => "save-fixed-special-conditions-range",
        "gmx" => "sandbox-fixed-special-conditions-range",
        _ => "fixed-special-conditions-range",
    }
    .to_string();
    let profile_family = container_profile
        .map(|profile| profile.profile_family.clone())
        .unwrap_or_else(|| "unknown".to_string());
    let mut evidence = vec![
        format!("fixed 36-dword range at 0x{SPECIAL_CONDITIONS_OFFSET:04x}"),
        "all observed lanes are boolean dwords".to_string(),
        "hidden slot 35 carries the expected sentinel value 1".to_string(),
        "slot metadata matches the grounded editor special-conditions table at 0x005f3ab0"
            .to_string(),
    ];
    if enabled_visible_labels.is_empty() {
        evidence.push("no visible special conditions enabled in this file".to_string());
    } else {
        evidence.push(format!(
            "enabled visible conditions: {}",
            enabled_visible_labels.join(", ")
        ));
    }

    Some(SmpSpecialConditionsProbe {
        profile_family,
        source_kind,
        table_offset: SPECIAL_CONDITIONS_OFFSET,
        table_len,
        enabled_visible_count: enabled_visible_labels.len(),
        enabled_visible_labels,
        hidden_sentinel_slot_index: SPECIAL_CONDITION_HIDDEN_SENTINEL_SLOT as u8,
        hidden_sentinel_value: hidden_sentinel.value,
        hidden_sentinel_value_hex: hidden_sentinel.value_hex.clone(),
        entries,
        evidence,
    })
}
