//! Pure front-matter helpers for `service::story`.
//!
//! These functions validate and inspect story front-matter field values
//! without performing any I/O. Parsing is delegated to `crate::io::story_metadata`.

#[allow(dead_code)]
/// Return `true` if `stage` is a recognised pipeline stage directory name.
///
/// Valid stage names match the `.huskies/work/N_name/` directory scheme.
pub fn is_valid_stage(stage: &str) -> bool {
    crate::pipeline_state::Stage::from_dir(stage).is_some()
}

#[allow(dead_code)]
/// Map a human-readable stage alias (e.g. `"backlog"`) to its directory name
/// (e.g. `"1_backlog"`). Returns `None` for unrecognised aliases.
pub fn stage_alias_to_dir(alias: &str) -> Option<&'static str> {
    use crate::pipeline_state::Stage;
    // Canonical directory names (e.g. "1_backlog") round-trip through the typed enum.
    if let Some(stage) = Stage::from_dir(alias) {
        return Some(stage.dir_name());
    }
    // Short human-readable aliases (user-facing input normalization).
    match alias {
        "backlog" => Some("1_backlog"),
        "current" => Some("2_current"),
        "qa" => Some("3_qa"),
        "merge" => Some("4_merge"),
        "done" => Some("5_done"),
        "archived" => Some("6_archived"),
        _ => None,
    }
}

// ── Tests ─────────────────────────────────────────────────────────────────────

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

    #[test]
    fn is_valid_stage_accepts_all_known_stages() {
        assert!(is_valid_stage("1_backlog"));
        assert!(is_valid_stage("2_current"));
        assert!(is_valid_stage("3_qa"));
        assert!(is_valid_stage("4_merge"));
        assert!(is_valid_stage("5_done"));
        assert!(is_valid_stage("6_archived"));
    }

    #[test]
    fn is_valid_stage_rejects_unknown() {
        assert!(!is_valid_stage("current"));
        assert!(!is_valid_stage("backlog"));
        assert!(!is_valid_stage("7_future"));
        assert!(!is_valid_stage(""));
    }

    #[test]
    fn stage_alias_maps_short_names() {
        assert_eq!(stage_alias_to_dir("backlog"), Some("1_backlog"));
        assert_eq!(stage_alias_to_dir("current"), Some("2_current"));
        assert_eq!(stage_alias_to_dir("qa"), Some("3_qa"));
        assert_eq!(stage_alias_to_dir("merge"), Some("4_merge"));
        assert_eq!(stage_alias_to_dir("done"), Some("5_done"));
        assert_eq!(stage_alias_to_dir("archived"), Some("6_archived"));
    }

    #[test]
    fn stage_alias_maps_full_dir_names() {
        assert_eq!(stage_alias_to_dir("1_backlog"), Some("1_backlog"));
        assert_eq!(stage_alias_to_dir("6_archived"), Some("6_archived"));
    }

    #[test]
    fn stage_alias_returns_none_for_unknown() {
        assert_eq!(stage_alias_to_dir("unknown"), None);
        assert_eq!(stage_alias_to_dir(""), None);
    }
}
