use std::path::{Path, PathBuf};

/// Base directory for generated MIG schema files, relative to workspace root.
const GENERATED_BASE: &str = "crates/mig-types/src/generated";

/// Returns the directory containing PID schema JSON files for a given
/// format version and message type.
///
/// Both arguments are lowercased automatically.
///
/// Example: `schema_dir("FV2504", "UTILMD")` -> `crates/mig-types/src/generated/fv2504/utilmd/pids`
pub fn schema_dir(format_version: &str, message_type: &str) -> PathBuf {
    Path::new(GENERATED_BASE)
        .join(format_version.to_lowercase())
        .join(message_type.to_lowercase())
        .join("pids")
}

/// Returns the path to a specific PID schema JSON file.
///
/// Example: `pid_schema_file("FV2504", "UTILMD", "55001")` ->
///   `crates/mig-types/src/generated/fv2504/utilmd/pids/pid_55001_schema.json`
pub fn pid_schema_file(format_version: &str, message_type: &str, pid: &str) -> PathBuf {
    schema_dir(format_version, message_type).join(format!("pid_{pid}_schema.json"))
}

/// Returns the base directory for all generated schemas.
pub fn generated_base() -> &'static Path {
    Path::new(GENERATED_BASE)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_schema_dir() {
        assert_eq!(
            schema_dir("FV2504", "UTILMD"),
            PathBuf::from("crates/mig-types/src/generated/fv2504/utilmd/pids")
        );
    }

    #[test]
    fn test_schema_dir_case_insensitive() {
        assert_eq!(
            schema_dir("fv2504", "utilmd"),
            schema_dir("FV2504", "UTILMD")
        );
    }

    #[test]
    fn test_pid_schema_file() {
        assert_eq!(
            pid_schema_file("FV2504", "UTILMD", "55001"),
            PathBuf::from(
                "crates/mig-types/src/generated/fv2504/utilmd/pids/pid_55001_schema.json"
            )
        );
    }

    #[test]
    fn test_generated_base() {
        assert_eq!(
            generated_base(),
            Path::new("crates/mig-types/src/generated")
        );
    }
}
