//! Comprehensive Analysis System
//!
//! This module provides deep analysis of server routes, components, and pages
//! with feature detection, build conditions, and automation possibilities.

use std::collections::HashMap;
use std::fs;
use std::path::{Path, PathBuf};

use crate::route_analysis::{ApiRouteInfo, ComponentInfo, PageRouteInfo, RouteDocumentation};
use crate::utils::RouteMetadataAccess;

/// Generate ISO 8601 timestamp
fn format_timestamp() -> String {
    use std::time::{SystemTime, UNIX_EPOCH};

    let now = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default();

    let seconds = now.as_secs();

    // Simple ISO 8601 format: 2025-08-29T21:42:32Z
    format!(
        "{}T{}Z",
        format_date_from_secs(seconds),
        format_time_from_secs(seconds)
    )
}

fn format_date_from_secs(timestamp: u64) -> String {
    // Simplified date calculation (approximate)
    let days_since_epoch = timestamp / (24 * 3600);
    let year = 1970 + (days_since_epoch / 365);
    let remaining_days = days_since_epoch % 365;
    let month = 1 + (remaining_days / 30).min(11);
    let day = 1 + (remaining_days % 30);
    format!("{:04}-{:02}-{:02}", year, month, day)
}

fn format_time_from_secs(timestamp: u64) -> String {
    let seconds_today = timestamp % (24 * 3600);
    let hours = seconds_today / 3600;
    let minutes = (seconds_today % 3600) / 60;
    let seconds = seconds_today % 60;
    format!("{:02}:{:02}:{:02}", hours, minutes, seconds)
}

/// Generate comprehensive documentation with new organized structure
pub fn generate_comprehensive_documentation(
    content_root: &str,
    codegen_path: &str,
    project_root: &str,
) -> Result<(), Box<dyn std::error::Error>> {
    // Use manifest resolver to get paths
    let resolver = rustelo_utils::ManifestResolver::load()?;
    let project_root_path = resolver.root();

    // Create data directory structure - use site/info instead of devtools
    let data_dir = project_root_path.join("site").join("info");
    fs::create_dir_all(data_dir.join("server/routes"))?;
    fs::create_dir_all(data_dir.join("components"))?;
    fs::create_dir_all(data_dir.join("pages/routes"))?;
    fs::create_dir_all(data_dir.join("automation"))?;

    // Also maintain original codegen structure for compatibility
    let codegen_dir = Path::new(codegen_path);
    fs::create_dir_all(codegen_dir.join("server"))?;
    fs::create_dir_all(codegen_dir.join("components"))?;
    fs::create_dir_all(codegen_dir.join("pages"))?;
    fs::create_dir_all(codegen_dir.join("automation"))?;

    // Generate server documentation
    generate_server_documentation(project_root, codegen_path)?;

    // Generate components documentation
    generate_components_documentation(project_root, codegen_path)?;

    // Generate pages documentation
    generate_pages_documentation(project_root, content_root, codegen_path)?;

    // Generate automation documentation
    generate_automation_documentation(codegen_path)?;

    // Generate top-level comprehensive summary
    generate_top_level_summary(project_root, codegen_path)?;

    Ok(())
}

/// Generate comprehensive server documentation
pub fn generate_server_documentation(
    project_root: &str,
    codegen_path: &str,
) -> Result<(), Box<dyn std::error::Error>> {
    let server_path = Path::new(project_root).join("crates/server");
    let output_dir = Path::new(codegen_path).join("server");

    let mut documentation = RouteDocumentation::new();

    // Analyze main routes
    if let Ok(routes) = analyze_main_routes(&server_path) {
        documentation.api_routes.extend(routes);
    }

    // Deep dive into startup routes
    if let Ok(startup_routes) = analyze_startup_routes(&server_path) {
        documentation.api_routes.extend(startup_routes);
    }

    // Analyze static routes and paths
    if let Ok(static_routes) = analyze_static_routes(&server_path) {
        documentation.api_routes.extend(static_routes);
    }

    // Save with new structure
    save_server_documentation(&documentation, &output_dir)?;

    Ok(())
}

/// Generate comprehensive components documentation
pub fn generate_components_documentation(
    project_root: &str,
    codegen_path: &str,
) -> Result<(), Box<dyn std::error::Error>> {
    let components_path = Path::new(project_root).join("crates/components");
    let output_dir = Path::new(codegen_path).join("components");

    let mut documentation = RouteDocumentation::new();

    // Deep dive into all component subdirectories
    if let Ok(components) = analyze_all_components(&components_path) {
        documentation.components.extend(components);
    }

    // Save with new structure
    save_components_documentation(&documentation, &output_dir)?;

    Ok(())
}

