use tvai_sdk::*;

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

    let sdk = TvaiSdk::new();

    // Example 1: AAC Audio (Most Common)
    println!("\n1. AAC Audio Codec (Universal Compatibility):");
    let aac_template = TemplateBuilder::new("AAC Demo")
        .description("Universal compatibility with AAC audio")
        .enable_enhancement("prob-4")
        .enhancement_params(20, 25, 15)
        .resolution(1920, 1080)
        .video_codec("libx264", Some(23))
        .container_format("mp4") // Auto-selects AAC
        .audio_settings_optimized("aac", "high") // 256 kbps
        .build()?;

    println!("   Template: {}", aac_template.name);
    println!("   Audio Codec: {}", aac_template.settings.output.audio_codec.as_ref().unwrap_or(&"None".to_string()));
    println!("   Audio Bitrate: {} kbps", aac_template.settings.output.audio_bitrate.unwrap_or(0));
    println!("   Audio Channels: {}", aac_template.settings.output.audio_channels.unwrap_or(0));

    let aac_command = sdk.generate_ffmpeg_command(&aac_template, "input.mp4", "output_aac.mp4")?;
    println!("   Command:");
    println!("   {}", aac_command);

    // Example 2: AC3 Audio (Surround Sound)
    println!("\n2. AC3 Audio Codec (Dolby Digital 5.1):");
    let ac3_template = TemplatePresets::broadcast_ready()?;
    println!("   Template: {}", ac3_template.name);
    println!("   Audio Codec: {}", ac3_template.settings.output.audio_codec.as_ref().unwrap_or(&"None".to_string()));
    println!("   Audio Bitrate: {} kbps", ac3_template.settings.output.audio_bitrate.unwrap_or(0));
    println!("   Audio Channels: {} (5.1 Surround)", ac3_template.settings.output.audio_channels.unwrap_or(0));

    let ac3_command = sdk.generate_ffmpeg_command(&ac3_template, "input.mp4", "output_ac3.mp4")?;
    println!("   Command:");
    println!("   {}", ac3_command);

    // Example 3: PCM Audio (Lossless)
    println!("\n3. PCM Audio Codec (Lossless Professional):");
    let pcm_template = TemplatePresets::professional_master()?;
    println!("   Template: {}", pcm_template.name);
    println!("   Audio Codec: {}", pcm_template.settings.output.audio_codec.as_ref().unwrap_or(&"None".to_string()));
    println!("   Audio Quality: Lossless (no bitrate limit)");
    println!("   Audio Channels: {}", pcm_template.settings.output.audio_channels.unwrap_or(0));

    let pcm_command = sdk.generate_ffmpeg_command(&pcm_template, "input.mp4", "output_pcm.mov")?;
    println!("   Command:");
    println!("   {}", pcm_command);

    // Example 4: Vorbis Audio (Web Optimized)
    println!("\n4. Vorbis Audio Codec (Web/Open Source):");
    let vorbis_template = TemplatePresets::web_optimized()?;
    println!("   Template: {}", vorbis_template.name);
    println!("   Audio Codec: {}", vorbis_template.settings.output.audio_codec.as_ref().unwrap_or(&"None".to_string()));
    println!("   Container: WebM (Web optimized)");
    println!("   Audio Channels: {}", vorbis_template.settings.output.audio_channels.unwrap_or(0));

    let vorbis_command = sdk.generate_ffmpeg_command(&vorbis_template, "input.mp4", "output_web.webm")?;
    println!("   Command:");
    println!("   {}", vorbis_command);

    // Example 5: Audio Quality Comparison
    println!("\n5. Audio Quality Comparison (AAC):");
    
    let qualities = vec![
        ("Low", "low", 128),
        ("Medium", "medium", 192),
        ("High", "high", 256),
        ("Highest", "highest", 320),
    ];

    for (name, quality, expected_bitrate) in qualities {
        let template = TemplateBuilder::new(&format!("AAC {}", name))
            .enable_enhancement("prob-4")
            .audio_settings_optimized("aac", quality)
            .build()?;

        let actual_bitrate = template.settings.output.audio_bitrate.unwrap_or(0);
        println!("   {} Quality: {} kbps (expected: {} kbps)", name, actual_bitrate, expected_bitrate);
        
        let command = sdk.generate_ffmpeg_command(&template, "input.mp4", &format!("output_{}.mp4", quality))?;
        println!("   Command: {}", command);
    }

    // Example 6: Container Format Auto-Selection
    println!("\n6. Container Format Auto-Selection:");
    
    let formats = vec![
        ("MP4", "mp4", "aac"),
        ("MOV", "mov", "aac"),
        ("MKV", "mkv", "aac"),
        ("WebM", "webm", "vorbis"),
    ];

    for (name, format, expected_codec) in formats {
        let template = TemplateBuilder::new(&format!("{} Container", name))
            .enable_enhancement("prob-4")
            .container_format(format) // Auto-selects compatible audio codec
            .build()?;

        let actual_codec = template.settings.output.audio_codec.as_ref().map(|s| s.as_str()).unwrap_or("None");
        println!("   {} Container: Auto-selected {} audio (expected: {})", name, actual_codec, expected_codec);
    }

    // Example 7: Custom Audio Settings
    println!("\n7. Custom Audio Settings:");
    let custom_template = TemplateBuilder::new("Custom Audio")
        .description("Custom audio configuration example")
        .enable_enhancement("prob-4")
        .enhancement_params(25, 30, 20)
        .resolution(1920, 1080)
        .video_codec("libx264", Some(23))
        .audio_settings("aac", 256, 2) // Manual settings
        .build()?;

    println!("   Template: {}", custom_template.name);
    println!("   Manual Audio Settings:");
    println!("     Codec: {}", custom_template.settings.output.audio_codec.as_ref().unwrap_or(&"None".to_string()));
    println!("     Bitrate: {} kbps", custom_template.settings.output.audio_bitrate.unwrap_or(0));
    println!("     Channels: {}", custom_template.settings.output.audio_channels.unwrap_or(0));

    let custom_command = sdk.generate_ffmpeg_command(&custom_template, "input.mp4", "output_custom.mp4")?;
    println!("   Command:");
    println!("   {}", custom_command);

    // Example 8: Use Case Recommendations
    println!("\n8. Audio Codec Use Case Recommendations:");
    println!("   🎵 AAC:");
    println!("     • Best for: General video, streaming, mobile devices");
    println!("     • Bitrates: 128-320 kbps");
    println!("     • Containers: MP4, MOV, MKV, AVI");
    println!("     • Channels: Stereo (2.0)");
    
    println!("\n   🎭 AC3 (Dolby Digital):");
    println!("     • Best for: Home theater, broadcast, DVD/Blu-ray");
    println!("     • Bitrates: 160-640 kbps");
    println!("     • Containers: MP4, MOV, MKV, AVI");
    println!("     • Channels: Up to 5.1 surround");
    
    println!("\n   🎼 PCM:");
    println!("     • Best for: Professional mastering, archival");
    println!("     • Quality: Lossless (no compression)");
    println!("     • Containers: MOV, MKV, AVI (not MP4)");
    println!("     • Channels: Stereo or multi-channel");
    
    println!("\n   🌐 Vorbis:");
    println!("     • Best for: Web content, open source projects");
    println!("     • Quality: Variable bitrate encoding");
    println!("     • Containers: WebM only");
    println!("     • Channels: Stereo (2.0)");

    println!("\n✅ Audio codecs demo completed successfully!");
    println!("\nKey Features Demonstrated:");
    println!("  • Automatic audio codec selection based on container");
    println!("  • Quality-based bitrate optimization");
    println!("  • Multi-channel audio support (stereo vs surround)");
    println!("  • Lossless vs lossy audio options");
    println!("  • Web-optimized audio encoding");
    println!("  • Professional mastering workflows");

    Ok(())
}
