//! Shared backup-path policy used by both the async `Migrator::migrate`
//! orchestrator and the sync `migrate_to_v7_sync` chain.
//!
//! The destructive V→V7 chain (per-step migrations plus `split_graph`
//! and `v6_to_v7_rename`) runs against a freshly-written file via the
//! atomic temp+rename pattern. A pre-chain `.v{N}.backup` is the user's
//! rollback artifact if any step fails mid-chain. The backup is taken
//! before the first per-step migration runs and removed only on full
//! V→V7 success, so it covers the entire chain from V1/V2/V3/V4/V5/V6
//! all the way to V7.

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

/// Return `Some(path.vN.backup)` for source versions that need a
/// pre-V7-chain backup; `None` for V7 (no migration needed).
pub(crate) fn pre_v7_backup_path_for(from: FormatVersion, path: &Path) -> Option<PathBuf> {
    match from {
        FormatVersion::V1 => Some(path.with_extension("v1.backup")),
        FormatVersion::V2 => Some(path.with_extension("v2.backup")),
        FormatVersion::V3 => Some(path.with_extension("v3.backup")),
        FormatVersion::V4 => Some(path.with_extension("v4.backup")),
        FormatVersion::V5 => Some(path.with_extension("v5.backup")),
        FormatVersion::V6 => Some(path.with_extension("v6.backup")),
        _ => None,
    }
}

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

    fn p() -> PathBuf {
        PathBuf::from("/tmp/board.json")
    }

    #[test]
    fn returns_some_for_v1() {
        assert_eq!(
            pre_v7_backup_path_for(FormatVersion::V1, &p()),
            Some(PathBuf::from("/tmp/board.v1.backup"))
        );
    }

    #[test]
    fn returns_some_for_v2() {
        assert_eq!(
            pre_v7_backup_path_for(FormatVersion::V2, &p()),
            Some(PathBuf::from("/tmp/board.v2.backup"))
        );
    }

    #[test]
    fn returns_some_for_v3() {
        assert_eq!(
            pre_v7_backup_path_for(FormatVersion::V3, &p()),
            Some(PathBuf::from("/tmp/board.v3.backup"))
        );
    }

    #[test]
    fn returns_some_for_v4() {
        assert_eq!(
            pre_v7_backup_path_for(FormatVersion::V4, &p()),
            Some(PathBuf::from("/tmp/board.v4.backup"))
        );
    }

    #[test]
    fn returns_some_for_v5() {
        assert_eq!(
            pre_v7_backup_path_for(FormatVersion::V5, &p()),
            Some(PathBuf::from("/tmp/board.v5.backup"))
        );
    }

    #[test]
    fn returns_some_for_v6() {
        assert_eq!(
            pre_v7_backup_path_for(FormatVersion::V6, &p()),
            Some(PathBuf::from("/tmp/board.v6.backup"))
        );
    }

    #[test]
    fn returns_none_for_v7() {
        // V7→V7 is a no-op upstream; should never reach the chain.
        assert_eq!(pre_v7_backup_path_for(FormatVersion::V7, &p()), None);
    }
}
