//! CONTRL FV2510 test utilities.
//!
//! Mirrors FV2504 CONTRL config with FV2510 paths.
//! CONTRL has no TOML mappings in FV2504 — only assembly/roundtrip.
//! FV2510 adds 3 TOML mapping files.

use super::test_utils::MessageTypeConfig;
use mig_bo4e::engine::MappingEngine;
use mig_bo4e::path_resolver::PathResolver;
use mig_bo4e::pid_schema_index::PidSchemaIndex;
use mig_types::schema::mig::MigSchema;
use std::path::{Path, PathBuf};

// ── Paths (relative to crate root = crates/mig-bo4e) ──

pub const FIXTURE_DIR: &str = "../../example_market_communication_bo4e_transactions/CONTRL/FV2510";
pub const MAPPINGS_BASE: &str = "../../mappings/FV2510/CONTRL";
pub const SCHEMA_DIR: &str = "../../crates/mig-types/src/generated/fv2510/contrl/pids";

/// CONTRL has no transaction group — message-only.
pub const TX_GROUP: &str = "";

/// AHB PID is empty string (CONTRL has no real PID).
const PID_AHB: &str = "";

/// Fixture filenames use "91001" prefix by convention.
pub const PID_FIXTURE: &str = "91001";

const CONFIG: MessageTypeConfig = MessageTypeConfig {
    fixture_dir: FIXTURE_DIR,
    mappings_base: MAPPINGS_BASE,
    schema_dir: SCHEMA_DIR,
    message_type: "CONTRL",
    variant: None,
    tx_group: TX_GROUP,
    format_version: "FV2510",
};

pub fn path_resolver() -> PathResolver {
    CONFIG.path_resolver()
}

pub fn message_dir() -> PathBuf {
    CONFIG.message_dir()
}

pub fn schema_index() -> PidSchemaIndex {
    CONFIG.schema_index(PID_AHB)
}

pub fn load_pid_filtered_mig() -> Option<MigSchema> {
    CONFIG.load_pid_filtered_mig(PID_AHB)
}

pub fn load_message_engine() -> MappingEngine {
    // CONTRL has no message/ subdirectory — TOML files are directly in MAPPINGS_BASE
    let resolver = CONFIG.path_resolver();
    MappingEngine::load(Path::new(MAPPINGS_BASE))
        .unwrap()
        .with_path_resolver(resolver)
}

/// Discover generated fixture file.
///
/// Prefers fixtures/generated/ (locally regenerated) over submodule generated/ dir.
pub fn discover_generated_fixture() -> Option<PathBuf> {
    let local = Path::new("../../fixtures/generated/fv2510/contrl").join("_CONTRL_generated.edi");
    if local.exists() {
        return Some(local);
    }
    // Try enhanced fixture first, then basic
    let enhanced = Path::new(FIXTURE_DIR).join("generated/.edi");
    if enhanced.exists() {
        return Some(enhanced);
    }
    let basic = Path::new(FIXTURE_DIR).join("generated/_CONTRL_generated.edi");
    if basic.exists() {
        Some(basic)
    } else {
        None
    }
}
