use tvai_sdk::*;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    println!("Topaz Video AI SDK - Basic Usage Example");
    println!("=========================================");

    // Create a new SDK instance
    let mut sdk = TvaiSdk::new();

    // Example 1: Create templates using presets
    println!("\n1. Creating templates using presets:");
    
    let upscale_template = TemplatePresets::upscale_to_4k()?;
    println!("   Created: {}", upscale_template.name);
    
    let fps_template = TemplatePresets::convert_to_60fps()?;
    println!("   Created: {}", fps_template.name);
    
    let denoise_template = TemplatePresets::remove_noise()?;
    println!("   Created: {}", denoise_template.name);

    // Add templates to SDK
    sdk.add_template(upscale_template.clone())?;
    sdk.add_template(fps_template.clone())?;
    sdk.add_template(denoise_template.clone())?;

    println!("   Total templates loaded: {}", sdk.template_count());

    // Example 2: Generate FFmpeg commands
    println!("\n2. Generating FFmpeg commands:");
    
    let input_file = "input_video.mp4";
    let output_file = "output_video.mp4";
    
    // Basic command generation
    let command = sdk.generate_ffmpeg_command(&upscale_template, input_file, output_file)?;
    println!("   Basic upscale command:");
    println!("   {}", command);
    
    // Command with GPU acceleration
    let gpu_command = sdk.generate_ffmpeg_command_with_gpu(&upscale_template, input_file, "output_gpu.mp4", "0")?;
    println!("\n   GPU accelerated command:");
    println!("   {}", gpu_command);
    
    // Command with custom codec
    let nvenc_command = sdk.generate_ffmpeg_command_with_codec(&upscale_template, input_file, "output_nvenc.mp4", "hevc_nvenc", Some(20))?;
    println!("\n   NVENC H.265 command:");
    println!("   {}", nvenc_command);

    // Example 3: Create custom template using builder
    println!("\n3. Creating custom template:");
    
    let custom_template = TemplateBuilder::new("Custom Enhancement")
        .description("Custom video enhancement with multiple effects")
        .author("Example User")
        .enable_enhancement("prob-4")
        .enhancement_params(25, 15, 5) // denoise=25, detail=15, sharpen=5
        .enable_stabilization(70, 1) // smoothness=70, full frame
        .enable_grain(3, 2) // grain amount=3, size=2
        .output_settings(6, 0.0) // FHD output, original FPS
        .build()?;
    
    println!("   Created custom template: {}", custom_template.name);
    println!("   Description: {}", custom_template.description);
    println!("   Enhancement active: {}", custom_template.settings.enhance.active);
    println!("   Stabilization active: {}", custom_template.settings.stabilize.active);
    println!("   Grain active: {}", custom_template.settings.grain.active);

    // Generate command for custom template
    let custom_command = sdk.generate_ffmpeg_command(&custom_template, input_file, "custom_output.mp4")?;
    println!("\n   Custom template command:");
    println!("   {}", custom_command);

    // Example 4: Batch processing
    println!("\n4. Batch processing:");
    
    let input_files = vec![
        "video1.mp4".to_string(),
        "video2.mp4".to_string(),
        "video3.mp4".to_string(),
        "video4.mp4".to_string(),
    ];
    
    let batch_commands = sdk.generate_batch_commands(&fps_template, &input_files, "./output")?;
    println!("   Generated {} batch commands:", batch_commands.len());
    
    for (i, command) in batch_commands.iter().enumerate() {
        println!("   Batch {}: {}", i + 1, command);
    }

    // Example 5: Template validation
    println!("\n5. Template validation:");
    
    // Validate template for FFmpeg generation
    match sdk.validate_template_for_ffmpeg(&upscale_template) {
        Ok(()) => println!("   Upscale template is valid for FFmpeg generation"),
        Err(e) => println!("   Validation error: {}", e),
    }

    // Example 6: Export and import templates
    println!("\n6. Template export/import:");
    
    // Export all templates to JSON
    let json_export = sdk.export_templates_to_json()?;
    println!("   Exported {} templates to JSON ({} bytes)", sdk.template_count(), json_export.len());
    
    // Clear templates and import them back
    sdk.clear_templates();
    println!("   Cleared all templates. Count: {}", sdk.template_count());
    
    let imported_count = sdk.import_templates_from_json(&json_export)?;
    println!("   Imported {} templates from JSON. Count: {}", imported_count, sdk.template_count());

    // Example 7: Template management
    println!("\n7. Template management:");
    
    // List all template names
    let template_names = sdk.get_template_names();
    println!("   Available templates:");
    for name in template_names {
        println!("     - {}", name);
    }
    
    // Find specific template
    if let Some(template) = sdk.find_template("Upscale to 4K") {
        println!("   Found template: {} (Author: {})", template.name, template.author);
    }
    
    // Check if template exists
    println!("   Has 'Convert to 60 FPS' template: {}", sdk.has_template("Convert to 60 FPS"));
    println!("   Has 'Non-existent' template: {}", sdk.has_template("Non-existent"));

    println!("\n✅ Basic usage example completed successfully!");
    
    Ok(())
}
