use clap::Parser;
use siteforge::cli::{Cli, Commands};
use siteforge::config::Config;
use siteforge::errors::Result;
use siteforge::{cli, tui};
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, EnvFilter};

#[tokio::main]
async fn main() -> Result<()> {
    init_tracing();

    let cli = Cli::parse();
    let config = Config::load(cli.config.as_deref())?;

    match cli.command {
        Commands::Archive(command) => cli::run_archive(*command, config).await,
        Commands::Tui => tui::run(config).await,
        Commands::List => cli::run_list(config).await,
        Commands::Inspect { archive_id } => cli::run_inspect(config, &archive_id).await,
        Commands::Search {
            query,
            archive_id,
            limit,
        } => cli::run_search(config, archive_id.as_deref(), &query, limit).await,
        Commands::Export {
            archive_id,
            format,
            output,
        } => cli::run_export(config, &archive_id, format, output.as_deref()).await,
        Commands::Pack {
            archive_id,
            for_ai,
            max_tokens,
        } => cli::run_pack(config, &archive_id, for_ai, max_tokens).await,
        Commands::Verify { archive_id } => cli::run_verify(config, &archive_id).await,
        Commands::Resume { job_id, progress } => cli::run_resume(config, &job_id, progress).await,
        Commands::Diagnostics { archive_id } => {
            cli::run_diagnostics(config, archive_id.as_deref()).await
        }
        Commands::Config { edit } => cli::run_config(config, edit).await,
    }
}

fn init_tracing() {
    let env_filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("warn"));
    tracing_subscriber::registry()
        .with(env_filter)
        .with(tracing_subscriber::fmt::layer().with_writer(std::io::stderr))
        .init();
}
