mod config;
mod file_access;
mod nexrender;
mod processor;
mod synology;
mod web;

use anyhow::Result;
use clap::Parser;
use config::Config;
use dotenv::dotenv;
use log::info;

#[derive(Parser)]
#[command(author, version, about, long_about = None)]
struct Cli {
    /// Run web server instead of one-time processing
    #[arg(short, long)]
    web: bool,

    /// Run one-time processing (default if no flags)
    #[arg(short, long)]
    once: bool,
}

#[tokio::main]
async fn main() -> Result<()> {
    // Загружаем .env до инициализации логгера
    dotenv().ok();

    // Инициализируем логгер
    env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init();

    let cli = Cli::parse();
    let config = Config::from_env()?;

    if cli.web {
        info!(
            "Starting AE Anons web server v{}",
            env!("CARGO_PKG_VERSION")
        );
        web::run_web_server(config).await?;
    } else {
        info!(
            "Starting AE Anons processor v{} (one-time mode)",
            env!("CARGO_PKG_VERSION")
        );
        let submitted = processor::process_spreadsheet(&config).await?;
        info!("Submitted {} jobs. Exiting.", submitted.len());
    }

    Ok(())
}
