use clap::{Parser, Subcommand};
use std::{
    fs, io,
    path::{self, Path, PathBuf},
};

use crate::{
    commands::add_entry::add_entry,
    commands::list_entries::list_entries,
    md::{Doc, ToMd},
};

mod commands;
mod md;
mod utils;

#[derive(Debug, Parser)]
struct Cli {
    #[arg(env)]
    s10e_jrnl_file_loc: PathBuf,
    #[command(subcommand)]
    command: Option<Command>,
}

#[derive(Debug, Subcommand)]
enum Command {
    #[command(aliases = ["l", "ls", "list"])]
    ListEntries,
    #[command(aliases = ["a", "add-entry"])]
    Add { title: Option<String> },
}

fn main() -> io::Result<()> {
    let cli = Cli::parse();
    println!("Hello, world!");
    println!("cli: {cli:#?}");
    let path = dbg!(path::absolute(&cli.s10e_jrnl_file_loc)?);

    match cli.command {
        Some(Command::ListEntries) => list_entries(&path),
        Some(Command::Add { title }) => add_entry(&path, title),
        None => {
            // TODO: handle btter
            let file = fs::read_to_string(path)?;

            let doc = Doc::new(&file).unwrap();
            println!("{}", doc.to_md());
            Ok(())
        }
    }
}
