#![allow(dead_code)]

//! Common test utilities for integration tests
//!
//! This module provides shared fixtures, helpers, and utilities for
//! integration testing the Mecha10 CLI.

use std::path::PathBuf;
use tempfile::TempDir;

/// Test fixture that creates a temporary project directory
pub struct TestProject {
    /// Temporary directory (kept alive for duration of test)
    _temp_dir: TempDir,
    /// Path to the project root
    pub root: PathBuf,
}

impl TestProject {
    /// Create a new test project in a temporary directory
    pub fn new() -> anyhow::Result<Self> {
        let temp_dir = TempDir::new()?;
        let root = temp_dir.path().to_path_buf();

        Ok(Self {
            _temp_dir: temp_dir,
            root,
        })
    }

    /// Create a file in the test project
    pub fn create_file(&self, path: &str, contents: &str) -> anyhow::Result<PathBuf> {
        let file_path = self.root.join(path);

        // Create parent directories if needed
        if let Some(parent) = file_path.parent() {
            std::fs::create_dir_all(parent)?;
        }

        std::fs::write(&file_path, contents)?;
        Ok(file_path)
    }

    /// Create a directory in the test project
    pub fn create_dir(&self, path: &str) -> anyhow::Result<PathBuf> {
        let dir_path = self.root.join(path);
        std::fs::create_dir_all(&dir_path)?;
        Ok(dir_path)
    }

    /// Get path relative to project root
    pub fn path(&self, relative: &str) -> PathBuf {
        self.root.join(relative)
    }

    /// Check if a file exists in the project
    pub fn file_exists(&self, path: &str) -> bool {
        self.root.join(path).exists()
    }

    /// Read a file from the project
    pub fn read_file(&self, path: &str) -> anyhow::Result<String> {
        Ok(std::fs::read_to_string(self.root.join(path))?)
    }
}

/// Mock Redis service for testing (to be implemented)
pub struct MockRedis {
    // Add fields as needed
}

impl MockRedis {
    pub fn new() -> Self {
        Self {}
    }
}

/// Helper to create a minimal mecha10.json config
pub fn minimal_config() -> serde_json::Value {
    serde_json::json!({
        "name": "test-robot",
        "version": "0.1.0",
        "nodes": [],
        "drivers": [],
        "types": []
    })
}

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

    #[test]
    fn test_project_creation() {
        let project = TestProject::new().unwrap();
        assert!(project.root.exists());
    }

    #[test]
    fn test_file_creation() {
        let project = TestProject::new().unwrap();
        project.create_file("test.txt", "hello").unwrap();
        assert!(project.file_exists("test.txt"));
        assert_eq!(project.read_file("test.txt").unwrap(), "hello");
    }

    #[test]
    fn test_nested_file_creation() {
        let project = TestProject::new().unwrap();
        project.create_file("foo/bar/test.txt", "nested").unwrap();
        assert!(project.file_exists("foo/bar/test.txt"));
    }
}
