use std::time::Duration;

use rpki::fetch::http::{BlockingHttpFetcher, HttpFetcherConfig};
use rpki::fetch::rsync_system::{SystemRsyncConfig, SystemRsyncFetcher};
use rpki::policy::Policy;
use rpki::storage::RocksStore;
use rpki::validation::run_tree_from_tal::run_tree_from_tal_url_serial;
use rpki::validation::run_tree_from_tal::run_tree_from_tal_url_serial_audit;
use rpki::validation::tree::TreeRunConfig;

const APNIC_TAL_URL: &str = "https://tal.apnic.net/tal-archive/apnic-rfc7730-https.tal";

#[test]
#[ignore = "live network + rsync smoke test (APNIC TAL)"]
fn apnic_tree_depth1_processes_more_than_root() {
    let http = BlockingHttpFetcher::new(HttpFetcherConfig {
        timeout: Duration::from_secs(30 * 60),
        ..HttpFetcherConfig::default()
    })
    .expect("http fetcher");
    let rsync = SystemRsyncFetcher::new(SystemRsyncConfig {
        timeout: Duration::from_secs(30 * 60),
        ..SystemRsyncConfig::default()
    });

    let temp = tempfile::tempdir().expect("tempdir");
    let store = RocksStore::open(temp.path()).expect("open rocksdb");
    let policy = Policy::default();
    let out = run_tree_from_tal_url_serial(
        &store,
        &policy,
        APNIC_TAL_URL,
        &http,
        &rsync,
        time::OffsetDateTime::now_utc(),
        &TreeRunConfig {
            max_depth: Some(1),
            max_instances: Some(2),
            compact_audit: false,
            persist_vcir: true,
            build_ccr_accumulator: true,
            enable_roa_validation_cache: false,
            enable_child_certificate_validation_cache: false,
            publication_point_cache_observe_only: false,
            enable_publication_point_validation_cache: false,
            enable_transport_request_prefetch: false,
        },
    )
    .expect("run tree from tal");

    assert!(
        out.tree.instances_processed >= 2,
        "expected to process root + at least one child"
    );
}

#[test]
#[ignore = "live network + rsync root-only smoke test (APNIC TAL); 30min timeouts"]
fn apnic_tree_root_only_processes_root_with_long_timeouts() {
    let http = BlockingHttpFetcher::new(HttpFetcherConfig {
        timeout: Duration::from_secs(30 * 60),
        ..HttpFetcherConfig::default()
    })
    .expect("http fetcher");
    let rsync = SystemRsyncFetcher::new(SystemRsyncConfig {
        timeout: Duration::from_secs(30 * 60),
        ..SystemRsyncConfig::default()
    });

    let temp = tempfile::tempdir().expect("tempdir");
    let store = RocksStore::open(temp.path()).expect("open rocksdb");
    let policy = Policy::default();
    let validation_time = time::OffsetDateTime::now_utc();

    let out = run_tree_from_tal_url_serial_audit(
        &store,
        &policy,
        APNIC_TAL_URL,
        &http,
        &rsync,
        validation_time,
        &TreeRunConfig {
            max_depth: Some(0),
            max_instances: Some(1),
            compact_audit: false,
            persist_vcir: true,
            build_ccr_accumulator: true,
            enable_roa_validation_cache: false,
            enable_child_certificate_validation_cache: false,
            publication_point_cache_observe_only: false,
            enable_publication_point_validation_cache: false,
            enable_transport_request_prefetch: false,
        },
    )
    .expect("run APNIC root-only");

    assert_eq!(
        out.tree.instances_processed, 1,
        "expected to process exactly the root publication point"
    );
    assert_eq!(
        out.publication_points.len(),
        out.tree.instances_processed,
        "audit should include one publication point"
    );
}
