//! Exit codes: 0 = success, 1 = fatal error.
use clap::Parser;
use psql_scip::{cli::Cli, config::Config, pipeline};

fn main() {
    let cli = Cli::parse();

    let filter = tracing_subscriber::EnvFilter::try_from_default_env()
        .unwrap_or_else(|_| tracing_subscriber::EnvFilter::new(cli.log_level.as_str()));
    tracing_subscriber::fmt().with_env_filter(filter).init();

    let config = Config {
        schema_path: cli.schema,
        root_dir: cli.root,
        output_path: cli.out,
        no_gitignore: cli.no_gitignore,
    };

    match pipeline::run(&config) {
        Ok(stats) => {
            tracing::info!(
                files = stats.files_processed,
                occurrences = stats.total_occurrences,
                "indexing complete"
            );
        }
        Err(e) => {
            tracing::error!(error = %e, "indexing failed");
            std::process::exit(1);
        }
    }
}
