#!/usr/bin/env python3
"""Debug background analyzer timing processing"""

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_timing(pcap_file, src_ip="192.168.4.89"):
    """Debug timing in background processing"""
    
    print("=== DEBUGGING BACKGROUND TIMING ===")
    
    # Test 1: Single-threaded background analyzer
    print("\n1. Single-threaded background analyzer:")
    analyzer1 = EthernetAnalyzer(enable_realtime=False, outlier_threshold_sigma=3.0)
    bg_analyzer1 = BackgroundAnalyzer(analyzer1, num_threads=1)  # Single thread
    
    bg_analyzer1.start_parsing(pcap_file)
    while bg_analyzer1.is_parsing:
        time.sleep(0.1)
    
    flow1 = None
    for flow_key, flow in analyzer1.flows.items():
        if flow.src_ip == src_ip:
            flow1 = flow
            break
    
    if flow1:
        print(f"  Packets: {flow1.frame_count}")
        print(f"  Inter-arrival count: {len(flow1.inter_arrival_times)}")
        print(f"  Avg ΔT: {flow1.avg_inter_arrival * 1000:.3f} ms")
        print(f"  Std σ: {flow1.std_inter_arrival * 1000:.3f} ms")
        ch10_data_outliers = len(flow1.frame_types.get('CH10-Data', type('', (), {'outlier_frames': []})).outlier_frames)
        print(f"  CH10-Data outliers: {ch10_data_outliers}")
    
    # Test 2: Multi-threaded background analyzer (default)
    print("\n2. Multi-threaded background analyzer:")
    analyzer2 = EthernetAnalyzer(enable_realtime=False, outlier_threshold_sigma=3.0)
    bg_analyzer2 = BackgroundAnalyzer(analyzer2, num_threads=4)  # Multi thread
    
    bg_analyzer2.start_parsing(pcap_file)
    while bg_analyzer2.is_parsing:
        time.sleep(0.1)
    
    flow2 = None
    for flow_key, flow in analyzer2.flows.items():
        if flow.src_ip == src_ip:
            flow2 = flow
            break
    
    if flow2:
        print(f"  Packets: {flow2.frame_count}")
        print(f"  Inter-arrival count: {len(flow2.inter_arrival_times)}")
        print(f"  Avg ΔT: {flow2.avg_inter_arrival * 1000:.3f} ms")
        print(f"  Std σ: {flow2.std_inter_arrival * 1000:.3f} ms")
        ch10_data_outliers = len(flow2.frame_types.get('CH10-Data', type('', (), {'outlier_frames': []})).outlier_frames)
        print(f"  CH10-Data outliers: {ch10_data_outliers}")
    
    # Test 3: Batch processing (reference)
    print("\n3. Batch processing (reference):")
    analyzer3 = EthernetAnalyzer(enable_realtime=False, outlier_threshold_sigma=3.0)
    
    loader = PCAPLoader(pcap_file)
    packets = loader.load_all()
    
    for i, packet in enumerate(packets, 1):
        analyzer3._process_single_packet(packet, i)
    
    analyzer3.calculate_statistics()
    
    flow3 = None
    for flow_key, flow in analyzer3.flows.items():
        if flow.src_ip == src_ip:
            flow3 = flow
            break
    
    if flow3:
        print(f"  Packets: {flow3.frame_count}")
        print(f"  Inter-arrival count: {len(flow3.inter_arrival_times)}")
        print(f"  Avg ΔT: {flow3.avg_inter_arrival * 1000:.3f} ms")
        print(f"  Std σ: {flow3.std_inter_arrival * 1000:.3f} ms")
        ch10_data_outliers = len(flow3.frame_types.get('CH10-Data', type('', (), {'outlier_frames': []})).outlier_frames)
        print(f"  CH10-Data outliers: {ch10_data_outliers}")
    
    print(f"\n=== TIMING COMPARISON ===")
    if flow1 and flow2 and flow3:
        print(f"Single-thread BG: Avg={flow1.avg_inter_arrival * 1000:.6f}ms, Std={flow1.std_inter_arrival * 1000:.6f}ms")
        print(f"Multi-thread BG:  Avg={flow2.avg_inter_arrival * 1000:.6f}ms, Std={flow2.std_inter_arrival * 1000:.6f}ms")
        print(f"Batch:            Avg={flow3.avg_inter_arrival * 1000:.6f}ms, Std={flow3.std_inter_arrival * 1000:.6f}ms")
        
        # Check if multithreading is the issue
        if abs(flow1.std_inter_arrival - flow3.std_inter_arrival) < 0.001:
            print("\n✅ Single-threaded matches batch - multithreading is the issue!")
        elif abs(flow2.std_inter_arrival - flow3.std_inter_arrival) < 0.001:
            print("\n✅ Multi-threaded matches batch - no threading issue")
        else:
            print("\n⚠️ Neither background method matches batch processing")
    
    # Check for packet order issues
    print(f"\n=== PACKET ORDER CHECK ===")
    if flow3:
        print("First 10 packet timestamps (batch):")
        for i, ts in enumerate(flow3.timestamps[:10]):
            print(f"  [{i}] {ts:.6f}")
    
    if flow2:
        print("First 10 packet timestamps (background):")
        for i, ts in enumerate(flow2.timestamps[:10]):
            print(f"  [{i}] {ts:.6f}")

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