//! `pic whoami` — re-validates the saved token by hitting `/auth/me`
//! every time. Cached username in the credentials file is for
//! display-only contexts; this command is the source of truth.
//!
//! TSV output uses `KvBlock` (aligned `key: value` rows), JSON output
//! is a flat object — both downstream-friendly without the user having
//! to parse a headerless tab-line.

use anyhow::Result;
use picloud_shared::InstanceRole;

use crate::client::Client;
use crate::config;
use crate::output::{KvBlock, OutputMode};

pub async fn run(mode: OutputMode) -> Result<()> {
    let creds = config::resolve()?;
    let client = Client::from_creds(&creds)?;
    let me = client.auth_me().await?;
    let role = match me.instance_role {
        InstanceRole::Owner => "owner",
        InstanceRole::Admin => "admin",
        InstanceRole::Member => "member",
    };
    let email = me.email.as_deref().unwrap_or("-");
    let mut block = KvBlock::new();
    block
        .field("username", me.username)
        .field("role", role)
        .field("email", email)
        .field("url", creds.url.clone());
    block.print(mode);
    Ok(())
}
