//! ORDRSP full pipeline roundtrip tests.
//!
//! Tests EDIFACT → tokenize → split → assemble → map_interchange(SG27)
//! → map_interchange_reverse → disassemble → render → byte-identical comparison.

mod common;

// ── Roundtrip tests for PIDs with real fixtures ──

#[test]
fn test_ordrsp_pid_19120_roundtrip() {
    common::ordrsp::run_full_roundtrip("19120");
}

#[test]
fn test_ordrsp_pid_19131_roundtrip() {
    common::ordrsp::run_full_roundtrip("19131");
}

#[test]
fn test_ordrsp_pid_19133_roundtrip() {
    // KNOWN_INCOMPLETE: 19133_ORDRSP_1.4_DEV-90605.edi has FTX with trailing empty
    // C108 components (d4440_4/d4440_5 = "::") that are dropped by empty string omission
    // in forward mapping and not reconstructed on reverse. Semantically equivalent.
    common::ordrsp::run_full_roundtrip_with_skip("19133", &["19133_ORDRSP_1.4_DEV-90605.edi"]);
}

/// Verify all ORDRSP PID TOML mappings load successfully.
#[test]
fn test_ordrsp_all_pids_toml_loading() {
    use mig_bo4e::engine::MappingEngine;
    use mig_bo4e::path_resolver::PathResolver;
    use std::path::Path;

    let schema_dir = Path::new(common::ordrsp::SCHEMA_DIR);
    let msg_dir = common::ordrsp::message_dir();
    let resolver = PathResolver::from_schema_dir(schema_dir);

    // Verify message-level engine loads
    let msg_engine = MappingEngine::load(&msg_dir)
        .unwrap()
        .with_path_resolver(resolver.clone());
    assert!(
        !msg_engine.definitions().is_empty(),
        "Message engine should have definitions"
    );

    let pids = [
        "19001", "19002", "19003", "19004", "19005", "19006", "19007", "19009", "19010", "19011",
        "19012", "19013", "19014", "19015", "19016", "19101", "19102", "19103", "19104", "19110",
        "19114", "19115", "19116", "19117", "19118", "19119", "19120", "19121", "19123", "19124",
        "19127", "19128", "19129", "19130", "19131", "19132", "19133", "19204", "19301", "19302",
    ];

    // PIDs without SG27 don't need a tx directory
    let no_sg27 = [
        "19001", "19002", "19003", "19004", "19005", "19006", "19007", "19009", "19010", "19012",
        "19013", "19014", "19015", "19016", "19101", "19102", "19103", "19104", "19110", "19114",
        "19115", "19117", "19118", "19119", "19120", "19121", "19123", "19124", "19127", "19128",
        "19129", "19130", "19131", "19132", "19133", "19204", "19301", "19302",
    ];

    for pid in pids {
        let tx_dir = common::ordrsp::pid_dir(pid);
        if !tx_dir.exists() {
            if no_sg27.contains(&pid) {
                eprintln!("PID {pid}: no SG27, no tx directory needed — OK");
            } else {
                eprintln!("PID {pid}: no mapping directory — skipping");
            }
            continue;
        }

        match MappingEngine::load(&tx_dir) {
            Ok(engine) => {
                let engine = engine.with_path_resolver(resolver.clone());
                let count = engine.definitions().len();
                eprintln!("PID {pid}: {count} definitions loaded OK");
            }
            Err(e) => panic!("PID {pid}: failed to load TOML mappings: {e}"),
        }
    }
}

// ── Generated fixture roundtrip tests ──

const ALL_PIDS: &[&str] = &[
    "19001", "19002", "19003", "19004", "19005", "19006", "19007", "19009", "19010", "19011",
    "19012", "19013", "19014", "19015", "19016", "19101", "19102", "19103", "19104", "19110",
    "19114", "19115", "19116", "19117", "19118", "19119", "19120", "19121", "19123", "19124",
    "19127", "19128", "19129", "19130", "19131", "19132", "19133", "19204", "19301", "19302",
];

/// PIDs with known mapping gaps that prevent byte-identical roundtrip.
const KNOWN_INCOMPLETE: &[&str] = &[];

#[test]
fn test_ordrsp_generated_roundtrip_all() {
    let mut passed = 0;
    let mut failed = Vec::new();
    let mut skipped = 0;

    for pid in ALL_PIDS {
        let Some(fixture) = common::ordrsp::discover_generated_fixture(pid) else {
            eprintln!("PID {pid}: no generated fixture — skipping");
            skipped += 1;
            continue;
        };

        let Some(mig) = common::ordrsp::load_pid_filtered_mig(pid) else {
            eprintln!("PID {pid}: MIG/AHB XML not available — skipping");
            skipped += 1;
            continue;
        };

        let tx_dir = common::ordrsp::pid_dir(pid);
        let (msg_engine, tx_engine) = if tx_dir.exists() {
            common::ordrsp::load_split_engines(pid)
        } else {
            let msg = common::ordrsp::load_message_engine();
            let tx = mig_bo4e::engine::MappingEngine::from_definitions(vec![]);
            (msg, tx)
        };

        let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
            common::test_utils::run_single_fixture_roundtrip_with_tx_group(
                pid,
                &fixture,
                &mig,
                &msg_engine,
                &tx_engine,
                common::ordrsp::TX_GROUP,
            );
        }));

        match result {
            Ok(()) => {
                if KNOWN_INCOMPLETE.contains(pid) {
                    eprintln!(
                        "PID {pid}: PASSED (was KNOWN_INCOMPLETE — can be removed from list!)"
                    );
                }
                passed += 1;
            }
            Err(_) => {
                if KNOWN_INCOMPLETE.contains(pid) {
                    eprintln!("PID {pid}: KNOWN_INCOMPLETE (expected failure)");
                } else {
                    eprintln!("PID {pid}: UNEXPECTED FAILURE");
                    failed.push(*pid);
                }
            }
        }
    }

    eprintln!(
        "\nORDRSP generated roundtrip: {passed} passed, {} known incomplete, {skipped} skipped",
        KNOWN_INCOMPLETE.len()
    );

    if !failed.is_empty() {
        panic!(
            "Unexpected failures for PIDs: {:?} — add to KNOWN_INCOMPLETE or fix mappings",
            failed
        );
    }
}
