use elph_agent::write_file_if_missing;

use super::paths::Paths;
use anyhow::Result;

const GITIGNORE: &str = "\
# Auto-generated by Elph agent -- DO NOT REMOVE!!
# memory.db holds project-local agent memory (floppy).
*
";

pub fn ensure(paths: &Paths) -> Result<()> {
    std::fs::create_dir_all(paths.project_elph_dir())?;
    write_file_if_missing(&paths.project_gitignore_path(), GITIGNORE.as_bytes())?;
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::runtime::paths::Paths;

    #[test]
    fn writes_project_gitignore() {
        let tmp = tempfile::tempdir().expect("tempdir");
        let project = tmp.path().join("repo");
        let paths = Paths::from_dirs(tmp.path().join("config"), tmp.path().join("data"), project);

        ensure(&paths).expect("ensure project home");

        let gitignore = paths.project_gitignore_path();
        assert!(gitignore.exists());
        assert_eq!(std::fs::read_to_string(gitignore).expect("read gitignore"), GITIGNORE);
    }
}
