use anyhow::Result;
use danube_client::{DanubeClient, SchemaType};

/// This example demonstrates schema evolution and compatibility checking.
/// It shows how to evolve a schema while maintaining backward compatibility.
#[tokio::main]
async fn main() -> Result<()> {
    let client = DanubeClient::builder()
        .service_url("http://127.0.0.1:6650")
        .build()
        .await?;

    let schema_client = client.schema();

    // Step 1: Register the initial schema (v1)
    println!("📝 Step 1: Registering initial schema (v1)");

    let schema_v1 = r#"
    {
        "type": "record",
        "name": "Product",
        "namespace": "com.example.catalog",
        "fields": [
            {"name": "product_id", "type": "string"},
            {"name": "name", "type": "string"},
            {"name": "price", "type": "double"}
        ]
    }
    "#;

    let schema_id_v1 = schema_client
        .register_schema("product-catalog")
        .with_type(SchemaType::Avro)
        .with_schema_data(schema_v1.as_bytes())
        .execute()
        .await?;

    println!("✅ Schema v1 registered with ID: {}", schema_id_v1);

    // Give the broker time to sync metadata to LocalCache via watch
    // This is necessary because compatibility checks read from LocalCache
    println!("⏳ Waiting for metadata to sync...");
    tokio::time::sleep(tokio::time::Duration::from_millis(1500)).await;

    // Step 2: Check compatibility before evolving the schema
    println!("\n📝 Step 2: Checking compatibility for schema evolution (v2)");

    // Schema v2: Add a new optional field (backward compatible)
    let schema_v2 = r#"
    {
        "type": "record",
        "name": "Product",
        "namespace": "com.example.catalog",
        "fields": [
            {"name": "product_id", "type": "string"},
            {"name": "name", "type": "string"},
            {"name": "price", "type": "double"},
            {"name": "description", "type": ["null", "string"], "default": null}
        ]
    }
    "#;

    let compatibility_result = schema_client
        .check_compatibility(
            "product-catalog",
            schema_v2.as_bytes().to_vec(),
            SchemaType::Avro,
            None,
        )
        .await?;

    if compatibility_result.is_compatible {
        println!("✅ Schema v2 is compatible! Safe to register.");

        // Register the new version
        match schema_client
            .register_schema("product-catalog")
            .with_type(SchemaType::Avro)
            .with_schema_data(schema_v2.as_bytes())
            .execute()
            .await
        {
            Ok(schema_id_v2) => {
                println!("✅ Schema v2 registered with ID: {}", schema_id_v2);
            }
            Err(e) => {
                println!("⚠️  Failed to register schema v2: {}", e);
                println!("    This might be a broker timing issue. The schema may already be registered.");
            }
        }
    } else {
        println!("❌ Schema v2 is NOT compatible");
        if !compatibility_result.errors.is_empty() {
            println!("   Errors: {:?}", compatibility_result.errors);
        }
    }

    // Step 3: Try to register an incompatible schema
    println!("\n📝 Step 3: Testing incompatible schema (v3 - adds required field without default)");

    // Schema v3: Add a new REQUIRED field without default (NOT backward compatible)
    // Old data (v2) doesn't have "category", but v3 requires it → INCOMPATIBLE
    let schema_v3_incompatible = r#"
    {
        "type": "record",
        "name": "Product",
        "namespace": "com.example.catalog",
        "fields": [
            {"name": "product_id", "type": "string"},
            {"name": "name", "type": "string"},
            {"name": "price", "type": "double"},
            {"name": "description", "type": ["null", "string"], "default": null},
            {"name": "category", "type": "string"}
        ]
    }
    "#;

    let compatibility_result_v3 = schema_client
        .check_compatibility(
            "product-catalog",
            schema_v3_incompatible.as_bytes().to_vec(),
            SchemaType::Avro,
            None,
        )
        .await?;

    if compatibility_result_v3.is_compatible {
        println!("✅ Schema v3 is compatible (unexpected!)");
        println!("   Note: This should have been rejected for adding a required field!");
    } else {
        println!("❌ Schema v3 is NOT compatible (expected!)");
        println!("   Reason: Added required field 'category' without default");
        println!("   This protects against breaking old data!");
        if !compatibility_result_v3.errors.is_empty() {
            println!("   Errors: {:?}", compatibility_result_v3.errors);
        }
    }

    // Step 4: List all versions
    println!("\n📝 Step 4: Listing all schema versions");

    let versions = schema_client.list_versions("product-catalog").await?;

    println!("📋 Schema versions for 'product-catalog': {:?}", versions);

    // Step 5: Get the latest schema
    println!("\n📝 Step 5: Retrieving latest schema");

    let latest_schema = schema_client.get_latest_schema("product-catalog").await?;

    println!("✅ Latest schema:");
    println!("   Subject: {}", latest_schema.subject);
    println!("   Version: {}", latest_schema.version);
    println!("   Type: {}", latest_schema.schema_type);

    println!("\n🎉 Schema evolution demo completed!");
    println!("   Key takeaways:");
    println!("   • Adding optional fields: ✅ Compatible (backward)");
    println!("   • Adding required fields without default: ❌ Incompatible");
    println!("   • Compatibility mode: BACKWARD (default)");
    println!("   • Backward = new schema can read old data");

    println!("\n✅ SUCCESS: Schema evolution example completed!");
    println!("   All operations succeeded:");
    println!("   - Registered schema v1 (3 fields)");
    println!("   - Checked compatibility (v2 adds optional field → compatible)");
    println!("   - Registered schema v2 (4 fields)");
    println!("   - Checked compatibility (v3 adds required field → incompatible)");
    println!("   - Listed {} version(s)", versions.len());
    println!("   - Retrieved latest schema");

    Ok(())
}
