// ---------------------------------------------------------------------------
// whoami.ts — `vskill whoami` (0839 US-003 / T-009).
//
// Single-screen identity summary:
//   - The signed-in GitHub login (resolved via /user with the gho_*).
//   - The active token kind + 12-char prefix (vsk_/gho_).
//   - The active tenant slug (or "(none)").
//   - The list of tenants the user belongs to (slug + name + role).
//
// Anonymous users get "Not logged in. Run `vskill auth login`." and a
// non-zero exit so scripts can branch on it.
//
// Network calls are kept minimal — one /user (cached/short) and one
// /api/v1/account/tenants. Both are fed via dependency-injected fetchers
// so tests never hit the wire.
// ---------------------------------------------------------------------------
import { getDefaultKeychain, } from "../lib/keychain.js";
import { getActiveTenant, } from "../lib/active-tenant.js";
import { listTenants } from "../api/client.js";
function summarizeToken(token) {
    const kind = token.startsWith("vsk_") ? "vsk_" : "gho_";
    return {
        token,
        kind,
        prefix: token.slice(0, 12),
    };
}
async function defaultFetchGitHubUser(token, fetchImpl, version) {
    // Only `gho_*` works against api.github.com; `vsk_*` is verified-skill
    // territory. We still try /user with whatever we have because failures
    // are non-fatal for whoami — we just won't print a login.
    try {
        const res = await fetchImpl("https://api.github.com/user", {
            headers: {
                Accept: "application/vnd.github+json",
                Authorization: `Bearer ${token}`,
                "User-Agent": `vskill/${version}`,
            },
        });
        if (!res.ok)
            return null;
        return (await res.json());
    }
    catch {
        return null;
    }
}
export async function whoamiCommand(argv, deps) {
    const { io } = deps;
    void argv; // No flags yet; reserved for --json in a follow-up.
    const keychain = deps.keychain ?? getDefaultKeychain();
    const vsk = keychain.getVskillToken();
    const gho = keychain.getGitHubToken();
    if (!vsk && !gho) {
        io.stdout.write("Not logged in. Run `vskill auth login`.\n");
        if (deps.io.exit)
            deps.io.exit(1);
        return 1;
    }
    // Pick the auth token the API client would actually use (vsk_ wins).
    const apiToken = summarizeToken(vsk ?? gho);
    const githubToken = gho ?? null;
    // Resolve identity via GitHub /user (only meaningful when we have a gho_*).
    // The vsk_-only path still gets a useful whoami via the tenant list.
    let login = null;
    if (githubToken) {
        const fetchUser = deps.fetchGitHubUser
            ? deps.fetchGitHubUser
            : (token) => defaultFetchGitHubUser(token, deps.fetchImpl ?? fetch, deps.version ?? "vskill");
        try {
            const u = await fetchUser(githubToken);
            if (u && typeof u.login === "string")
                login = u.login;
        }
        catch {
            login = null;
        }
    }
    // Tenant info — both the active slug (local) and the full list.
    const active = getActiveTenant(deps.activeTenantOptions);
    let tenants = [];
    let tenantsErr = null;
    try {
        tenants = await (deps.listTenants ?? listTenants)();
    }
    catch (err) {
        tenantsErr = err.message ?? String(err);
    }
    // ----- Render ------------------------------------------------------------
    if (login) {
        io.stdout.write(`Logged in as @${login}\n`);
    }
    else {
        io.stdout.write(`Logged in (no GitHub /user response)\n`);
    }
    io.stdout.write(`Token: ${apiToken.kind}* (${apiToken.prefix}…)\n`);
    if (vsk && gho) {
        // Both stored — useful diagnostic.
        io.stdout.write(`  GitHub token: gho_* (${gho.slice(0, 12)}…)\n`);
    }
    io.stdout.write(`Active tenant: ${active ?? "(none)"}\n`);
    if (tenantsErr) {
        io.stdout.write(`Tenants: (failed to fetch — ${tenantsErr})\n`);
    }
    else if (tenants.length === 0) {
        io.stdout.write(`Tenants: (none)\n`);
    }
    else {
        io.stdout.write(`Tenants:\n`);
        for (const t of tenants) {
            const marker = t.slug === active ? "*" : " ";
            io.stdout.write(`  ${marker} ${t.slug} — ${t.name} (${t.role})\n`);
        }
    }
    if (deps.io.exit)
        deps.io.exit(0);
    return 0;
}
//# sourceMappingURL=whoami.js.map