use std::path::Path;

use crate::meta::META_FILENAME;

pub const SENTINEL_SCATTERED: &str = "scatter.done";
pub const SENTINEL_COUNTED:   &str = "count.done";
pub const SENTINEL_INDEXED:   &str = "index.done";

/// Progression state of a `KmerIndex`.
///
/// Variants are ordered: `Empty < Scattered < Counted < Indexed`.
/// A state is reported only when its sentinel file is fully present —
/// partial states (e.g. scatter interrupted mid-way) are not accepted.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum IndexState {
    /// `index.meta` present; scatter not yet completed.
    Empty,
    /// `scatter.done` sentinel present — all super-kmers have been routed.
    Scattered,
    /// `count.done` sentinel present — dereplicate + count complete.
    Counted,
    /// `index.done` sentinel present — layered MPHF index fully built.
    Indexed,
}

impl IndexState {
    /// Detect the state of the index at `root`.
    ///
    /// Returns `None` if `index.meta` is absent (not an obikindex directory).
    pub fn detect(root: &Path) -> Option<Self> {
        if !root.join(META_FILENAME).exists() {
            return None;
        }
        if root.join(SENTINEL_INDEXED).exists() {
            return Some(Self::Indexed);
        }
        if root.join(SENTINEL_COUNTED).exists() {
            return Some(Self::Counted);
        }
        if root.join(SENTINEL_SCATTERED).exists() {
            return Some(Self::Scattered);
        }
        Some(Self::Empty)
    }
}
