//! `pic extension-points ls --app|--group` — read-only view of a node's
//! §5.5 extension points. For an app, each EP visible on its chain is shown
//! with its resolved provider (`app override` / `inherited default` / `unset`);
//! for a group, its own declared names. Authoring is declarative only (the
//! manifest `extension_points = [...]`).

use anyhow::Result;

use crate::client::{Client, NodeKind};
use crate::cmds::OwnerRef;
use crate::config;
use crate::output::{OutputMode, Table};

pub async fn ls(app: Option<&str>, group: Option<&str>, mode: OutputMode) -> Result<()> {
    let (kind, ident) = match crate::cmds::require_one_owner(app, group)? {
        OwnerRef::App(a) => (NodeKind::App, a),
        OwnerRef::Group(g) => (NodeKind::Group, g),
    };
    let creds = config::resolve()?;
    let client = Client::from_creds(&creds)?;
    let items = client.extension_points_list(kind, ident).await?;

    match kind {
        NodeKind::App => {
            let mut table = Table::new(["name", "source", "provider"]);
            for ep in &items {
                table.row([
                    ep.name.clone(),
                    if ep.declared_here {
                        "declared".into()
                    } else {
                        "inherited".into()
                    },
                    ep.provider.clone().unwrap_or_else(|| "unset".into()),
                ]);
            }
            table.print(mode);
        }
        NodeKind::Group => {
            let mut table = Table::new(["name"]);
            for ep in &items {
                table.row([ep.name.clone()]);
            }
            table.print(mode);
        }
    }
    Ok(())
}
