#!/usr/bin/env python3
"""Debug background analyzer issue"""

import sys
sys.path.append('.')

from analyzer.analysis import EthernetAnalyzer
from analyzer.utils import PCAPLoader
from analyzer.analysis.background_analyzer import BackgroundAnalyzer
import time

def debug_background_processing(pcap_file, src_ip="192.168.4.89"):
    """Debug what's different in background processing"""
    
    print("=== BATCH PROCESSING (REFERENCE) ===")
    analyzer_batch = EthernetAnalyzer(outlier_threshold_sigma=3.0)
    
    loader = PCAPLoader(pcap_file)
    packets = loader.load_all()
    print(f"Loaded {len(packets)} packets")
    
    for i, packet in enumerate(packets, 1):
        analyzer_batch._process_single_packet(packet, i)
    
    analyzer_batch.calculate_statistics()
    
    flow_batch = None
    for flow_key, flow in analyzer_batch.flows.items():
        if flow.src_ip == src_ip:
            flow_batch = flow
            break
    
    if flow_batch:
        print(f"Batch - Packets: {flow_batch.frame_count}")
        print(f"Batch - Inter-arrival count: {len(flow_batch.inter_arrival_times)}")
        print(f"Batch - Avg ΔT: {flow_batch.avg_inter_arrival * 1000:.3f} ms")
        print(f"Batch - Std σ: {flow_batch.std_inter_arrival * 1000:.3f} ms")
        print(f"Batch - Outliers: {len(flow_batch.outlier_frames)} {sorted(flow_batch.outlier_frames)}")
        
        # Show first 10 inter-arrival times
        print("Batch - First 10 inter-arrival times:")
        for i, t in enumerate(flow_batch.inter_arrival_times[:10]):
            print(f"  [{i}] {t * 1000:.3f} ms")
    
    print("\n=== BACKGROUND PROCESSING ===")
    analyzer_bg = EthernetAnalyzer(outlier_threshold_sigma=3.0)
    bg_analyzer = BackgroundAnalyzer(analyzer_bg)
    
    bg_analyzer.start_parsing(pcap_file) 
    
    while bg_analyzer.is_parsing:
        time.sleep(0.1)
    
    flow_bg = None
    for flow_key, flow in analyzer_bg.flows.items():
        if flow.src_ip == src_ip:
            flow_bg = flow
            break
    
    if flow_bg:
        print(f"Background - Packets: {flow_bg.frame_count}")
        print(f"Background - Inter-arrival count: {len(flow_bg.inter_arrival_times)}")
        print(f"Background - Avg ΔT: {flow_bg.avg_inter_arrival * 1000:.3f} ms")
        print(f"Background - Std σ: {flow_bg.std_inter_arrival * 1000:.3f} ms")
        print(f"Background - Outliers: {len(flow_bg.outlier_frames)} {sorted(flow_bg.outlier_frames)}")
        
        # Show first 10 inter-arrival times
        print("Background - First 10 inter-arrival times:")
        for i, t in enumerate(flow_bg.inter_arrival_times[:10]):
            print(f"  [{i}] {t * 1000:.3f} ms")
    
    print("\n=== COMPARISON ===")
    if flow_batch and flow_bg:
        if flow_batch.frame_count != flow_bg.frame_count:
            print(f"⚠️ Packet count mismatch! {flow_batch.frame_count} vs {flow_bg.frame_count}")
        
        if len(flow_batch.inter_arrival_times) != len(flow_bg.inter_arrival_times):
            print(f"⚠️ Inter-arrival count mismatch! {len(flow_batch.inter_arrival_times)} vs {len(flow_bg.inter_arrival_times)}")
        
        # Check first few times for differences
        print("Comparing first 10 inter-arrival times:")
        min_len = min(len(flow_batch.inter_arrival_times), len(flow_bg.inter_arrival_times))
        for i in range(min(10, min_len)):
            t_batch = flow_batch.inter_arrival_times[i] * 1000
            t_bg = flow_bg.inter_arrival_times[i] * 1000
            diff = abs(t_batch - t_bg)
            if diff > 0.001:  # More than 1 microsecond difference
                print(f"  [{i}] DIFF: Batch={t_batch:.6f} vs Background={t_bg:.6f} (diff={diff:.6f})")

if __name__ == "__main__":
    if len(sys.argv) > 1:
        debug_background_processing(sys.argv[1])
    else:
        debug_background_processing("1 PTPGM.pcapng")