//! Test that connects the current (latest) Rust SDK sync against the oldest supported Ditto version
//!
//! Counterpart, which inserts the doc and listens on TCP is in `/tools/oldest-sdk-test`.

use std::{env, error::Error, process, str::FromStr, thread, time::Duration};

use dittolive_ditto::prelude::*;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize)]
struct MyDoc {
    _id: String,
}

fn main() -> Result<(), Box<dyn Error>> {
    let mut args = env::args();
    let _app_name = args.next();
    let app_id = args.next().expect("expected App ID as first parameter");
    let doc_id = args
        .next()
        .expect("expected target document ID as second parameter");
    let connect_port: u16 = args
        .next()
        .expect("expected connect port as third parameter")
        .parse()
        .expect("expected port to be a valid u16");

    let ditto = Ditto::builder()
        .with_temp_dir()
        .with_minimum_log_level(LogLevel::Debug)
        .with_identity(|ditto_root| {
            let app_id = AppId::from_str(&app_id)?;
            OfflinePlayground::new(ditto_root, app_id)
        })?
        .with_transport_config(|_identity| {
            let mut transport_config = TransportConfig::new();
            let other_peer = format!("127.0.0.1:{}", connect_port);
            transport_config.connect.tcp_servers.insert(other_peer);
            transport_config
        })?
        .build()?;
    ditto.set_license_from_env("DITTO_LICENSE")?;
    ditto.start_sync()?;

    let collection = ditto.store().collection("test_coll").unwrap();
    println!("old_sync_test looking for doc_id {}", doc_id);
    let doc_id = DocumentId::new(&doc_id).unwrap();
    let _sub = collection.find_by_id(doc_id.clone()).subscribe();
    let _obs =
        collection
            .find_by_id(doc_id)
            .observe_local(|maybe_doc: Option<BoxedDocument>, _| {
                if maybe_doc.is_some() {
                    println!("old_sync_test found expected doc");
                    process::exit(0);
                }
            });

    // Should succeed within a reasonable time
    thread::sleep(Duration::from_secs(30));
    Err("timed out".into())
}
