/// Versification systems for biblical texts.
///
/// Different traditions number chapters and verses differently in the same books.
/// This enum identifies the versification scheme to use when working with references.
///
/// # Examples
///
/// ```rust
/// use biblicist::{ParseOptions, Versification};
///
/// // Parse with Masoretic versification (Hebrew numbering)
/// let options = ParseOptions::new().with_versification(Versification::Masoretic);
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Versification {
    /// Standard English/Protestant versification (default).
    ///
    /// Used by most modern English translations (KJV, ESV, NIV, NASB, etc.).
    /// This is the default versification and matches the verse counts stored in
    /// each book's layout.
    #[default]
    English,

    /// Hebrew Masoretic Text (MT) versification.
    ///
    /// Used in Jewish and scholarly contexts. Differs from English versification
    /// notably in Joel (4 chapters vs 3) and Malachi (3 chapters vs 4).
    /// Many Psalms also differ as superscription titles are numbered as verse 1
    /// in the Masoretic tradition.
    Masoretic,

    /// Greek Septuagint (LXX) versification.
    ///
    /// Used in Eastern Orthodox traditions. Differs from English versification
    /// in Psalms (adds Psalm 151), and in portions of Job, Jeremiah, and Esther.
    Septuagint,

    /// Latin Vulgate versification.
    ///
    /// Used in Catholic traditions. Primarily follows the Masoretic Text with
    /// some Septuagint-influenced differences.
    Vulgate,
}

/// Returns a versification-specific chapter layout override for a book, if one exists.
///
/// Returns `None` when no override is defined for the given book/versification
/// combination, meaning the book's default (English) layout should be used.
///
/// The returned slice is indexed by chapter number (index 0 is a placeholder;
/// `slice[1]` is the verse count for chapter 1, etc.).
#[inline]
pub(crate) fn get_layout_override(
    book_title: &str,
    versification: Versification,
) -> Option<&'static [u8]> {
    match versification {
        Versification::English => None,
        Versification::Masoretic => get_masoretic_layout(book_title),
        Versification::Septuagint => get_septuagint_layout(book_title),
        Versification::Vulgate => get_vulgate_layout(book_title),
    }
}

/// Masoretic Text versification layout overrides.
///
/// Key differences from English:
/// - **Joel**: 4 chapters (English 2:28-32 = MT 3:1-5; English chapter 3 = MT chapter 4)
/// - **Malachi**: 3 chapters (English chapter 4 = MT 3:19-24)
fn get_masoretic_layout(book_title: &str) -> Option<&'static [u8]> {
    match book_title {
        // Joel in MT has 4 chapters:
        //   ch1 = 20v (same as English 1)
        //   ch2 = 27v (English 2:1-27)
        //   ch3 = 5v  (English 2:28-32)
        //   ch4 = 21v (English 3:1-21)
        "Joel" => Some(&[0, 20, 27, 5, 21]),

        // Malachi in MT has 3 chapters:
        //   ch1 = 14v (same as English 1)
        //   ch2 = 17v (same as English 2)
        //   ch3 = 24v (English 3:1-18 + English 4:1-6 = 18+6 = 24)
        "Malachi" => Some(&[0, 14, 17, 24]),

        _ => None,
    }
}

/// Septuagint versification layout overrides.
fn get_septuagint_layout(_book_title: &str) -> Option<&'static [u8]> {
    // TODO: Add Septuagint layout overrides (e.g., Psalms with Psalm 151,
    // differences in Job, Jeremiah, and Esther).
    None
}

/// Vulgate versification layout overrides.
fn get_vulgate_layout(_book_title: &str) -> Option<&'static [u8]> {
    // TODO: Add Vulgate layout overrides.
    None
}

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

    #[test]
    fn default_versification_is_english() {
        assert_eq!(Versification::default(), Versification::English);
    }

    #[test]
    fn english_versification_has_no_overrides() {
        assert!(get_layout_override("Joel", Versification::English).is_none());
        assert!(get_layout_override("Malachi", Versification::English).is_none());
        assert!(get_layout_override("Genesis", Versification::English).is_none());
    }

    #[test]
    fn masoretic_joel_has_four_chapters() {
        let layout = get_layout_override("Joel", Versification::Masoretic)
            .expect("Joel should have a Masoretic override");
        // Exclude index 0 (placeholder)
        assert_eq!(layout.len() - 1, 4, "Masoretic Joel should have 4 chapters");
        assert_eq!(layout[1], 20);
        assert_eq!(layout[2], 27);
        assert_eq!(layout[3], 5);
        assert_eq!(layout[4], 21);
    }

    #[test]
    fn masoretic_malachi_has_three_chapters() {
        let layout = get_layout_override("Malachi", Versification::Masoretic)
            .expect("Malachi should have a Masoretic override");
        assert_eq!(
            layout.len() - 1,
            3,
            "Masoretic Malachi should have 3 chapters"
        );
        assert_eq!(layout[1], 14);
        assert_eq!(layout[2], 17);
        assert_eq!(layout[3], 24);
    }

    #[test]
    fn masoretic_genesis_has_no_override() {
        assert!(
            get_layout_override("Genesis", Versification::Masoretic).is_none(),
            "Genesis has no Masoretic versification differences"
        );
    }

    #[test]
    fn versification_is_copy() {
        let v = Versification::Masoretic;
        let _v2 = v; // Copy, no move
        let _v3 = v; // Can be used again
    }
}
