use tvai_sdk::*;

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

    let sdk = TvaiSdk::new();

    // Example 1: High-quality NVENC encoding
    println!("\n1. High-Quality NVENC Template:");
    let nvenc_template = TemplatePresets::high_quality_nvenc()?;
    println!("   Template: {}", nvenc_template.name);
    println!("   Description: {}", nvenc_template.description);
    
    let nvenc_command = sdk.generate_ffmpeg_command(&nvenc_template, "input.mp4", "output_nvenc.mp4")?;
    println!("   Command:");
    println!("   {}", nvenc_command);

    // Example 2: HDR Processing
    println!("\n2. HDR Processing Template:");
    let hdr_template = TemplatePresets::hdr_processing()?;
    println!("   Template: {}", hdr_template.name);
    
    let hdr_command = sdk.generate_ffmpeg_command(&hdr_template, "input_hdr.mp4", "output_hdr.mp4")?;
    println!("   Command:");
    println!("   {}", hdr_command);

    // Example 3: Streaming Optimized
    println!("\n3. Streaming Optimized Template:");
    let streaming_template = TemplatePresets::streaming_optimized()?;
    println!("   Template: {}", streaming_template.name);
    
    let streaming_command = sdk.generate_ffmpeg_command(&streaming_template, "input.mp4", "output_streaming.mp4")?;
    println!("   Command:");
    println!("   {}", streaming_command);

    // Example 4: Mobile Optimized
    println!("\n4. Mobile Optimized Template:");
    let mobile_template = TemplatePresets::mobile_optimized()?;
    println!("   Template: {}", mobile_template.name);
    
    let mobile_command = sdk.generate_ffmpeg_command(&mobile_template, "input.mp4", "output_mobile.mp4")?;
    println!("   Command:");
    println!("   {}", mobile_command);

    // Example 5: Archival Quality
    println!("\n5. Archival Quality Template:");
    let archival_template = TemplatePresets::archival_quality()?;
    println!("   Template: {}", archival_template.name);
    
    let archival_command = sdk.generate_ffmpeg_command(&archival_template, "input.mp4", "output_archival.mp4")?;
    println!("   Command:");
    println!("   {}", archival_command);

    // Example 6: Custom template with advanced settings
    println!("\n6. Custom Advanced Template:");
    let custom_template = TemplateBuilder::new("Custom Advanced")
        .description("Custom template with advanced output settings")
        .enable_enhancement("prob-4")
        .enhancement_params(25, 30, 15)
        .resolution(2560, 1440) // 1440p
        .video_codec("hevc_nvenc", Some(20))
        .preset("slow")
        .profile_level("main10", Some("5.0"))
        .pixel_format("yuv420p10le")
        .audio_settings("aac", 192, 2)
        .hardware_acceleration("cuda", Some("0"))
        .custom_params(vec![
            "-rc vbr".to_string(),
            "-rc-lookahead 32".to_string(),
            "-spatial_aq 1".to_string(),
            "-aq-strength 8".to_string(),
        ])
        .build()?;

    println!("   Template: {}", custom_template.name);
    println!("   Settings:");
    println!("     Resolution: {}x{}", 
        custom_template.settings.output.width.unwrap_or(0),
        custom_template.settings.output.height.unwrap_or(0)
    );
    println!("     Codec: {}", custom_template.settings.output.codec.as_ref().unwrap());
    println!("     CRF: {}", custom_template.settings.output.crf.unwrap_or(0));
    println!("     Preset: {}", custom_template.settings.output.preset.as_ref().unwrap());
    println!("     Audio: {} @ {}kbps", 
        custom_template.settings.output.audio_codec.as_ref().unwrap(),
        custom_template.settings.output.audio_bitrate.unwrap_or(0)
    );

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

    // Example 7: Different codec comparisons
    println!("\n7. Codec Comparison Templates:");
    
    // H.264 template
    let h264_template = TemplateBuilder::new("H.264 Comparison")
        .description("H.264 encoding for compatibility")
        .enable_enhancement("prob-4")
        .resolution(1920, 1080)
        .video_codec("libx264", Some(23))
        .preset("medium")
        .profile_level("high", Some("4.0"))
        .audio_settings("aac", 128, 2)
        .build()?;

    // H.265 template
    let h265_template = TemplateBuilder::new("H.265 Comparison")
        .description("H.265 encoding for efficiency")
        .enable_enhancement("prob-4")
        .resolution(1920, 1080)
        .video_codec("libx265", Some(25)) // Higher CRF for similar quality
        .preset("medium")
        .profile_level("main", Some("4.0"))
        .audio_settings("aac", 128, 2)
        .build()?;

    // AV1 template
    let av1_template = TemplateBuilder::new("AV1 Comparison")
        .description("AV1 encoding for future-proofing")
        .enable_enhancement("prob-4")
        .resolution(1920, 1080)
        .video_codec("libaom-av1", Some(30)) // Much higher CRF for AV1
        .preset("6") // AV1 uses numeric presets
        .audio_settings("opus", 128, 2) // Opus for AV1
        .custom_params(vec![
            "-cpu-used 4".to_string(),
            "-row-mt 1".to_string(),
        ])
        .build()?;

    println!("   H.264 Command:");
    let h264_command = sdk.generate_ffmpeg_command(&h264_template, "input.mp4", "output_h264.mp4")?;
    println!("   {}", h264_command);

    println!("\n   H.265 Command:");
    let h265_command = sdk.generate_ffmpeg_command(&h265_template, "input.mp4", "output_h265.mp4")?;
    println!("   {}", h265_command);

    println!("\n   AV1 Command:");
    let av1_command = sdk.generate_ffmpeg_command(&av1_template, "input.mp4", "output_av1.mp4")?;
    println!("   {}", av1_command);

    // Example 8: Hardware-specific templates
    println!("\n8. Hardware-Specific Templates:");

    // NVIDIA template
    let nvidia_template = TemplateBuilder::new("NVIDIA Optimized")
        .description("Optimized for NVIDIA GPUs")
        .enable_enhancement("prob-4")
        .resolution(1920, 1080)
        .video_codec("hevc_nvenc", Some(21))
        .preset("p4") // NVENC preset
        .hardware_acceleration("cuda", Some("0"))
        .custom_params(vec![
            "-rc vbr".to_string(),
            "-multipass fullres".to_string(),
            "-spatial_aq 1".to_string(),
        ])
        .build()?;

    // AMD template
    let amd_template = TemplateBuilder::new("AMD Optimized")
        .description("Optimized for AMD GPUs")
        .enable_enhancement("prob-4")
        .resolution(1920, 1080)
        .video_codec("hevc_amf", Some(23))
        .preset("balanced") // AMF preset
        .custom_params(vec![
            "-rc cqp".to_string(),
            "-quality quality".to_string(),
        ])
        .build()?;

    // Intel template
    let intel_template = TemplateBuilder::new("Intel Optimized")
        .description("Optimized for Intel Quick Sync")
        .enable_enhancement("prob-4")
        .resolution(1920, 1080)
        .video_codec("hevc_qsv", Some(25))
        .preset("medium")
        .hardware_acceleration("qsv", None)
        .custom_params(vec![
            "-look_ahead 1".to_string(),
            "-look_ahead_depth 40".to_string(),
        ])
        .build()?;

    println!("   NVIDIA Command:");
    let nvidia_command = sdk.generate_ffmpeg_command(&nvidia_template, "input.mp4", "output_nvidia.mp4")?;
    println!("   {}", nvidia_command);

    println!("\n   AMD Command:");
    let amd_command = sdk.generate_ffmpeg_command(&amd_template, "input.mp4", "output_amd.mp4")?;
    println!("   {}", amd_command);

    println!("\n   Intel Command:");
    let intel_command = sdk.generate_ffmpeg_command(&intel_template, "input.mp4", "output_intel.mp4")?;
    println!("   {}", intel_command);

    println!("\n✅ Advanced output settings example completed successfully!");
    println!("\nKey Features Demonstrated:");
    println!("  • Custom resolution settings");
    println!("  • Multiple codec support (H.264, H.265, AV1)");
    println!("  • Hardware acceleration (NVIDIA, AMD, Intel)");
    println!("  • Quality control (CRF, bitrate)");
    println!("  • Audio settings configuration");
    println!("  • HDR and color space handling");
    println!("  • Custom FFmpeg parameters");
    println!("  • Preset-based encoding optimization");

    Ok(())
}
