use owo_colors::OwoColorize;
use util::Arch;

use crate::cli::cli_run;

mod artifact;
mod build;
mod cargo;
mod cli;
mod doctor;
mod init;
mod publish;
mod util;

#[derive(Debug, Clone)]
pub(crate) struct InitArgs {
  package_name: Option<String>,
  name: String,
}

#[derive(Debug, Clone)]
pub(crate) struct BuildArgs {
  dist: String,
  arch: Option<Vec<Arch>>,
  release: bool,
  copy_static: bool,
  cargo_args: Option<Vec<String>>,
  skip_libs: bool,
  dts_cache: bool,
  target_dir: Option<String>,
  skip_check: bool,
  zigbuild: bool,
  bisheng: bool,
  package: Option<String>,
  skip_napi_check: bool,
  soname: Option<String>,
}

#[derive(Debug, Clone)]
pub(crate) struct ArtifactArgs {
  dist: String,
  name: String,
  skip_libs: bool,
  package: Option<String>,
  no_workspace: bool,
}

#[derive(Debug, Clone)]
pub(crate) struct CargoArgs {
  arch: Option<Vec<Arch>>,
  bisheng: bool,
  disable_target: bool,
  args: Vec<String>,
  package: Option<String>,
  soname: Option<String>,
}

#[derive(Debug, Clone)]
pub(crate) enum Options {
  Init(InitArgs),
  Build(BuildArgs),
  Artifact(ArtifactArgs),
  Cargo(CargoArgs),
  #[allow(dead_code)]
  Publish,
  #[allow(dead_code)]
  Doctor,
}

fn main() {
  let parser = cli_run()
    .descr(cli::Info())
    .version(env!("CARGO_PKG_VERSION"));

  let ret = parser.fallback_to_usage().run();

  let run_ret = match ret {
    Options::Init(args) => init::init(args),
    Options::Build(args) => build::build(args),
    Options::Artifact(args) => artifact::artifact(args),
    Options::Cargo(args) => cargo::cargo(args),
    Options::Doctor => doctor::doctor(),
    Options::Publish => publish::publish(),
  };
  if let Err(e) = run_ret {
    println!("{:?}", e.red());
  }
}
