//! Justfile integration for feature-specific commands

use anyhow::Result;
use std::fs;
use std::path::Path;

use crate::commands::feature::FeatureManifest;

pub struct JustfileIntegrator<'a> {
    project_root: &'a Path,
}

impl<'a> JustfileIntegrator<'a> {
    pub fn new(project_root: &'a Path) -> Self {
        Self { project_root }
    }

    pub fn integrate(&self, manifest: &FeatureManifest) -> Result<()> {
        let justfile = self.project_root.join("justfile");

        if justfile.exists() {
            let content = fs::read_to_string(&justfile)?;
            let feature_section = format!("# {} feature commands", manifest.feature.name);

            if !content.contains(&feature_section) {
                let updated_content = format!("{}

{}
# Add feature commands here", content, feature_section);
                fs::write(&justfile, updated_content)?;
            }
        }

        Ok(())
    }
}
