
use clap::Parser;

use pasetoken_lib::pasetoken::ConfigPaSeToken;
//use pasetors::footer::Footer;

use crate::{PKG_NAME,PKG_VERSION};

use crate::tools::generate_uuid;

// Use `clap` to parse command line options with `derive` mode 
#[derive(Parser, Debug)]
pub struct Cli {

    /// * Configuration TOML file-path to run `WebServer` REQUIRED
    #[clap(short = 'c', long = "config", value_parser, display_order=1)]
    pub config_path: Option<String>,

    /// Generate id key for identification
    #[clap(short = 'i', long = "id",  action, display_order=2)]
    pub create_id: bool,

    /// Path to generate PaSeTo public and secret keys to use for tokens
    #[clap(short = 'p', long = "pasetoken", value_parser, display_order=3)]
    pub pasetokeys_path: Option<String>,

    /// Settings-toml-file to Generate PaSeTo Token (-o to filepath)
    #[clap(short = 't', long = "token", value_parser, display_order=4)]
    pub make_token: Option<String>,

    /// Output path to save generated token with `make_token` (-t)
    #[clap(short = 'o', long = "output", value_parser, display_order=5)]
    pub output_path: Option<String>,

    /// Show version 
    #[clap(short = 'v', long = "version", action, display_order=6)]
    pub version: bool,
}


/// Runs some options from command line like: genererate id, PaSeTo keys, Token. 
/// Set TOML config-path to load web-server settings 
pub fn parse_args() -> String {
    let args = Cli::parse();
    if let Some(pasetokeys_path) = args.pasetokeys_path {
        match pasetoken_lib::generate_keys(&pasetokeys_path,false) {
            Ok(_) => println!("PaSeTo keys generated to: {}", &pasetokeys_path),
            Err(e) => eprint!("Error generating keys: {}",e),
        };
        std::process::exit(0);
    }
    let output_path = if let Some(out_path) = args.output_path {
        out_path
    } else {
        String::from("")
    };
    if let Some(def_path) = args.make_token {
        match ConfigPaSeToken::token_from_file_defs(&def_path, &output_path) {
            Ok(token) => {
                if output_path.is_empty() {
                    println!("{}",&token)
                } else {
                    println!("Token from defs {} generated to {}",&def_path,&output_path)
                }
            },
            Err(e) => {
                if output_path.is_empty() {
                    eprintln!("Error generating token from file defs {} error: {}",
                        &def_path, e
                    )
                } else {
                    eprintln!("Error generating token from file defs {} to {} error: {}",
                        &def_path, &output_path, e
                    )
                }
            },
        }; 
        std::process::exit(0);
    }
    if args.create_id {
        println!("{}",generate_uuid(String::from("abcdef0123456789")));
        std::process::exit(0);
    }
    if args.version {
        println!("{} version: {}",PKG_NAME,PKG_VERSION);
        std::process::exit(0);
    }

    let config_path = args.config_path.unwrap_or(String::from(""));
    if config_path.is_empty() {
        eprintln!("No config-file found");
        std::process::exit(2);
    } 
    config_path
}
