//! Template utilities for CLI

use rustelo_core::{Result, RusteloError};
use std::collections::HashMap;

/// Template manager for CLI operations
pub struct TemplateManager {
    /// Available templates
    templates: HashMap<String, Template>,
}

/// Template definition
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub struct Template {
    /// Template name
    pub name: String,
    /// Template description
    pub description: String,
    /// Template files
    pub files: Vec<TemplateFile>,
}

/// Template file
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub struct TemplateFile {
    /// Source path in template
    pub source: String,
    /// Target path in implementation
    pub target: String,
    /// File content template
    pub content: String,
}

impl TemplateManager {
    /// Create a new template manager
    pub fn new() -> Self {
        let mut manager = Self {
            templates: HashMap::new(),
        };

        // Register built-in templates
        manager.register_builtin_templates();
        manager
    }

    /// Get available template names
    #[allow(dead_code)]
    pub fn list_templates(&self) -> Vec<String> {
        self.templates.keys().cloned().collect()
    }

    /// Get template by name
    #[allow(dead_code)]
    pub fn get_template(&self, name: &str) -> Option<&Template> {
        self.templates.get(name)
    }

    /// Apply template to create implementation
    #[allow(dead_code)]
    pub fn apply_template(
        &self,
        template_name: &str,
        _context: &HashMap<String, String>,
    ) -> Result<()> {
        let template = self
            .get_template(template_name)
            .ok_or_else(|| RusteloError::generic("Template not found"))?;

        tracing::info!("Applying template: {}", template.name);

        // TODO: Implement template application
        for file in &template.files {
            tracing::debug!("Creating file: {} -> {}", file.source, file.target);
            // Apply context substitution to content
            // Write file to target location
        }

        Ok(())
    }

    fn register_builtin_templates(&mut self) {
        // Basic template
        let basic_template = Template {
            name: "basic".to_string(),
            description: "Basic web application template".to_string(),
            files: vec![
                TemplateFile {
                    source: "Cargo.toml".to_string(),
                    target: "Cargo.toml".to_string(),
                    content: r#"[workspace]
resolver = "2"
members = ["src"]

[workspace.dependencies]
rustelo-core = { path = "../rustelo/crates/rustelo-core" }
rustelo-web = { path = "../rustelo/crates/rustelo-web" }

[[workspace.metadata.leptos]]
name = "{{name}}"
"#
                    .to_string(),
                },
                TemplateFile {
                    source: "src/lib.rs".to_string(),
                    target: "src/lib.rs".to_string(),
                    content: r#"//! {{name}}
//! A Rustelo framework implementation.

use rustelo_core::Result;

pub async fn init() -> Result<()> {
    Ok(())
}
"#
                    .to_string(),
                },
            ],
        };

        self.templates.insert("basic".to_string(), basic_template);
    }
}

impl Default for TemplateManager {
    fn default() -> Self {
        Self::new()
    }
}
