//! Demo of Topaz template system
//! 
//! This example demonstrates how to use the built-in Topaz Video AI templates
//! and load custom templates from JSON files.

use tvai::config::topaz_templates::*;
use std::path::Path;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    println!("=== Topaz Video AI Template System Demo ===\n");

    // Create template manager
    let mut manager = TopazTemplateManager::new();
    
    // List all built-in templates
    println!("Built-in templates:");
    let template_names = manager.list_templates();
    for name in &template_names {
        if let Some(template) = manager.get_template(name) {
            println!("  • {} - {}", template.name, template.description);
        }
    }
    println!("Total: {} templates\n", template_names.len());

    // Demonstrate specific templates
    println!("=== Template Details ===");
    
    // Show 4K upscale template
    if let Some(template) = manager.get_template("upscale_to_4k") {
        println!("Template: {}", template.name);
        println!("Description: {}", template.description);
        println!("Enhancement active: {}", template.settings.enhance.active);
        println!("Enhancement model: {}", template.settings.enhance.model);
        println!("Output size method: {}", template.settings.output.out_size_method);
        println!();
    }

    // Show slow motion template
    if let Some(template) = manager.get_template("4x_slow_motion") {
        println!("Template: {}", template.name);
        println!("Description: {}", template.description);
        println!("Slow motion active: {}", template.settings.slowmo.active);
        println!("Slow motion model: {}", template.settings.slowmo.model);
        println!("Slow motion factor: {}", template.settings.slowmo.factor);
        println!();
    }

    // Export a template to JSON
    println!("=== Template Export ===");
    if let Ok(json) = manager.export_to_json("upscale_to_4k") {
        println!("Exported 'upscale_to_4k' template:");
        println!("{}", &json[..200]); // Show first 200 characters
        println!("...\n");
    }

    // Try to load templates from examples directory if it exists
    let examples_path = Path::new("../../examples");
    if examples_path.exists() {
        println!("=== Loading from Examples Directory ===");
        match manager.load_examples_templates(examples_path) {
            Ok(count) => {
                println!("Successfully loaded {} templates from examples directory", count);
                
                // List all templates again
                let all_templates = manager.list_templates();
                println!("Total templates now: {}", all_templates.len());
                
                // Show some loaded templates
                for name in all_templates.iter().take(5) {
                    if let Some(template) = manager.get_template(name) {
                        println!("  • {} - {}", template.name, template.description);
                    }
                }
            }
            Err(e) => {
                println!("Failed to load from examples directory: {}", e);
            }
        }
    } else {
        println!("Examples directory not found at {:?}", examples_path);
    }

    // Demonstrate template validation
    println!("\n=== Template Validation ===");
    let test_template = BuiltinTemplates::upscale_to_4k();
    match manager.validate_template(&test_template) {
        Ok(()) => println!("✓ Template validation passed"),
        Err(e) => println!("✗ Template validation failed: {}", e),
    }

    // Test invalid template
    let mut invalid_template = test_template.clone();
    invalid_template.settings.enhance.model = "invalid-model".to_string();
    match manager.validate_template(&invalid_template) {
        Ok(()) => println!("✗ Invalid template passed validation (unexpected)"),
        Err(e) => println!("✓ Invalid template correctly rejected: {}", e),
    }

    // Demonstrate template application
    println!("\n=== Template Application ===");
    match manager.apply_template("upscale_to_4k") {
        Ok(applied) => {
            println!("✓ Applied template: {}", applied.name);
            println!("  Description: {}", applied.description);
            println!("  Stabilization enabled: {}", applied.stabilization_enabled);
            println!("  Grain enabled: {}", applied.grain_enabled);

            if let Some(upscale) = &applied.enhance_params.upscale {
                println!("  Upscale settings:");
                println!("    Scale factor: {}", upscale.scale_factor);
                println!("    Model: {:?}", upscale.model);
                println!("    Compression: {}", upscale.compression);
                println!("    Noise reduction: {}", upscale.noise);
                println!("    Detail enhancement: {}", upscale.details);
            }

            if let Some(interpolation) = &applied.enhance_params.interpolation {
                println!("  Interpolation settings:");
                println!("    Multiplier: {}", interpolation.multiplier);
                println!("    Model: {:?}", interpolation.model);
                println!("    Input FPS: {}", interpolation.input_fps);
            }

            if let Some(fps) = applied.output_fps {
                println!("  Output FPS: {}", fps);
            }
        }
        Err(e) => println!("✗ Failed to apply template: {}", e),
    }

    // Test slow motion template
    match manager.apply_template("4x_slow_motion") {
        Ok(applied) => {
            println!("\n✓ Applied slow motion template: {}", applied.name);
            if let Some(interpolation) = &applied.enhance_params.interpolation {
                println!("  Slow motion factor: {}", interpolation.multiplier);
                println!("  Model: {:?}", interpolation.model);
            }
        }
        Err(e) => println!("\n✗ Failed to apply slow motion template: {}", e),
    }

    println!("\n=== Demo Complete ===");
    Ok(())
}
