mod cli;
mod config;
mod list;
mod messages;
mod progress;
mod renderer;

use std::error::Error;

use clap::Parser;

use crate::cli::Cli;
use crate::config::{
    apply_args, load_config_with_context, resolve_filter_config, resolve_sort_config,
};
use crate::list::run_list;

fn main() -> Result<(), Box<dyn Error>> {
    let cli = Cli::parse();
    let Cli {
        ref config,
        no_dotenv,
        verbose,
        ..
    } = cli;

    let config = load_config_with_context(no_dotenv, config.as_ref())?;

    let mut config = config;
    apply_args(&mut config, &cli);
    let (sort_config, warnings) = resolve_sort_config(config.sort.take(), &cli)?;
    if let Some(sort_config) = sort_config {
        config.sort = Some(sort_config);
    }
    let filter_config = resolve_filter_config(config.filter.take(), &cli)?;
    if let Some(filter_config) = filter_config {
        config.filter = Some(filter_config);
    }
    run_list(config, warnings, verbose)?;

    Ok(())
}
