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

use anyhow::Result;
use config::Config;
use webdriver::webdriver::ChromeDriverPool;
use util::directories::DataPaths;
use util::logger;
use std::sync::Arc;

/// The entry point of the application.
///
/// This function loads the configuration, initializes a shared ChromeDriver pool,
/// 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,
/// 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_tasks={}", 
        config.economic_start_date, config.corporate_start_date, config.economic_lookahead_months, config.max_parallel_tasks)).await;

    // Initialize the shared ChromeDriver pool once
    let pool_size = config.max_parallel_tasks;
    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;

    // 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(())
}