//! Command line interface handling for the `voa` executable.

mod config;
mod import;
mod list;
mod verify;

use clap::{Parser, ValueEnum};
use clap_verbosity_flag::Verbosity;
pub use config::ConfigCommand;
use import::ImportCommand;
use list::ListCommand;

use crate::cli::verify::VerifyCommand;

/// Output format for `voa` commands with data output.
#[derive(Clone, Debug, Default, strum::Display, ValueEnum)]
#[strum(serialize_all = "kebab-case")]
pub enum OutputFormat {
    /// The JSON output format.
    Json,

    /// The default text output format.
    #[default]
    Text,
}

/// The top-level command line interface for the `voa` executable.
#[derive(Debug, Parser)]
#[command(
    about = "Command line interface for interacting with VOA hierarchies",
    author,
    long_about = None,
    version,
)]
pub struct Cli {
    /// The verbosity of the command.
    #[command(flatten)]
    pub verbosity: Verbosity,

    /// The commands of the `voa` executable.
    #[command(subcommand)]
    pub command: Command,
}

/// A command of the `voa` executable.
#[derive(Debug, Parser)]
#[command(about, author, version)]
pub enum Command {
    /// The config subcommand.
    #[command(
        about = "Interact with the VOA configuration for technology backends.",
        subcommand
    )]
    Config(ConfigCommand),

    /// The import subcommand.
    #[command(
        about = "Import a single verifier into a VOA hierarchy.",
        long_about = r#"Import a single verifier into a VOA hierarchy.

By default a single verifier is expected on stdin.
Using the "-i"/"--input" option a specific directory or file can be selected for import instead.

The verifier is written to the user's writable, persistent VOA load path, e.g.:

- "~/.config/voa/": for users with uid >= 1000.
  Note, that "voa import" respects the XDG Base Directory Specification and the "XDG_CONFIG_HOME" environment variable.
  The above serves as default example.
- "/etc/voa/": for users with uid < 1000

When using the "-r"/"--runtime" option, the verifier is written to the user's writable, ephemeral VOA load path instead, e.g.:

- "/run/user/$(id -u)/voa/": for users with uid >= 1000.
  Note, that "voa import" respects the XDG Base Directory Specification and the "XDG_RUNTIME_DIR" environment variable.
  The above serves as default example.
- "/run/voa": for users with uid < 1000

The verifier can be written to another, specific VOA base path using the "-b"/"--base-path" option."#
    )]
    Import(ImportCommand),

    /// The list subcommand.
    #[command(
        about = "List all verifiers in VOA that match provided identifiers.",
        long_about = r#"List all verifiers in VOA that match provided identifiers.

By default the "os" and "purpose" identifiers have to be provided for a search.
Optionally, the "context" and "technology" identifier can be provided for more fine-grained search results.
"#
    )]
    List(ListCommand),

    /// The verify subcommand.
    #[command(
        about = "Verify a file using suitable verifiers and signatures.",
        long_about = "Verify a file using suitable verifiers and signatures.

Returns a status message on stdout.

Returns a non-zero exit code and an error message on stderr, if the verification of one or more signature files fails."
    )]
    Verify(VerifyCommand),
}
