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

use anyhow::Result;
use config::Config;
use scraper::webdriver::ChromeDriverPool;
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 the shared ChromeDriver pool once
    let pool_size = config.max_parallel_tasks;
    let pool = Arc::new(ChromeDriverPool::new(pool_size).await?);

    // Run economic update first, passing the shared pool
    economic::run_full_update(&config, &pool).await?;

    // Then run corporate update, passing the shared pool
    corporate::run_full_update(&config, &pool).await?;

    Ok(())
}