mod dataset;
mod profile;
mod tui;

use std::path::PathBuf;

use anyhow::Result;
use clap::{Parser, Subcommand};

#[derive(Debug, Parser)]
#[command(
    name = "dap",
    version,
    about = "Terminal data exploration for pipeline prep"
)]
struct Cli {
    #[command(subcommand)]
    command: Commands,
}

#[derive(Debug, Subcommand)]
enum Commands {
    /// Open CSV or Parquet file in the interactive viewer.
    Open {
        /// Dataset path.
        path: PathBuf,

        /// Maximum rows to load while scrolling. Omit for no row cap.
        #[arg(short, long)]
        limit: Option<usize>,
    },
    /// Profile CSV or Parquet columns for pipeline evaluation.
    Profile {
        /// Dataset path.
        path: PathBuf,

        /// Maximum rows to profile. Omit for no row cap.
        #[arg(short, long)]
        limit: Option<usize>,

        /// Emit JSON instead of text.
        #[arg(long)]
        json: bool,
    },
}

fn main() -> Result<()> {
    let cli = Cli::parse();

    match cli.command {
        Commands::Open { path, limit } => {
            let dataset = dataset::load_preview(&path, limit)?;
            tui::run(dataset)?;
        }
        Commands::Profile { path, limit, json } => {
            let profile = profile::profile_path(&path, limit)?;
            if json {
                println!("{}", serde_json::to_string_pretty(&profile)?);
            } else {
                profile::print_text(&profile);
            }
        }
    }

    Ok(())
}