/// Generate comprehensive pages documentation
pub fn generate_pages_documentation(
    project_root: &str,
    content_root: &str,
    codegen_path: &str,
) -> Result<(), Box<dyn std::error::Error>> {
    let pages_path = Path::new(project_root).join("crates/pages");
    let output_dir = Path::new(codegen_path).join("pages");

    let mut documentation = RouteDocumentation::new();

    // Analyze pages with source code
    if let Ok(pages) = analyze_pages_with_source(&pages_path) {
        documentation.components.extend(pages);
    }

    // Analyze route configurations
    if let Ok(routes) = analyze_page_routes(content_root) {
        documentation.page_routes.extend(routes);
    }

    // Analyze FTL files and active status
    if let Ok(ftl_info) = analyze_ftl_files(content_root) {
        // Add FTL information to page routes
        enhance_routes_with_ftl(&mut documentation.page_routes, ftl_info);
    }

    // Save with new structure
    save_pages_documentation(&documentation, &output_dir)?;

    Ok(())
}

/// Generate automation documentation with creative examples
pub fn generate_automation_documentation(
    codegen_path: &str,
) -> Result<(), Box<dyn std::error::Error>> {
    let automation_dir = Path::new(codegen_path).join("automation");

    let automation_content = generate_creative_automation_guide();
    fs::write(automation_dir.join("reference.md"), automation_content)?;

    let automation_examples = generate_automation_examples_toml();
    fs::write(automation_dir.join("data.toml"), &automation_examples)?;

    // Also save as JSON for machine processing - convert TOML to JSON
    let toml_data: toml::Value = toml::from_str(&automation_examples)?;
    let automation_data: serde_json::Value = serde_json::to_value(toml_data)?;
    let json_content = serde_json::to_string_pretty(&automation_data)?;
    fs::write(automation_dir.join("data.json"), json_content)?;

    let automation_summary = generate_automation_summary();
    fs::write(automation_dir.join("summary.md"), automation_summary)?;

    Ok(())
}

/// Analyze startup routes with feature detection
fn analyze_startup_routes(
    server_path: &Path,
) -> Result<Vec<ApiRouteInfo>, Box<dyn std::error::Error>> {
    let mut routes = Vec::new();
    let startup_dir = server_path.join("src/startup");

    if !startup_dir.exists() {
        return Ok(routes);
    }

    // Analyze each startup module
    for entry in fs::read_dir(&startup_dir)? {
        let entry = entry?;
        let path = entry.path();

        if path.is_file() && path.extension().is_some_and(|ext| ext == "rs") {
            let module_routes = analyze_startup_module(&path)?;
            routes.extend(module_routes);
        }
    }

    Ok(routes)
}

