//! Integration functions for existing build system

use super::DocumentationGenerator;
use crate::route_analysis::{ApiRouteInfo, ComponentInfo, PageRouteInfo};

/// High-level function to replace existing documentation generation
/// This function can be called from existing build tasks as a drop-in replacement
///
/// # Behavior:
/// - If templates are available: generates beautiful templated documentation
/// - If templates are missing: generates only TOML/JSON data files
/// - If any errors occur: logs warnings but NEVER panics or stops the build
///
/// Returns true if any files were generated, false if complete failure
pub fn generate_site_documentation(
    routes: Vec<ApiRouteInfo>,
    components: Vec<ComponentInfo>,
    pages: Vec<PageRouteInfo>,
    output_dir: &str,
) -> bool {
    let generator = DocumentationGenerator::new();

    if generator.is_available() {
        // Templates available - generate full documentation
        println!("🎨 Using Tera templates for documentation generation");

        generator.generate_all_documentation(
            routes,
            components,
            pages,
            output_dir,
            &["en", "es"], // Default supported languages
        )
    } else {
        // Templates unavailable - generate data files only
        println!("📊 Templates unavailable - generating data files only");

        generator.generate_data_only(routes, components, pages, output_dir)
    }
}

/// Generate documentation for a specific section (server, components, pages)
/// Used by individual build task modules
pub fn generate_section_documentation(
    section_name: &str,
    routes: Vec<ApiRouteInfo>,
    components: Vec<ComponentInfo>,
    pages: Vec<PageRouteInfo>,
    output_dir: &str,
) -> bool {
    println!("📝 Generating {} documentation", section_name);

    // Use the main generation function - it handles all graceful error cases
    generate_site_documentation(routes, components, pages, output_dir)
}

/// Simple wrapper for backward compatibility with existing build tasks
/// This ensures existing calls continue to work without changes
pub fn save_documentation_with_templates(routes: &[ApiRouteInfo], output_dir: &str) -> bool {
    generate_site_documentation(
        routes.to_vec(),
        Vec::new(), // No components for server-only docs
        Vec::new(), // No pages for server-only docs
        output_dir,
    )
}

/// Legacy compatibility function - always succeeds (never panics)
/// Replaces functions that used to return Result<(), Error>
pub fn legacy_save_documentation(
    routes: &[ApiRouteInfo],
    components: &[ComponentInfo],
    pages: &[PageRouteInfo],
    output_dir: &str,
) -> Result<(), Box<dyn std::error::Error>> {
    // Convert to graceful generation
    let success = generate_site_documentation(
        routes.to_vec(),
        components.to_vec(),
        pages.to_vec(),
        output_dir,
    );

    // Always return Ok - graceful degradation means no build failures
    if success {
        println!("✅ Documentation generation completed");
    } else {
        eprintln!("⚠️  Documentation generation completed with warnings");
    }

    Ok(())
}
