//! Complete integration system for Rustelo features
//! Provides seamless integration at all stack levels

pub mod configuration;
pub mod dependency;
pub mod environment;
pub mod infrastructure;
pub mod resource;
pub mod styling;

use std::path::PathBuf;

use anyhow::Result;

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

/// Main integration orchestrator
pub struct IntegrationSystem {
    pub project_root: PathBuf,
    pub features_path: PathBuf,
}

impl IntegrationSystem {
    pub fn new(project_root: impl Into<PathBuf>) -> Self {
        let project_root = project_root.into();
        let features_path = project_root.join("features");

        Self {
            project_root,
            features_path,
        }
    }

    pub fn integrate_feature(&self, manifest: &FeatureManifest) -> Result<()> {
        println!(
            "🔄 Starting complete integration for feature: {}",
            manifest.feature.name
        );

        // Step-by-step integration
        self.integrate_dependencies(manifest)?;
        self.integrate_environment(manifest)?;
        self.integrate_configuration(manifest)?;
        self.integrate_resources(manifest)?;
        self.integrate_node_dependencies(manifest)?;
        self.integrate_styling(manifest)?;
        self.integrate_infrastructure(manifest)?;
        self.integrate_development_tools(manifest)?;

        println!(
            "✅ Complete integration finished for feature: {}",
            manifest.feature.name
        );
        Ok(())
    }

    fn integrate_dependencies(&self, manifest: &FeatureManifest) -> Result<()> {
        let integrator = dependency::DependencyIntegrator::new(&self.project_root);
        integrator.integrate(manifest)
    }

    fn integrate_environment(&self, manifest: &FeatureManifest) -> Result<()> {
        let integrator = environment::EnvironmentIntegrator::new(&self.project_root);
        integrator.integrate(manifest)
    }

    fn integrate_configuration(&self, manifest: &FeatureManifest) -> Result<()> {
        let integrator =
            configuration::ConfigurationIntegrator::new(&self.project_root, &self.features_path);
        integrator.integrate(manifest)
    }

    fn integrate_resources(&self, manifest: &FeatureManifest) -> Result<()> {
        let integrator = resource::ResourceIntegrator::new(&self.project_root, &self.features_path);
        integrator.integrate(manifest)
    }

    fn integrate_node_dependencies(&self, manifest: &FeatureManifest) -> Result<()> {
        let integrator = dependency::NodeIntegrator::new(&self.project_root);
        integrator.integrate(manifest)
    }

    fn integrate_styling(&self, manifest: &FeatureManifest) -> Result<()> {
        let integrator = styling::StylingIntegrator::new(&self.project_root);
        integrator.integrate(manifest)
    }

    fn integrate_infrastructure(&self, manifest: &FeatureManifest) -> Result<()> {
        let integrator = infrastructure::InfrastructureIntegrator::new(&self.project_root);
        integrator.integrate(manifest)
    }

    fn integrate_development_tools(&self, _manifest: &FeatureManifest) -> Result<()> {
        // Integrate scripts, Just commands, git hooks, etc.
        Ok(())
    }
}
