// src/main.rs
mod config;
mod corporate;
mod economic;
mod util;
mod scraper;

use anyhow::Result;
use config::Config;
use scraper::webdriver::ChromeDriverPool;
use util::directories::DataPaths;
use util::{logger, opnv};
use std::sync::Arc;

/// The entry point of the application.
///
/// This function loads the configuration, initializes a shared ChromeDriver pool,
/// fetches the latest VPNBook OpenVPN configurations if VPN rotation is enabled,
/// and sequentially runs the full updates for corporate and economic data.
/// Sequential execution helps prevent resource exhaustion from concurrent
/// chromedriver instances and avoids spamming the target websites with too many requests.
///
/// # Errors
///
/// Returns an error if configuration loading fails, pool initialization fails,
/// VPN fetching fails (if enabled), or if either update function encounters an issue
/// (e.g., network errors, scraping failures, or chromedriver spawn failures like "program not found").
#[tokio::main]
async fn main() -> Result<()> {
    let config = Config::load().map_err(|err| {
        println!("Failed to load Config .env: {}", err);
        err
    })?;

    // Initialize paths
    let paths = DataPaths::new(".")?;
    
    // Initialize logger
    logger::init_debug_logger(paths.logs_dir()).await.map_err(|e| {
        anyhow::anyhow!("Logger initialization failed: {}", e)
    })?;

    logger::log_info("=== Application started ===").await;
    logger::log_info(&format!("Config: economic_start_date={}, corporate_start_date={}, lookahead_months={}, max_parallel_instances={}, enable_vpn_rotation={}", 
        config.economic_start_date, config.corporate_start_date, config.economic_lookahead_months, config.max_parallel_instances, config.enable_vpn_rotation)).await;

    // Initialize the shared ChromeDriver pool once
    let pool_size = config.max_parallel_instances;
    logger::log_info(&format!("Initializing ChromeDriver pool with size: {}", pool_size)).await;
    
    let pool = Arc::new(ChromeDriverPool::new(pool_size).await?);
    logger::log_info("✓ ChromeDriver pool initialized successfully").await;

    // Fetch VPNBook configs if VPN rotation is enabled
    if config.enable_vpn_rotation {
        logger::log_info("--- Fetching latest VPNBook OpenVPN configurations ---").await;
        let (username, password, files) = 
            util::opnv::fetch_vpnbook_configs(&pool, paths.cache_dir()).await?;
        logger::log_info(&format!("Fetched VPN username: {}, password: {}", username, password)).await;
        for file in &files {
            logger::log_info(&format!("Extracted OVPN: {:?}", file)).await;
        }
        // Optionally, store username/password for rotation use (e.g., in a file or global state)
        // For now, just log them; extend as needed for rotation integration
    }

    // Run economic update first, passing the shared pool
    logger::log_info("--- Starting economic data update ---").await;
    economic::run_full_update(&config, &pool).await?;
    logger::log_info("✓ Economic data update completed").await;

    // Then run corporate update, passing the shared pool
    logger::log_info("--- Starting corporate data update ---").await;
    corporate::run_full_update(&config, &pool).await?;
    logger::log_info("✓ Corporate data update completed").await;

    logger::log_info("=== Application completed successfully ===").await;
    Ok(())
}