//! APERAK FV2604 test utilities.
//!
//! Mirrors FV2510 APERAK config with FV2604 paths.
//! APERAK has a single workflow with empty PID in the AHB, but fixtures
//! use "92001" prefix by convention.

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/APERAK/FV2604";
pub const MAPPINGS_BASE: &str = "../../mappings/FV2604/APERAK";
pub const SCHEMA_DIR: &str = "../../crates/mig-types/src/generated/fv2604/aperak/pids";

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

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

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

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

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

pub fn message_dir() -> PathBuf {
    // APERAK has no tx_group — all definitions are at root level (no message/ subdir)
    PathBuf::from(MAPPINGS_BASE)
}

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 discover_fixtures() -> Vec<PathBuf> {
    CONFIG.discover_fixtures(PID_FIXTURE)
}

pub fn load_message_engine() -> MappingEngine {
    // APERAK has no message/ subdir — load from root mappings dir
    let resolver = path_resolver();
    MappingEngine::load(&message_dir())
        .unwrap()
        .with_path_resolver(resolver)
}

/// Discover generated fixture file (FV2604 uses `_APERAK_generated.edi`).
///
/// Prefers fixtures/generated/ (locally regenerated) over submodule generated/ dir.
pub fn discover_generated_fixture() -> Option<PathBuf> {
    let local = Path::new("../../fixtures/generated/fv2604/aperak").join("_APERAK_generated.edi");
    if local.exists() {
        return Some(local);
    }
    let path = Path::new(FIXTURE_DIR).join("generated/_APERAK_generated.edi");
    if path.exists() {
        Some(path)
    } else {
        None
    }
}
