use anyhow::{bail, Result};

pub mod admins;
pub mod api_keys;
pub mod apply;
pub mod apps;
pub mod apps_domains;
pub mod collections;
pub mod config;
pub mod dead_letters;
pub mod docs;
pub mod extension_points;
pub mod files;
pub mod groups;
pub mod init;
pub mod kv;
pub mod login;
pub mod logout;
pub mod logs;
pub mod members;
pub mod plan;
pub mod projects;
pub mod pull;
pub mod queues;
pub mod routes;
pub mod scripts;
pub mod secrets;
pub mod suppress;
pub mod topics;
pub mod triggers;
pub mod users;
pub mod vars;
pub mod whoami;
pub mod workflows;

/// A resolved owner reference for a command that targets EITHER an app or a
/// group (the `--app` / `--group` XOR). See [`require_one_owner`].
#[derive(Debug)]
pub enum OwnerRef<'a> {
    App(&'a str),
    Group(&'a str),
}

/// Enforce the `--app` / `--group` XOR with ONE canonical message across every
/// owner-polymorphic command (kv, files, vars, secrets, scripts, triggers,
/// routes, suppress, extension-points, …), so the error reads the same
/// everywhere instead of five slightly different wordings.
pub fn require_one_owner<'a>(app: Option<&'a str>, group: Option<&'a str>) -> Result<OwnerRef<'a>> {
    match (app, group) {
        (Some(a), None) => Ok(OwnerRef::App(a)),
        (None, Some(g)) => Ok(OwnerRef::Group(g)),
        (Some(_), Some(_)) => bail!("provide exactly one of --app or --group, not both"),
        (None, None) => bail!("provide exactly one of --app or --group"),
    }
}
