use anyhow::Result;
use std::process::{Command, Stdio};
use std::io::BufReader;
use crate::context::ContextSession;

pub struct MooseWatcher {
    session: ContextSession,
}

impl MooseWatcher {
    pub fn new(session: ContextSession) -> Self {
        Self { session }
    }
    
    pub fn is_moose_project() -> bool {
        std::path::Path::new(".moose").exists()
    }

    pub fn spawn_watcher(self) -> Result<()> {
        let mut cmd = Command::new("moose")
            .args(["dev"])
            .stdout(Stdio::piped())
            .spawn()
            .expect("Failed to spawn moose dev command");

        let stdout = cmd.stdout.take().expect("Failed to capture stdout");
        let reader = BufReader::new(stdout);

        self.session.spawn_line_processor(reader, "moose_output".to_string())
    }
} 