use bpaf::{construct, long, positional, Parser};

use crate::util::Arch;

pub fn cli_build() -> impl Parser<crate::Options> {
  let dist = long("dist")
    .argument::<String>("DIST")
    .help("The path of the final build product is set to `dist` by default.")
    .fallback(String::from("dist"));

  let release = long("release")
    .help("Build with release mode.")
    .flag(true, false);

  let arch = long("arch")
    .short('a')
    .help("The target build products support arm64/aarch, arm/arm32, x86_64/x64 and loongarch64 architectures, with arm64/arm/x64 enabled by default.")
    .argument::<Arch>("ARCH")
    .some("Please provide at least one architecture")
    .optional()
    .fallback(Some([Arch::ARM64,Arch::ARM32,Arch::X86_64].to_vec()));

  let copy_static = long("static")
    .help("Copy the static link library to the final output directory, will be set to false by default.")
    .flag(true, false);

  let cargo_args = positional("CARGO_ARGS")
    .help("The custom parameters for cargo build in the current project.")
    .strict()
    .many()
    .optional();

  let skip_libs = long("skip-libs")
    .help("Do not copy the dynamic link library to the final output directory, will be set to false by default.")
    .flag(true, false);

  let dts_cache = long("dts-cache")
    .help(
      "Use the dts cache file to generate the final output file, will be set to true by default.",
    )
    .flag(true, true);

  let skip_check = long("skip-check")
    .help("Skip the check of the version of the napi-ohos, will be set to true by default.")
    .flag(true, true);

  let target_dir = long("target-dir")
    .help("The temp directory of the ohrs build, will be set to the current directory by default.")
    .argument::<String>("TARGET_DIR")
    .optional();

  let zigbuild = long("zigbuild")
    .help("Use zigbuild to build the project, will be set to false by default.")
    .flag(true, false);

  let bisheng = long("bisheng")
    .help("Use bisheng to build the project, will be set to false by default.")
    .flag(true, false);

  let skip_napi_check = long("skip-napi-check")
    .help("Skip the check if the napi-ohos and napi-derive-ohos dependencies are installed, will be set to true by default.")
    .flag(true, true);

  let package = long("package")
    .short('p')
    .help(
      "Package to build (workspace mode only). Can also be specified via cargo args: -- -p package",
    )
    .argument::<String>("PACKAGE")
    .optional();

  let soname = long("soname")
    .help("Set the SONAME for the generated shared library (e.g., libexample.so.1).")
    .argument::<String>("SONAME")
    .optional();

  let init_parser = construct!(crate::BuildArgs {
    dist,
    arch,
    release,
    copy_static,
    skip_libs,
    dts_cache,
    skip_check,
    zigbuild,
    bisheng,
    target_dir,
    package,
    skip_napi_check,
    soname,
    cargo_args,
  });
  construct!(crate::Options::Build(init_parser))
}
