// src/monitoring/events.rs
use super::metrics::ProxyInfo;

/// Events emitted by the scraper system
#[derive(Debug, Clone)]
pub enum MonitoringEvent {
    // Pool initialization
    PoolInitialized {
        pool_size: usize,
        with_proxy: bool,
        with_rotation: bool,
    },
    
    // Instance lifecycle
    InstanceCreated {
        instance_id: usize,
        max_tasks: usize,
        proxy: Option<ProxyInfo>,
    },
    
    InstanceStatusChanged {
        instance_id: usize,
        status: InstanceStatusChange,
    },

    InstanceSelected {
        instance_id: usize,
        half: usize,
    },
    
    // Task execution
    TaskStarted {
        instance_id: usize,
        url: String,
    },
    
    TaskCompleted {
        instance_id: usize,
        success: bool,
        duration_ms: u64,
        error: Option<String>,
    },
    
    NavigationTimeout {
        instance_id: usize,
        url: String,
    },
    
    BotDetectionTriggered {
        instance_id: usize,
        url: String,
    },
    
    // Session management
    SessionStarted {
        instance_id: usize,
        proxy: Option<ProxyInfo>,
    },
    
    SessionRenewed {
        instance_id: usize,
        old_request_count: usize,
        reason: RenewalReason,
        new_proxy: Option<ProxyInfo>,
    },
    
    SessionRequestIncremented {
        instance_id: usize,
        new_count: usize,
    },
    
    // Proxy events
    ProxyConnected {
        container_name: String,
        ip_address: String,
        port: u16,
    },
    
    ProxyFailed {
        container_name: String,
        error: String,
    },
    
    ProxyRotated {
        instance_id: usize,
        old_proxy: Option<String>,
        new_proxy: String,
    },
    
    // Pool rotation events
    RotationTriggered {
        reason: String,
    },
    
    // Yahoo API events
    YahooRequestStarted {
        instance_id: usize,
        endpoint: String,
        symbol: Option<String>,
    },
    
    YahooRequestCompleted {
        instance_id: usize,
        success: bool,
        duration_ms: u64,
        error: Option<String>,
    },
    
    YahooBatchRequestStarted {
        count: usize,
        symbols: Vec<String>,
        endpoint: String,
    },
    
    YahooBatchRequestCompleted {
        successful: usize,
        failed: usize,
        total: usize,
        duration_ms: u64,
    },
    
    YahooClientCreated {
        instance_id: usize,
        has_proxy: bool,
        max_requests: u32,
    },
    
    YahooClientReset {
        instance_id: usize,
        previous_requests: u32,
        reason: String,
    },
    
    // Logging
    LogMessage {
        level: LogLevel,
        message: String,
    },
}

#[derive(Debug, Clone)]
pub enum InstanceStatusChange {
    Idle,
    Active,
    Renewing,
    Error(String),
}

#[derive(Debug, Clone)]
pub enum RenewalReason {
    TaskLimit,
    RequestLimit,
    Error,
    Manual,
}

#[derive(Debug, Clone)]
pub enum LogLevel {
    Info,
    Warn,
    Error,
}

impl std::fmt::Display for RenewalReason {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            RenewalReason::TaskLimit => write!(f, "task_limit"),
            RenewalReason::RequestLimit => write!(f, "request_limit"),
            RenewalReason::Error => write!(f, "error"),
            RenewalReason::Manual => write!(f, "manual"),
        }
    }
}