/// Analyze a startup module for routes and feature requirements
fn analyze_startup_module(
    module_path: &Path,
) -> Result<Vec<ApiRouteInfo>, Box<dyn std::error::Error>> {
    let content = fs::read_to_string(module_path)?;
    let mut routes = Vec::new();

    // Split content into lines for line number calculation
    let _lines: Vec<&str> = content.lines().collect();

    let module_name = module_path
        .file_stem()
        .and_then(|s| s.to_str())
        .unwrap_or("unknown");

    // Look for route definitions
    let route_regex =
        regex::Regex::new(r#"\.route\s*\(\s*"([^"]+)"\s*,\s*(\w+)\s*\(\s*([^)]+)\s*\)\s*\)"#)?;

    for cap in route_regex.captures_iter(&content) {
        if let (Some(path), Some(method), Some(handler)) = (cap.get(1), cap.get(2), cap.get(3)) {
            let _features = detect_features_from_content(&content);
            let _build_conditions = detect_build_conditions(&content);

            // Find line number for this match
            let line_number = content[..cap.get(0).unwrap().start()].matches('\n').count() + 1;

            let src_ref = Some(crate::route_analysis::SourceReference {
                path: crate::route_analysis::make_relative_path(module_path),
                line: line_number,
                context: "startup route definition".to_string(),
            });

            let route = ApiRouteInfo {
                path: path.as_str().to_string(),
                methods: vec![method.as_str().to_uppercase()],
                handler: handler.as_str().to_string(),
                module: format!("crates/server/src/startup/{}.rs", module_name),
                parameters: crate::route_analysis::extract_path_parameters(path.as_str()),
                response_type: "Unknown".to_string(),
                requires_auth: detect_auth_requirement(&content),
                middleware: detect_middleware(&content),
                description: Some(format!("Startup module: {}", module_name)),
                src_ref,
            };

            routes.push(route);
        }
    }

    Ok(routes)
}

/// Analyze main routes file
fn analyze_main_routes(
    server_path: &Path,
) -> Result<Vec<ApiRouteInfo>, Box<dyn std::error::Error>> {
    let routes_file = server_path.join("src/routes.rs");
    if !routes_file.exists() {
        return Ok(Vec::new());
    }

    crate::build::build_tasks::server_route_analysis::analyze_routes_file(&routes_file)
}

/// Analyze static routes and file serving
fn analyze_static_routes(
    server_path: &Path,
) -> Result<Vec<ApiRouteInfo>, Box<dyn std::error::Error>> {
    let mut routes = Vec::new();

    // Look for static file serving configurations
    let files_to_check = [
        "src/main.rs",
        "src/lib.rs",
        "src/startup/server.rs",
        "src/startup/router.rs",
    ];

    for file_path in &files_to_check {
        let full_path = server_path.join(file_path);
        if full_path.exists() {
            let static_routes = extract_static_routes(&full_path)?;
            routes.extend(static_routes);
        }
    }

    Ok(routes)
}

/// Extract static routes from server configuration
fn extract_static_routes(
    file_path: &Path,
) -> Result<Vec<ApiRouteInfo>, Box<dyn std::error::Error>> {
    let content = fs::read_to_string(file_path)?;
    let mut routes = Vec::new();

    // Split content into lines for line number calculation
    let _lines: Vec<&str> = content.lines().collect();

    // Look for static file serving patterns
    let static_patterns = [
        (r#"nest_service\s*\(\s*["']([^"']+)["']"#, "Static files"),
        (r#"serve_dir\s*\(\s*["']([^"']+)["']"#, "Directory serving"),
        (
            r#"ServeDir::new\s*\(\s*["']([^"']+)["']"#,
            "Static directory",
        ),
    ];

    for (pattern, description) in &static_patterns {
        let regex = regex::Regex::new(pattern)?;
        for cap in regex.captures_iter(&content) {
            if let Some(path_match) = cap.get(1) {
                // Find line number for this match
                let line_number = content[..cap.get(0).unwrap().start()].matches('\n').count() + 1;

                let src_ref = Some(crate::route_analysis::SourceReference {
                    path: crate::route_analysis::make_relative_path(file_path),
                    line: line_number,
                    context: "static route definition".to_string(),
                });

                routes.push(ApiRouteInfo {
                    path: path_match.as_str().to_string(),
                    methods: vec!["GET".to_string()],
                    handler: "static_files".to_string(),
                    module: file_path.to_string_lossy().to_string(),
                    parameters: vec![],
                    response_type: "Static Files".to_string(),
                    requires_auth: false,
                    middleware: vec!["static".to_string()],
                    description: Some(description.to_string()),
                    src_ref,
                });
            }
        }
    }

    Ok(routes)
}

/// Analyze all components with subdirectory traversal
fn analyze_all_components(
    components_path: &Path,
) -> Result<Vec<ComponentInfo>, Box<dyn std::error::Error>> {
    let mut components = Vec::new();

    // Recursively walk all component directories
    for entry in walkdir::WalkDir::new(components_path) {
        let entry = entry?;
        let path = entry.path();

        if path.is_file() && path.extension().is_some_and(|ext| ext == "rs") {
            let file_components =
                crate::build::build_tasks::component_analysis::analyze_component_file(
                    path,
                    components_path,
                )?;
            components.extend(file_components);
        }
    }

    Ok(components)
}

/// Analyze pages with source code tracking
fn analyze_pages_with_source(
    pages_path: &Path,
) -> Result<Vec<ComponentInfo>, Box<dyn std::error::Error>> {
    let mut pages = Vec::new();

    // Walk through pages directory
    for entry in walkdir::WalkDir::new(pages_path) {
        let entry = entry?;
        let path = entry.path();

        if path.is_file() && path.extension().is_some_and(|ext| ext == "rs") {
            let page_components =
                crate::build::build_tasks::component_analysis::analyze_component_file(
                    path, pages_path,
                )?;
            pages.extend(page_components);
        }
    }

    Ok(pages)
}

/// Analyze page routes from configuration
fn analyze_page_routes(
    content_root: &str,
) -> Result<Vec<PageRouteInfo>, Box<dyn std::error::Error>> {
    // Routes are in site/config/routes, not site/content/config/routes
    let content_path = Path::new(content_root);
    let site_root = content_path.parent().unwrap_or(content_path);
    let routes_dir = site_root.join("config/routes");

    crate::build::build_tasks::client_route_analysis::load_all_route_configs(&routes_dir)
}

/// Analyze FTL files and their relationship to pages
fn analyze_ftl_files(
    content_root: &str,
) -> Result<HashMap<String, Vec<String>>, Box<dyn std::error::Error>> {
    let mut ftl_info = HashMap::new();

    // Look for FTL files in content directory
    let content_path = Path::new(content_root);

    // Search for .ftl files
    for entry in walkdir::WalkDir::new(content_path) {
        let entry = entry?;
        let path = entry.path();

        if path.extension().is_some_and(|ext| ext == "ftl") {
            if let Some(filename) = path.file_stem().and_then(|s| s.to_str()) {
                let content = fs::read_to_string(path).unwrap_or_default();
                let keys = extract_ftl_keys(&content);
                ftl_info.insert(filename.to_string(), keys);
            }
        }
    }

    Ok(ftl_info)
}

/// Extract FTL keys from content
fn extract_ftl_keys(content: &str) -> Vec<String> {
    let mut keys = Vec::new();
    let key_regex = regex::Regex::new(r"^([a-zA-Z][a-zA-Z0-9_-]*)\s*=").unwrap();

    for line in content.lines() {
        if let Some(cap) = key_regex.captures(line.trim()) {
            if let Some(key) = cap.get(1) {
                keys.push(key.as_str().to_string());
            }
        }
    }

    keys
}

/// Enhance routes with FTL information
fn enhance_routes_with_ftl(routes: &mut [PageRouteInfo], ftl_info: HashMap<String, Vec<String>>) {
    for route in routes {
        // Check if route has associated FTL keys
        if let Some(keys) = ftl_info.get(&route.get_language().unwrap_or_default()) {
            // Add FTL information to route props
            route.props.insert(
                "ftl_keys".to_string(),
                toml::Value::Array(
                    keys.iter()
                        .map(|k| toml::Value::String(k.clone()))
                        .collect(),
                ),
            );
        }
    }
}

/// Save server documentation using template system
fn save_server_documentation(
    documentation: &RouteDocumentation,
    output_dir: &Path,
) -> Result<(), Box<dyn std::error::Error>> {
    // Create routes subdirectory for server route data
    let routes_dir = output_dir.join("routes");
    std::fs::create_dir_all(&routes_dir)?;

    // Generate dual-format server route data
    generate_server_routes_data_files(documentation, &routes_dir)?;

    // Use the new template integration system
    let success = crate::templates::integration::generate_section_documentation(
        "server",
        documentation.api_routes.clone(),
        Vec::new(), // No components for server documentation
        Vec::new(), // No page routes for server documentation
        &output_dir.to_string_lossy(),
    );

    if success {
        println!("✅ Server documentation generated with Tera templates");
    } else {
        eprintln!("⚠️  Server documentation generated with fallback method");
    }

    Ok(())
}

/// Generate server routes data files
fn generate_server_routes_data_files(
    documentation: &RouteDocumentation,
    routes_dir: &Path,
) -> Result<(), Box<dyn std::error::Error>> {
    // Also generate in SITE_DEVTOOLS_PATH/data structure
    if let Ok(devtools_path) = std::env::var("SITE_DEVTOOLS_PATH") {
        // Ensure path is relative to workspace root
        let resolver = rustelo_utils::ManifestResolver::load()?;
        let devtools_routes_dir = resolver
            .root()
            .join(&devtools_path)
            .join("data/server/routes");
        fs::create_dir_all(&devtools_routes_dir)?;
        generate_server_routes_data_files_impl(documentation, &devtools_routes_dir, true)?;
    }

    // Generate in original location for compatibility
    generate_server_routes_data_files_impl(documentation, routes_dir, false)
}

fn generate_server_routes_data_files_impl(
    documentation: &RouteDocumentation,
    routes_dir: &Path,
    create_info_file: bool,
) -> Result<(), Box<dyn std::error::Error>> {
    let server_document = ServerRoutesDataDocument {
        generated_at: format_timestamp(),
        total_routes: documentation.api_routes.len(),
        api_routes: documentation.api_routes.clone(),
        metadata: ServerRoutesMetadata {
            methods: extract_http_methods(&documentation.api_routes),
            modules: extract_modules(&documentation.api_routes),
            auth_required_count: documentation
                .api_routes
                .iter()
                .filter(|r| r.requires_auth)
                .count(),
        },
    };

    // Generate JSON format
    let json_content = serde_json::to_string_pretty(&server_document)?;
    std::fs::write(routes_dir.join("data.json"), json_content)?;

    // Generate TOML format
    let toml_content = toml::to_string_pretty(&server_document)?;
    std::fs::write(routes_dir.join("data.toml"), toml_content)?;

    // Generate info file with timestamp if requested
    if create_info_file {
        let info_content = format!(
            "Generated at: {}\nTotal API routes: {}\nData files: data.json, data.toml\nSource: \
             build-tools server analysis\nAuth required routes: {}\n",
            server_document.generated_at,
            server_document.total_routes,
            server_document.metadata.auth_required_count
        );
        std::fs::write(routes_dir.join("info.txt"), info_content)?;
        println!(
            "✅ Generated server routes data.json, data.toml, and info.txt in {}/",
            routes_dir.display()
        );
    } else {
        println!("✅ Generated server routes data.json and data.toml");
    }

    Ok(())
}

/// Document structure for server routes data files
#[derive(serde::Serialize, serde::Deserialize)]
struct ServerRoutesDataDocument {
    generated_at: String,
    total_routes: usize,
    api_routes: Vec<ApiRouteInfo>,
    metadata: ServerRoutesMetadata,
}

/// Metadata about server routes
#[derive(serde::Serialize, serde::Deserialize)]
struct ServerRoutesMetadata {
    methods: Vec<String>,
    modules: Vec<String>,
    auth_required_count: usize,
}

/// Extract HTTP methods from API routes
fn extract_http_methods(routes: &[ApiRouteInfo]) -> Vec<String> {
    let mut methods: Vec<String> = routes
        .iter()
        .flat_map(|r| r.methods.iter().cloned())
        .collect::<std::collections::HashSet<_>>()
        .into_iter()
        .collect();
    methods.sort();
    methods
}

/// Extract modules from API routes
fn extract_modules(routes: &[ApiRouteInfo]) -> Vec<String> {
    let mut modules: Vec<String> = routes
        .iter()
        .map(|r| r.module.clone())
        .collect::<std::collections::HashSet<_>>()
        .into_iter()
        .collect();
    modules.sort();
    modules
}

/// Save components documentation using template system
fn save_components_documentation(
    documentation: &RouteDocumentation,
    output_dir: &Path,
) -> Result<(), Box<dyn std::error::Error>> {
    // Create routes subdirectory for components data
    let routes_dir = output_dir.join("routes");
    std::fs::create_dir_all(&routes_dir)?;

    // Generate dual-format components data
    generate_components_data_files(documentation, &routes_dir)?;

    // Use the new template integration system
    let success = crate::templates::integration::generate_section_documentation(
        "components",
        Vec::new(), // No server routes for components documentation
        documentation.components.clone(),
        Vec::new(), // No page routes for components documentation
        &output_dir.to_string_lossy(),
    );

    if success {
        println!("✅ Components documentation generated with Tera templates");
    } else {
        eprintln!("⚠️  Components documentation generated with fallback method");
    }

    Ok(())
}

/// Generate components data files
fn generate_components_data_files(
    documentation: &RouteDocumentation,
    routes_dir: &Path,
) -> Result<(), Box<dyn std::error::Error>> {
    let components_document = ComponentsDataDocument {
        generated_at: format_timestamp(),
        total_components: documentation.components.len(),
        components: documentation.components.clone(),
        metadata: ComponentsMetadata {
            component_types: extract_component_types(&documentation.components),
            modules: extract_component_modules(&documentation.components),
            with_props_count: documentation
                .components
                .iter()
                .filter(|c| !c.props.is_empty())
                .count(),
        },
    };

    // Generate JSON format
    let json_content = serde_json::to_string_pretty(&components_document)?;
    std::fs::write(routes_dir.join("data.json"), json_content)?;

    // Generate TOML format
    let toml_content = toml::to_string_pretty(&components_document)?;
    std::fs::write(routes_dir.join("data.toml"), toml_content)?;

    println!("✅ Generated components data.json and data.toml");

    Ok(())
}

/// Document structure for components data files
#[derive(serde::Serialize, serde::Deserialize)]
struct ComponentsDataDocument {
    generated_at: String,
    total_components: usize,
    components: Vec<ComponentInfo>,
    metadata: ComponentsMetadata,
}

/// Metadata about components
#[derive(serde::Serialize, serde::Deserialize)]
struct ComponentsMetadata {
    component_types: Vec<String>,
    modules: Vec<String>,
    with_props_count: usize,
}

/// Extract component types
fn extract_component_types(components: &[ComponentInfo]) -> Vec<String> {
    let mut types: Vec<String> = components
        .iter()
        .map(|c| c.component_type.clone())
        .collect::<std::collections::HashSet<_>>()
        .into_iter()
        .collect();
    types.sort();
    types
}

/// Extract component modules
fn extract_component_modules(components: &[ComponentInfo]) -> Vec<String> {
    let mut modules: Vec<String> = components
        .iter()
        .map(|c| c.module_path.clone())
        .collect::<std::collections::HashSet<_>>()
        .into_iter()
        .collect();
    modules.sort();
    modules
}

/// Save pages documentation using template system
fn save_pages_documentation(
    documentation: &RouteDocumentation,
    output_dir: &Path,
) -> Result<(), Box<dyn std::error::Error>> {
    // Create routes subdirectory
    let routes_dir = output_dir.join("routes");
    std::fs::create_dir_all(&routes_dir)?;

    // Generate dual-format route data for manager consumption
    generate_routes_data_files(documentation, &routes_dir)?;

    // Use the new template integration system for documentation
    let success = crate::templates::integration::generate_section_documentation(
        "pages",
        Vec::new(),                        // No server routes for pages documentation
        documentation.components.clone(),  // Page components
        documentation.page_routes.clone(), // Page routes
        &output_dir.to_string_lossy(),
    );

    if success {
        println!("✅ Pages documentation generated with Tera templates");
    } else {
        eprintln!("⚠️  Pages documentation generated with fallback method");
    }

    Ok(())
}

/// Generate data.json and data.toml files for manager consumption with info
/// tracking
fn generate_routes_data_files(
    documentation: &RouteDocumentation,
    routes_dir: &Path,
) -> Result<(), Box<dyn std::error::Error>> {
    // Also generate in SITE_DEVTOOLS_PATH/data structure
    if let Ok(devtools_path) = std::env::var("SITE_DEVTOOLS_PATH") {
        // Ensure path is relative to workspace root
        let resolver = rustelo_utils::ManifestResolver::load()?;
        let devtools_routes_dir = resolver
            .root()
            .join(&devtools_path)
            .join("data/pages/routes");
        fs::create_dir_all(&devtools_routes_dir)?;
        generate_routes_data_files_impl(documentation, &devtools_routes_dir, true)?;
    }

    // Generate in original location for compatibility
    generate_routes_data_files_impl(documentation, routes_dir, false)
}

/// Implementation of route data file generation
fn generate_routes_data_files_impl(
    documentation: &RouteDocumentation,
    routes_dir: &Path,
    create_info_file: bool,
) -> Result<(), Box<dyn std::error::Error>> {
    // Create consolidated route document
    let routes_document = RoutesDataDocument {
        generated_at: format_timestamp(),
        total_routes: documentation.page_routes.len(),
        routes: documentation.page_routes.clone(),
        metadata: RoutesMetadata {
            languages: extract_languages(&documentation.page_routes),
            components: extract_unique_components(&documentation.page_routes),
            menu_groups: extract_menu_groups(&documentation.page_routes),
        },
    };

    // Generate JSON format
    let json_content = serde_json::to_string_pretty(&routes_document)?;
    std::fs::write(routes_dir.join("data.json"), json_content)?;

    // Generate TOML format
    let toml_content = toml::to_string_pretty(&routes_document)?;
    std::fs::write(routes_dir.join("data.toml"), toml_content)?;

    // Generate info file with timestamp if requested
    if create_info_file {
        let info_content = format!(
            "Generated at: {}\nTotal routes: {}\nData files: data.json, data.toml\nSource: \
             build-tools comprehensive analysis\n",
            routes_document.generated_at, routes_document.total_routes
        );
        std::fs::write(routes_dir.join("info.txt"), info_content)?;
        println!(
            "✅ Generated data.json, data.toml, and info.txt in {}/",
            routes_dir.display()
        );
    } else {
        println!(
            "✅ Generated data.json and data.toml in {}/",
            routes_dir.display()
        );
    }

    Ok(())
}

/// Document structure for routes data files
#[derive(serde::Serialize, serde::Deserialize)]
struct RoutesDataDocument {
    generated_at: String,
    total_routes: usize,
    routes: Vec<PageRouteInfo>,
    metadata: RoutesMetadata,
}

/// Metadata about the routes collection
#[derive(serde::Serialize, serde::Deserialize)]
struct RoutesMetadata {
    languages: Vec<String>,
    components: Vec<String>,
    menu_groups: Vec<String>,
}

/// Extract unique languages from routes
fn extract_languages(routes: &[PageRouteInfo]) -> Vec<String> {
    let mut languages: Vec<String> = routes
        .iter()
        .map(|r| r.get_language().unwrap_or_default())
        .collect::<std::collections::HashSet<_>>()
        .into_iter()
        .collect();
    languages.sort();
    languages
}

/// Extract unique components from routes
fn extract_unique_components(routes: &[PageRouteInfo]) -> Vec<String> {
    let mut components: Vec<String> = routes
        .iter()
        .map(|r| r.get_unified_component().unwrap_or_default())
        .collect::<std::collections::HashSet<_>>()
        .into_iter()
        .collect();
    components.sort();
    components
}

/// Extract unique menu groups from routes
fn extract_menu_groups(routes: &[PageRouteInfo]) -> Vec<String> {
    let mut groups: Vec<String> = routes
        .iter()
        .filter_map(|r| r.get_menu_group())
        .collect::<std::collections::HashSet<_>>()
        .into_iter()
        .collect();
    groups.sort();
    groups
}

/// Combined pages document structure
#[derive(serde::Serialize, serde::Deserialize)]
struct PagesDocument {
    generated_at: String,
    page_routes: Vec<PageRouteInfo>,
    page_components: Vec<ComponentInfo>,
}

// Helper functions for detecting features and conditions
fn detect_features_from_content(content: &str) -> Vec<String> {
    let mut features = Vec::new();
    let feature_patterns = [
        (r#"#\[cfg\(feature\s*=\s*"([^"]+)"\)\]"#, "feature"),
        (r"use.*auth", "auth"),
        (r"use.*content", "content"),
        (r"use.*rbac", "rbac"),
    ];

    for (pattern, feature) in &feature_patterns {
        let regex = regex::Regex::new(pattern).unwrap();
        if regex.is_match(content) {
            features.push(feature.to_string());
        }
    }

    features
}

fn detect_build_conditions(content: &str) -> Vec<String> {
    let mut conditions = Vec::new();

    if content.contains("#[cfg(debug_assertions)]") {
        conditions.push("debug".to_string());
    }
    if content.contains("#[cfg(not(debug_assertions))]") {
        conditions.push("release".to_string());
    }
    if content.contains("cfg(target_arch") {
        conditions.push("target_arch".to_string());
    }

    conditions
}

fn detect_auth_requirement(content: &str) -> bool {
    content.contains("RequireAuth")
        || content.contains("AuthLayer")
        || content.contains("jwt")
        || content.contains("session")
}

fn detect_middleware(content: &str) -> Vec<String> {
    let mut middleware = Vec::new();

    let middleware_patterns = [
        ("cors", "cors"),
        ("auth", "auth"),
        ("rate_limit", "rate_limit"),
        ("csrf", "csrf"),
        ("compression", "compression"),
    ];

    for (pattern, name) in &middleware_patterns {
        if content.to_lowercase().contains(pattern) {
            middleware.push(name.to_string());
        }
    }

    middleware
}

// Summary table creation functions
// OLD: Replaced by Tera template system
fn _unused_create_server_summary_table(routes: &[ApiRouteInfo]) -> String {
    let mut summary = String::from("# Server Routes Summary\n\n");
    summary.push_str("| Path | Methods | Handler | Module | Auth | Features |\n");
    summary.push_str("|------|---------|---------|---------|------|----------|\n");

    for route in routes {
        let methods = route.methods.join(", ");
        let auth = if route.requires_auth { "✅" } else { "❌" };
        let features = route.middleware.join(", ");

        summary.push_str(&format!(
            "| {} | {} | {} | {} | {} | {} |\n",
            route.path,
            methods,
            route.handler,
            route.module.split('/').next_back().unwrap_or(&route.module),
            auth,
            features
        ));
    }

    summary.push_str(&format!("\n**Total Routes:** {}\n", routes.len()));
    summary
}

/// Generate top-level comprehensive summary using template system
pub fn generate_top_level_summary(
    _project_root: &str,
    info_path: &str,
) -> Result<(), Box<dyn std::error::Error>> {
    // Load all documentation data for comprehensive summary
    let mut all_routes = Vec::new();
    let mut all_components = Vec::new();
    let mut all_pages = Vec::new();

    // Try to load existing documentation data
    let info_dir = Path::new(info_path);
    if let Ok(server_data) = load_server_routes(&info_dir.join("server/data.toml")) {
        all_routes.extend(server_data);
    }
    if let Ok(component_data) = load_components(&info_dir.join("components/data.toml")) {
        all_components.extend(component_data);
    }
    if let Ok(page_data) = load_pages(&info_dir.join("pages/data.toml")) {
        all_pages.extend(page_data);
    }

    // Use template system for top-level summary
    let success = crate::templates::integration::generate_section_documentation(
        "summary", // Special section name for top-level
        all_routes,
        all_components,
        all_pages,
        info_path,
    );

    if success {
        println!("✅ Top-level summary generated with Tera templates");
    } else {
        println!("✅ Top-level summary generated with fallback data");
    }

    Ok(())
}

/// Load server routes from data.toml file
fn load_server_routes(data_path: &Path) -> Result<Vec<ApiRouteInfo>, Box<dyn std::error::Error>> {
    if !data_path.exists() {
        return Ok(Vec::new());
    }
    let content = fs::read_to_string(data_path)?;
    let parsed: crate::route_analysis::ServerRoutesDocument = toml::from_str(&content)?;
    Ok(parsed.api_routes)
}

/// Load components from data.toml file
fn load_components(data_path: &Path) -> Result<Vec<ComponentInfo>, Box<dyn std::error::Error>> {
    if !data_path.exists() {
        return Ok(Vec::new());
    }
    let content = fs::read_to_string(data_path)?;
    let parsed: crate::route_analysis::ComponentsDocument = toml::from_str(&content)?;
    Ok(parsed.components)
}

/// Load pages from data.toml file
fn load_pages(data_path: &Path) -> Result<Vec<PageRouteInfo>, Box<dyn std::error::Error>> {
    if !data_path.exists() {
        return Ok(Vec::new());
    }
    let content = fs::read_to_string(data_path)?;
    let parsed: PagesDocument = toml::from_str(&content)?;
    Ok(parsed.page_routes)
}

/// Generate creative automation guide content
fn generate_creative_automation_guide() -> String {
    format!(
        r#"# Advanced Automation with Rustelo Route Documentation

Generated at: {}

## Overview

The Rustelo route documentation system generates comprehensive metadata that powers numerous automation scenarios beyond basic API client generation.

## 🚀 Automation Possibilities

### 1. Rustelo Manager Integration
- **Route Discovery**: Automatically populates available routes for navigation
- **Component Inspector**: Live component documentation and prop validation
- **Page Editor**: Dynamic form generation based on page route configurations

### 2. MCP Server Integration
- **Route Endpoints**: Expose API routes as MCP tools
- **Component Library**: Provide component templates for AI-assisted development
- **Documentation Context**: Feed route information to AI for better code understanding

### 3. CI/CD Pipeline Automation
- **Route Testing**: Generate test cases for all documented API endpoints
- **Component Testing**: Validate component props and interfaces
- **Documentation Drift**: Detect when routes change without documentation updates

### 4. API Client Generation
- **TypeScript/JavaScript**: Generate fetch-based API clients
- **Rust**: Generate reqwest-based client libraries
- **OpenAPI**: Convert to OpenAPI 3.0 specifications

*This automation guide is generated from your actual route definitions and stays in sync with your codebase.*
"#,
        chrono::Utc::now().to_rfc3339()
    )
}

/// Generate automation examples TOML configuration
fn generate_automation_examples_toml() -> String {
    format!(
        r#"# Automation Examples Configuration
generated_at = "{}"

[mcp_server]
enabled = true
port = 8080

[api_clients]
typescript = {{ enabled = true, output = "clients/typescript/" }}
rust = {{ enabled = true, output = "clients/rust/" }}

[monitoring]
enabled = true
health_check_interval = "30s"

[rustelo_manager]
integration = true
auto_sync = true
"#,
        chrono::Utc::now().to_rfc3339()
    )
}

/// Generate automation summary content
fn generate_automation_summary() -> String {
    format!(
        r#"# Automation Capabilities Summary

Generated at: {}

## 🎯 Core Integrations

| System | Status | Description |
|--------|--------|-------------|
| Rustelo Manager | ✅ Active | Dashboard integration with live route data |
| MCP Server | 🚧 Ready | Transform routes into MCP tools for AI |
| CI/CD Pipelines | ✅ Active | Automated testing and validation |
| API Clients | ✅ Ready | Multi-language client generation |

## 📊 Coverage Statistics

- **Server Routes**: Auto-discovered from startup modules
- **Components**: Full component tree with props and dependencies
- **Pages**: Route configurations enhanced with FTL localization data

## 🚀 Quick Start Commands

```bash
# Generate all documentation
just build-tools

# Validate routes
just docs-validate

# Sync with manager
just manager-sync
```

*This summary reflects the current state of automation capabilities in your Rustelo application.*
"#,
        chrono::Utc::now().to_rfc3339()
    )
}
