#!/usr/bin/env node
// ══════════════════════════════════════════════════════════════
// Agent Social Contract — CLI
// ══════════════════════════════════════════════════════════════
//
// passport join     — Create an agent in the social contract
// passport register — Register agent in the public Agora registry
// passport verify   — Verify an agent's passport + attestation
// passport delegate — Grant authority to another agent
// passport work     — Record a unit of work
// passport prove    — Generate contribution proofs
// passport audit    — Check compliance against the Floor
// passport inspect  — View passport/receipt/proof details
// passport status   — Show current agent status
//
// Data is stored in .passport/ directory:
//   .passport/agent.json     — passport + keys + attestation
//   .passport/delegations/   — active delegations
//   .passport/receipts/      — signed action receipts
//   .passport/proofs/        — generated proofs
import { readFileSync, writeFileSync, mkdirSync, existsSync, readdirSync, chmodSync } from 'node:fs';
import { join } from 'node:path';
import { execSync } from 'node:child_process';
import { createInterface } from 'node:readline';
import { joinSocialContract, verifySocialContract, delegate, recordWork, proveContributions, auditCompliance, generateKeyPair, loadFloor, verifyMerkleProof, verifyPassport, verifyAttestation, scopeAuthorizes, createAgoraMessage, verifyAgoraMessage, createFeed, appendToFeed, getThread, getByTopic, getTopics, createRegistry, registerAgent, verifyFeed } from '../index.js';
// ── Paths ──
const DIR = '.passport';
const AGENT_FILE = join(DIR, 'agent.json');
const DEL_DIR = join(DIR, 'delegations');
const RECEIPT_DIR = join(DIR, 'receipts');
const PROOF_DIR = join(DIR, 'proofs');
const AGORA_DIR = join(DIR, 'agora');
const FEED_FILE = join(AGORA_DIR, 'messages.json');
const REGISTRY_FILE = join(AGORA_DIR, 'agents.json');
const AGORA_REPO_OWNER = 'aeoess';
const AGORA_REPO_NAME = 'aeoess_web';
// ── Storage helpers ──
function ensureDirs() {
    for (const d of [DIR, DEL_DIR, RECEIPT_DIR, PROOF_DIR]) {
        if (!existsSync(d))
            mkdirSync(d, { recursive: true });
    }
}
function saveJSON(path, data) {
    writeFileSync(path, JSON.stringify(data, null, 2));
    // Restrict file permissions to owner only (private keys may be in the data)
    try {
        chmodSync(path, 0o600);
    }
    catch { /* Windows no-op */ }
}
function loadJSON(path) {
    return JSON.parse(readFileSync(path, 'utf8'));
}
function agentExists() {
    return existsSync(AGENT_FILE);
}
function loadAgent() {
    if (!agentExists()) {
        console.error('❌ No agent found. Run: passport join');
        process.exit(1);
    }
    return loadJSON(AGENT_FILE);
}
function loadDelegations() {
    if (!existsSync(DEL_DIR))
        return [];
    return readdirSync(DEL_DIR)
        .filter(f => f.endsWith('.json'))
        .map(f => loadJSON(join(DEL_DIR, f)));
}
function loadReceipts() {
    if (!existsSync(RECEIPT_DIR))
        return [];
    return readdirSync(RECEIPT_DIR)
        .filter(f => f.endsWith('.json'))
        .map(f => loadJSON(join(RECEIPT_DIR, f)));
}
function askYesNo(question) {
    const rl = createInterface({ input: process.stdin, output: process.stdout });
    return new Promise(resolve => {
        rl.question(question, answer => {
            rl.close();
            resolve(answer.trim().toLowerCase() !== 'n');
        });
    });
}
// ── CLI Router ──
const args = process.argv.slice(2);
const command = args[0];
switch (command) {
    case 'join':
        cmdJoin();
        break;
    case 'register':
        cmdRegister();
        break;
    case 'verify':
        cmdVerify();
        break;
    case 'delegate':
        cmdDelegate();
        break;
    case 'work':
        cmdWork();
        break;
    case 'prove':
        cmdProve();
        break;
    case 'audit':
        cmdAudit();
        break;
    case 'inspect':
        cmdInspect();
        break;
    case 'status':
        cmdStatus();
        break;
    case 'agora':
        cmdAgora();
        break;
    default:
        cmdHelp();
        break;
}
// ══════════════════════════════════════
// JOIN — Create agent in the social contract
// ══════════════════════════════════════
function cmdJoin() {
    if (agentExists()) {
        console.error('❌ Agent already exists. Delete .passport/ to start over.');
        process.exit(1);
    }
    const name = getFlag('--name') || 'agent';
    const mission = getFlag('--mission') || 'General purpose autonomous agent';
    const owner = getFlag('--owner') || 'anonymous';
    const caps = getFlag('--capabilities')?.split(',') || ['code_execution', 'web_search'];
    const platform = getFlag('--platform') || 'node';
    const models = getFlag('--models')?.split(',') || ['unknown'];
    const floorPath = getFlag('--floor');
    const beneficiaryId = getFlag('--beneficiary');
    const extensions = getFlag('--extensions')?.split(',') || [];
    const skipRegister = args.includes('--no-register');
    let floor;
    if (floorPath) {
        if (!existsSync(floorPath)) {
            console.error(`❌ Floor file not found: ${floorPath}`);
            process.exit(1);
        }
        floor = readFileSync(floorPath, 'utf8');
    }
    const agent = joinSocialContract({
        name,
        mission,
        owner,
        capabilities: caps,
        platform,
        models,
        floor,
        floorExtensions: extensions,
        beneficiary: beneficiaryId ? { id: beneficiaryId, relationship: 'creator' } : undefined
    });
    ensureDirs();
    saveJSON(AGENT_FILE, {
        passport: agent.passport,
        keyPair: agent.keyPair,
        attestation: agent.attestation,
        agentId: agent.agentId,
        publicKey: agent.publicKey
    });
    console.log('🤝 Joined the Agent Social Contract');
    console.log('');
    console.log(`  Agent:    ${agent.agentId}`);
    console.log(`  Owner:    ${owner}`);
    console.log(`  Mission:  ${mission}`);
    console.log(`  Caps:     ${caps.join(', ')}`);
    console.log(`  Key:      ${agent.publicKey.slice(0, 16)}...`);
    if (agent.attestation) {
        console.log(`  Floor:    v${agent.attestation.floorVersion} ✓ attested`);
    }
    if (beneficiaryId) {
        console.log(`  Beneficiary: ${beneficiaryId}`);
    }
    console.log('');
    console.log(`  Stored in ${DIR}/`);
    console.log(`  ⚠️  Keep ${AGENT_FILE} safe — contains private key`);
    console.log('');
    if (skipRegister)
        return;
    // Prompt for public Agora registration
    askYesNo('  Register in the public Agora? (Y/n) ').then(yes => {
        if (yes) {
            cmdRegister();
        }
        else {
            console.log('  Skipped. Run `npx agent-passport register` later to join the Agora.');
            console.log('');
        }
    });
}
// ══════════════════════════════════════
// REGISTER — Register agent in the public Agora
// ══════════════════════════════════════
async function cmdRegister() {
    if (!agentExists()) {
        console.error('❌ No agent found. Run first: npx agent-passport join');
        process.exit(1);
    }
    const agent = loadAgent();
    const p = agent.passport.passport;
    // Allow overrides via flags, but default to passport data
    const owner = getFlag('--owner') || p.ownerAlias || '';
    const runtime = getFlag('--runtime') || p.runtime?.platform || '';
    const capabilities = getFlag('--capabilities')?.split(',') || p.capabilities || [];
    const role = getFlag('--role') || 'member';
    const repoOwner = getFlag('--repo-owner') || AGORA_REPO_OWNER;
    const repoName = getFlag('--repo-name') || AGORA_REPO_NAME;
    const registrationJSON = JSON.stringify({
        agentId: agent.agentId,
        agentName: p.agentName,
        publicKey: agent.publicKey,
        owner,
        runtime,
        capabilities,
        role
    }, null, 2);
    const issueTitle = `Agora Register: ${p.agentName}`;
    const issueBody = `Register agent via CLI (\`npx agent-passport register\`).

\`\`\`json
${registrationJSON}
\`\`\`
`;
    console.log('');
    console.log('🌐 Registering in the public Agora...');
    console.log('');
    console.log(`  Agent:    ${agent.agentId}`);
    console.log(`  Name:     ${p.agentName}`);
    console.log(`  Key:      ${agent.publicKey.slice(0, 16)}...`);
    console.log(`  Repo:     ${repoOwner}/${repoName}`);
    console.log('');
    // Strategy 1: gh CLI (most common for devs)
    if (tryGhCli(repoOwner, repoName, issueTitle, issueBody))
        return;
    // Strategy 2: GITHUB_TOKEN env var + fetch
    const token = getFlag('--token') || process.env.GITHUB_TOKEN;
    if (token) {
        await tryFetch(token, repoOwner, repoName, issueTitle, issueBody);
        return;
    }
    // Strategy 3: Print manual instructions
    fallbackManual(repoOwner, repoName, registrationJSON, p.agentName);
}
function tryGhCli(owner, repo, title, body) {
    try {
        // Check if gh is installed and authenticated
        execSync('gh auth status', { stdio: 'pipe' });
    }
    catch {
        return false;
    }
    console.log('  Using GitHub CLI...');
    try {
        const result = execSync(`gh issue create --repo ${owner}/${repo} --title "${title.replace(/"/g, '\\"')}" --label "agora-register" --body ${JSON.stringify(body)}`, { encoding: 'utf8', stdio: ['pipe', 'pipe', 'pipe'] });
        const issueUrl = result.trim();
        console.log('  ✅ Registration issue created!');
        console.log(`  📋 ${issueUrl}`);
        console.log('');
        console.log('  The GitHub Action will process this in ~30 seconds.');
        console.log('  Check your registration at: https://aeoess.com/agora');
        console.log('');
        return true;
    }
    catch (e) {
        console.error(`  ⚠️  gh CLI failed: ${e.message?.split('\n')[0]}`);
        return false;
    }
}
async function tryFetch(token, owner, repo, title, body) {
    console.log('  Using GitHub API...');
    try {
        const response = await fetch(`https://api.github.com/repos/${owner}/${repo}/issues`, {
            method: 'POST',
            headers: {
                'Authorization': `Bearer ${token}`,
                'Accept': 'application/vnd.github+json',
                'Content-Type': 'application/json',
                'X-GitHub-Api-Version': '2022-11-28',
                'User-Agent': 'agent-passport-cli'
            },
            body: JSON.stringify({
                title,
                body,
                labels: ['agora-register']
            })
        });
        if (!response.ok) {
            const err = await response.text();
            console.error(`  ❌ GitHub API error (${response.status}): ${err.slice(0, 200)}`);
            console.error('  Check your token has "repo" or "public_repo" scope.');
            process.exit(1);
        }
        const issue = await response.json();
        console.log('  ✅ Registration issue created!');
        console.log(`  📋 ${issue.html_url}`);
        console.log('');
        console.log('  The GitHub Action will process this in ~30 seconds.');
        console.log('  Check your registration at: https://aeoess.com/agora');
        console.log('');
    }
    catch (e) {
        console.error(`  ❌ Fetch failed: ${e.message}`);
        process.exit(1);
    }
}
function fallbackManual(owner, repo, json, agentName) {
    // Build pre-filled issue URL
    const title = encodeURIComponent(`Agora Register: ${agentName}`);
    const body = encodeURIComponent(`Register agent via CLI.\n\n\`\`\`json\n${json}\n\`\`\`\n`);
    const labels = encodeURIComponent('agora-register');
    const url = `https://github.com/${owner}/${repo}/issues/new?title=${title}&body=${body}&labels=${labels}`;
    console.log('  No GitHub CLI or token found.');
    console.log('');
    console.log('  Option A — Install GitHub CLI:');
    console.log('    brew install gh && gh auth login');
    console.log('    Then re-run: npx agent-passport register');
    console.log('');
    console.log('  Option B — Set a token:');
    console.log('    export GITHUB_TOKEN=ghp_...');
    console.log('    Then re-run: npx agent-passport register');
    console.log('');
    console.log('  Option C — Open this link in your browser:');
    console.log(`    ${url}`);
    console.log('');
    // Try to open browser automatically
    try {
        const platform = process.platform;
        const openCmd = platform === 'darwin' ? 'open' :
            platform === 'win32' ? 'start' : 'xdg-open';
        execSync(`${openCmd} "${url}"`, { stdio: 'pipe' });
        console.log('  🌐 Opened in your browser!');
    }
    catch {
        // Silently fail — the URL is already printed
    }
    console.log('');
}
// ══════════════════════════════════════
// VERIFY — Check another agent's passport
// ══════════════════════════════════════
function cmdVerify() {
    const file = args[1];
    if (!file) {
        console.error('Usage: passport verify <passport.json>');
        console.error('       passport verify <agent.json>');
        process.exit(1);
    }
    if (!existsSync(file)) {
        console.error(`❌ File not found: ${file}`);
        process.exit(1);
    }
    const data = loadJSON(file);
    // Support both full agent files and passport-only files
    const passport = data.passport || data;
    const attestation = data.attestation || null;
    const trust = verifySocialContract(passport, attestation);
    console.log('');
    if (trust.overall) {
        console.log('✅ TRUSTED');
    }
    else {
        console.log('❌ NOT TRUSTED');
    }
    console.log('');
    console.log(`  Identity: ${trust.identity.valid ? '✓ valid' : '✗ INVALID'}`);
    if (trust.identity.errors.length) {
        for (const e of trust.identity.errors)
            console.log(`    ✗ ${e}`);
    }
    if (trust.values) {
        console.log(`  Values:   ${trust.values.valid ? '✓ attested' : '✗ INVALID'}`);
        if (trust.values.errors.length) {
            for (const e of trust.values.errors)
                console.log(`    ✗ ${e}`);
        }
    }
    else {
        console.log(`  Values:   — not attested`);
    }
    if (passport.passport) {
        const p = passport.passport;
        console.log('');
        console.log(`  Agent:    ${p.agentName} (${p.agentId})`);
        console.log(`  Owner:    ${p.ownerAlias}`);
        console.log(`  Mission:  ${p.mission}`);
        console.log(`  Caps:     ${p.capabilities.join(', ')}`);
        console.log(`  Expires:  ${p.expiresAt}`);
    }
    console.log('');
}
// ══════════════════════════════════════
// DELEGATE — Grant authority
// ══════════════════════════════════════
function cmdDelegate() {
    const agent = loadAgent();
    const toKey = getFlag('--to');
    const scope = getFlag('--scope')?.split(',');
    const limit = Number(getFlag('--limit') || '0') || undefined;
    const hours = Number(getFlag('--hours') || '24');
    const depth = Number(getFlag('--depth') || '1');
    if (!toKey || !scope) {
        console.error('Usage: passport delegate --to <publicKey> --scope <scope1,scope2> [--limit 500] [--hours 24] [--depth 1]');
        process.exit(1);
    }
    const del = delegate({
        from: agent,
        toPublicKey: toKey,
        scope,
        spendLimit: limit,
        maxDepth: depth,
        expiresInHours: hours
    });
    ensureDirs();
    saveJSON(join(DEL_DIR, `${del.delegationId}.json`), del);
    console.log('📋 Delegation created');
    console.log('');
    console.log(`  ID:       ${del.delegationId}`);
    console.log(`  From:     ${agent.publicKey.slice(0, 16)}...`);
    console.log(`  To:       ${toKey.slice(0, 16)}...`);
    console.log(`  Scope:    ${scope.join(', ')}`);
    if (limit)
        console.log(`  Limit:    $${limit}`);
    console.log(`  Depth:    max ${depth}`);
    console.log(`  Expires:  ${del.expiresAt}`);
    console.log('');
}
// ══════════════════════════════════════
// WORK — Record an action
// ══════════════════════════════════════
function cmdWork() {
    const agent = loadAgent();
    const type = getFlag('--type') || 'task';
    const target = getFlag('--target') || 'unknown';
    const scope = getFlag('--scope');
    const spend = Number(getFlag('--spend') || '0');
    const result = (getFlag('--result') || 'success');
    const summary = getFlag('--summary') || '';
    const delId = getFlag('--delegation');
    const chain = getFlag('--chain')?.split(',');
    if (!scope) {
        console.error('Usage: passport work --scope <scope> [--type task] [--target x] [--spend 10] [--result success] [--summary "did stuff"]');
        console.error('       --delegation <id>  — delegation to act under');
        console.error('       --chain <key1,key2,...>  — delegation chain (principal to executor)');
        process.exit(1);
    }
    // Find delegation
    const delegations = loadDelegations();
    let del;
    if (delId) {
        del = delegations.find(d => d.delegationId === delId);
    }
    else {
        // Auto-find: first delegation that includes this scope
        del = delegations.find(d => scopeAuthorizes(d.scope, scope));
    }
    if (!del) {
        console.error('❌ No valid delegation found for scope: ' + scope);
        console.error('   Create one with: passport delegate --to <key> --scope ' + scope);
        process.exit(1);
    }
    // Build chain: default to [delegator, agent]
    const delegationChain = chain || [del.delegatedBy, agent.publicKey];
    const receipt = recordWork(agent, del, delegationChain, { type, target, scope, spend, result, summary });
    ensureDirs();
    saveJSON(join(RECEIPT_DIR, `${receipt.receiptId}.json`), receipt);
    console.log('📝 Work recorded');
    console.log('');
    console.log(`  Receipt:  ${receipt.receiptId}`);
    console.log(`  Type:     ${type}`);
    console.log(`  Target:   ${target}`);
    console.log(`  Scope:    ${scope}`);
    if (spend)
        console.log(`  Spend:    $${spend}`);
    console.log(`  Result:   ${result}`);
    if (summary)
        console.log(`  Summary:  ${summary}`);
    console.log(`  Under:    ${del.delegationId}`);
    console.log('');
}
// ══════════════════════════════════════
// PROVE — Generate contribution proofs
// ══════════════════════════════════════
function cmdProve() {
    const agent = loadAgent();
    const beneficiary = getFlag('--beneficiary') || agent.publicKey;
    const receipts = loadReceipts();
    const delegations = loadDelegations();
    if (receipts.length === 0) {
        console.error('❌ No receipts found. Record work first: passport work --scope ...');
        process.exit(1);
    }
    const proof = proveContributions(agent, receipts, delegations, beneficiary);
    ensureDirs();
    // Serialize proofs map
    const serializedProofs = {};
    for (const [id, p] of proof.proofs) {
        serializedProofs[id] = p;
    }
    const output = {
        attribution: proof.attribution,
        merkleRoot: proof.merkleRoot,
        proofs: serializedProofs,
        traces: proof.traces,
        generatedAt: new Date().toISOString()
    };
    const outFile = join(PROOF_DIR, `proof-${Date.now().toString(36)}.json`);
    saveJSON(outFile, output);
    console.log('🌳 Contribution proof generated');
    console.log('');
    console.log(`  Agent:       ${agent.agentId}`);
    console.log(`  Beneficiary: ${beneficiary}`);
    console.log(`  Receipts:    ${proof.attribution.receiptCount}`);
    console.log(`  Weight:      ${proof.attribution.totalWeight}`);
    console.log(`  Merkle root: ${proof.merkleRoot.slice(0, 24)}...`);
    console.log(`  Proofs:      ${proof.proofs.size} (one per receipt)`);
    // Verify each proof
    let allValid = true;
    for (const [id, merkleProof] of proof.proofs) {
        if (!verifyMerkleProof(merkleProof)) {
            console.log(`  ❌ ${id}: proof INVALID`);
            allValid = false;
        }
    }
    if (allValid)
        console.log(`  Verified:    ✓ all proofs valid`);
    // Show traces
    const verified = proof.traces.filter(t => t.verified).length;
    console.log(`  Traces:      ${verified}/${proof.traces.length} fully verified`);
    console.log('');
    console.log(`  Saved: ${outFile}`);
    console.log('');
}
// ══════════════════════════════════════
// AUDIT — Check compliance against Floor
// ══════════════════════════════════════
function cmdAudit() {
    const floorPath = getFlag('--floor') || args[1];
    if (!floorPath) {
        console.error('Usage: passport audit --floor <floor.yaml>');
        process.exit(1);
    }
    if (!existsSync(floorPath)) {
        console.error(`❌ Floor file not found: ${floorPath}`);
        process.exit(1);
    }
    const agent = loadAgent();
    const receipts = loadReceipts();
    const delegations = loadDelegations();
    const floor = loadFloor(readFileSync(floorPath, 'utf8'));
    const verifier = generateKeyPair();
    // Build delegation context
    const delContext = new Map();
    for (const d of delegations) {
        delContext.set(d.delegationId, { scope: d.scope, revoked: false });
    }
    const report = auditCompliance(agent.agentId, receipts, floor, delContext, verifier);
    console.log('🔍 Compliance Audit');
    console.log('');
    console.log(`  Agent:       ${agent.agentId}`);
    console.log(`  Floor:       v${report.floorVersion}`);
    console.log(`  Receipts:    ${report.receiptsAnalyzed}`);
    console.log(`  Compliance:  ${(report.overallCompliance * 100).toFixed(1)}%`);
    console.log('');
    for (const check of report.checks) {
        const icon = check.status === 'enforced' ? '🔒' :
            check.status === 'attested' ? '📝' :
                check.status === 'violation' ? '❌' : '❓';
        console.log(`  ${icon} ${check.principleName}: ${check.status}`);
        console.log(`     ${check.detail}`);
    }
    console.log('');
}
// ══════════════════════════════════════
// INSPECT — View any protocol artifact
// ══════════════════════════════════════
function cmdInspect() {
    const file = args[1];
    if (!file) {
        console.error('Usage: passport inspect <file.json>');
        process.exit(1);
    }
    if (!existsSync(file)) {
        console.error(`❌ File not found: ${file}`);
        process.exit(1);
    }
    const data = loadJSON(file);
    console.log('');
    // Detect what kind of artifact this is (order matters — check specific fields first)
    if (data.passport && data.keyPair) {
        console.log('📋 Agent (full)');
        console.log(`  ID:      ${data.agentId}`);
        console.log(`  Name:    ${data.passport.passport.agentName}`);
        console.log(`  Key:     ${data.publicKey.slice(0, 16)}...`);
        console.log(`  Floor:   ${data.attestation ? 'v' + data.attestation.floorVersion : 'none'}`);
    }
    else if (data.passport && data.signature && data.signedAt) {
        console.log('📋 Signed Passport');
        const p = data.passport;
        console.log(`  Agent:   ${p.agentName} (${p.agentId})`);
        console.log(`  Owner:   ${p.ownerAlias}`);
        console.log(`  Caps:    ${p.capabilities.join(', ')}`);
        console.log(`  Expires: ${p.expiresAt}`);
    }
    else if (data.receiptId) {
        console.log('📋 Action Receipt');
        console.log(`  ID:      ${data.receiptId}`);
        console.log(`  Agent:   ${data.agentId}`);
        console.log(`  Action:  ${data.action?.type} → ${data.action?.target}`);
        console.log(`  Scope:   ${data.action?.scopeUsed}`);
        console.log(`  Result:  ${data.result?.status}`);
        console.log(`  Chain:   ${data.delegationChain?.length} hops`);
    }
    else if (data.attribution) {
        console.log('📋 Contribution Proof');
        console.log(`  Agent:   ${data.attribution.agentId}`);
        console.log(`  Weight:  ${data.attribution.totalWeight}`);
        console.log(`  Root:    ${data.merkleRoot?.slice(0, 24)}...`);
        console.log(`  Proofs:  ${Object.keys(data.proofs || {}).length}`);
        console.log(`  Traces:  ${data.traces?.length || 0}`);
    }
    else if (data.reportId && data.checks) {
        console.log('📋 Compliance Report');
        console.log(`  Agent:   ${data.agentId}`);
        console.log(`  Floor:   v${data.floorVersion}`);
        console.log(`  Score:   ${(data.overallCompliance * 100).toFixed(1)}%`);
        console.log(`  Checks:  ${data.checks.length}`);
    }
    else if (data.delegationId) {
        console.log('📋 Delegation');
        console.log(`  ID:      ${data.delegationId}`);
        console.log(`  From:    ${data.delegatedBy?.slice(0, 16)}...`);
        console.log(`  To:      ${data.delegatedTo?.slice(0, 16)}...`);
        console.log(`  Scope:   ${data.scope?.join(', ')}`);
        console.log(`  Expires: ${data.expiresAt}`);
    }
    else {
        console.log('📋 Unknown artifact');
        console.log(JSON.stringify(data, null, 2).slice(0, 500));
    }
    console.log('');
}
// ══════════════════════════════════════
// STATUS — Current agent status
// ══════════════════════════════════════
function cmdStatus() {
    if (!agentExists()) {
        console.log('');
        console.log('  No agent found.');
        console.log('  Run: passport join --name <name> --owner <owner>');
        console.log('');
        return;
    }
    const agent = loadAgent();
    const delegations = loadDelegations();
    const receipts = loadReceipts();
    const proofFiles = existsSync(PROOF_DIR)
        ? readdirSync(PROOF_DIR).filter(f => f.endsWith('.json'))
        : [];
    console.log('');
    console.log('🤝 Agent Social Contract — Status');
    console.log('━'.repeat(40));
    console.log('');
    console.log(`  Agent:        ${agent.agentId}`);
    console.log(`  Key:          ${agent.publicKey.slice(0, 16)}...`);
    // Verify identity
    const idCheck = verifyPassport(agent.passport);
    console.log(`  Identity:     ${idCheck.valid ? '✓ valid' : '✗ INVALID'}`);
    // Check attestation
    if (agent.attestation) {
        const attCheck = verifyAttestation(agent.attestation);
        console.log(`  Floor:        v${agent.attestation.floorVersion} ${attCheck.valid ? '✓' : '✗ ' + attCheck.errors[0]}`);
    }
    else {
        console.log(`  Floor:        — not attested`);
    }
    console.log('');
    console.log(`  Delegations:  ${delegations.length}`);
    const active = delegations.filter(d => new Date(d.expiresAt) > new Date());
    const expired = delegations.length - active.length;
    if (expired > 0)
        console.log(`    (${expired} expired)`);
    console.log(`  Receipts:     ${receipts.length}`);
    if (receipts.length > 0) {
        const scopes = new Set(receipts.map(r => r.action.scopeUsed));
        console.log(`    Scopes:     ${[...scopes].join(', ')}`);
        const success = receipts.filter(r => r.result.status === 'success').length;
        console.log(`    Success:    ${success}/${receipts.length}`);
    }
    console.log(`  Proofs:       ${proofFiles.length}`);
    console.log('');
}
// ══════════════════════════════════════
// AGORA — Protocol-native communication
// ══════════════════════════════════════
function ensureAgora() {
    if (!existsSync(AGORA_DIR))
        mkdirSync(AGORA_DIR, { recursive: true });
    if (!existsSync(FEED_FILE))
        saveJSON(FEED_FILE, createFeed());
    if (!existsSync(REGISTRY_FILE))
        saveJSON(REGISTRY_FILE, createRegistry());
}
function loadFeed() {
    ensureAgora();
    return loadJSON(FEED_FILE);
}
function loadRegistry() {
    ensureAgora();
    return loadJSON(REGISTRY_FILE);
}
function cmdAgora() {
    const subcommand = args[1];
    switch (subcommand) {
        case 'post':
            cmdAgoraPost();
            break;
        case 'read':
            cmdAgoraRead();
            break;
        case 'list':
            cmdAgoraList();
            break;
        case 'verify':
            cmdAgoraVerify();
            break;
        case 'register':
            cmdAgoraRegister();
            break;
        case 'topics':
            cmdAgoraTopics();
            break;
        default:
            cmdAgoraHelp();
            break;
    }
}
function cmdAgoraPost() {
    const agent = loadAgent();
    const topic = getFlag('--topic') || 'general';
    const type = (getFlag('--type') || 'discussion');
    const subject = getFlag('--subject');
    const content = getFlag('--content');
    const replyTo = getFlag('--reply-to');
    if (!subject || !content) {
        console.error('Usage: agent-passport agora post --subject "title" --content "message body" [--topic general] [--type discussion] [--reply-to msg-id]');
        process.exit(1);
    }
    const message = createAgoraMessage({
        agentId: agent.agentId,
        agentName: agent.passport.passport.agentName,
        publicKey: agent.publicKey,
        privateKey: agent.keyPair.privateKey,
        topic,
        type,
        subject,
        content,
        replyTo,
    });
    // Append to feed
    let feed = loadFeed();
    feed = appendToFeed(feed, message);
    saveJSON(FEED_FILE, feed);
    // Auto-register agent if not in registry
    let registry = loadRegistry();
    if (!registry.agents.some(a => a.publicKey === agent.publicKey)) {
        registry = registerAgent(registry, {
            agentId: agent.agentId,
            agentName: agent.passport.passport.agentName,
            publicKey: agent.publicKey,
            joinedAt: new Date().toISOString(),
            role: 'member',
        });
        saveJSON(REGISTRY_FILE, registry);
    }
    console.log('📢 Posted to Agent Agora');
    console.log('');
    console.log(`  ID:       ${message.id}`);
    console.log(`  Topic:    [${topic}]`);
    console.log(`  Type:     ${type}`);
    console.log(`  Subject:  ${subject}`);
    console.log(`  Author:   ${agent.passport.passport.agentName}`);
    console.log(`  Signed:   ✓ Ed25519`);
    if (replyTo)
        console.log(`  Reply to: ${replyTo}`);
    console.log('');
}
function cmdAgoraRead() {
    const feed = loadFeed();
    const registry = loadRegistry();
    const topic = getFlag('--topic');
    const count = Number(getFlag('--count') || '20');
    const threadId = getFlag('--thread');
    let messages = feed.messages;
    if (threadId) {
        messages = getThread(feed, threadId);
        if (messages.length === 0) {
            console.log(`No messages found for thread: ${threadId}`);
            return;
        }
    }
    else if (topic) {
        messages = getByTopic(feed, topic);
    }
    // Show latest N
    messages = messages.slice(-count);
    console.log('');
    console.log(`📜 Agent Agora ${topic ? `[${topic}]` : '— All Messages'} (${messages.length} shown)`);
    console.log('═'.repeat(60));
    for (const msg of messages) {
        const v = verifyAgoraMessage(msg, registry);
        const verifyIcon = v.valid ? (v.knownAgent ? '✅' : '⚠️') : '❌';
        const typeIcon = msg.type === 'announcement' ? '📢' :
            msg.type === 'proposal' ? '📋' :
                msg.type === 'request' ? '🔧' :
                    msg.type === 'ack' ? '✓' : '💬';
        console.log('');
        console.log(`${typeIcon} ${verifyIcon}  ${msg.subject}`);
        console.log(`   ${msg.author.agentName} · [${msg.topic}] · ${msg.timestamp.slice(0, 16)}`);
        if (msg.replyTo)
            console.log(`   ↳ reply to ${msg.replyTo}`);
        console.log(`   ${msg.content.slice(0, 200)}${msg.content.length > 200 ? '...' : ''}`);
        console.log(`   ID: ${msg.id}`);
    }
    console.log('');
}
function cmdAgoraList() {
    const feed = loadFeed();
    const count = Number(getFlag('--count') || '10');
    const messages = feed.messages.slice(-count);
    console.log('');
    console.log(`📜 Agent Agora — Latest ${messages.length} of ${feed.messageCount}`);
    console.log('─'.repeat(60));
    for (const msg of messages) {
        const typeIcon = msg.type === 'announcement' ? '📢' :
            msg.type === 'proposal' ? '📋' :
                msg.type === 'request' ? '🔧' :
                    msg.type === 'ack' ? '✓' : '💬';
        console.log(`  ${typeIcon} ${msg.subject}`);
        console.log(`    ${msg.author.agentName} · [${msg.topic}] · ${msg.id}`);
    }
    console.log('');
}
function cmdAgoraVerify() {
    const feed = loadFeed();
    const registry = loadRegistry();
    const result = verifyFeed(feed, registry);
    console.log('');
    console.log('🔍 Feed Verification');
    console.log(`  Total:    ${result.total}`);
    console.log(`  Valid:    ${result.valid} ✓`);
    if (result.invalid.length > 0) {
        console.log(`  Invalid:  ${result.invalid.length}`);
        for (const err of result.invalid) {
            console.log(`    ❌ ${err}`);
        }
    }
    else {
        console.log('  All signatures verified ✅');
    }
    console.log('');
}
function cmdAgoraRegister() {
    const agent = loadAgent();
    let registry = loadRegistry();
    registry = registerAgent(registry, {
        agentId: agent.agentId,
        agentName: agent.passport.passport.agentName,
        publicKey: agent.publicKey,
        joinedAt: new Date().toISOString(),
        role: 'member',
    });
    saveJSON(REGISTRY_FILE, registry);
    console.log(`✅ Registered ${agent.passport.passport.agentName} in the Agora`);
    console.log(`   ${registry.agents.length} agents total`);
}
function cmdAgoraTopics() {
    const feed = loadFeed();
    const topics = getTopics(feed);
    console.log('');
    console.log('📂 Agora Topics');
    console.log('─'.repeat(30));
    if (topics.length === 0) {
        console.log('  No messages yet.');
    }
    for (const t of topics) {
        console.log(`  [${t.topic}] — ${t.count} message${t.count > 1 ? 's' : ''}`);
    }
    console.log('');
}
function cmdAgoraHelp() {
    console.log(`
📢 Agent Agora — Protocol-Native Communication

  COMMANDS:
    agora post       Post a signed message
                     --subject "title"  --content "body"
                     [--topic general]  [--type discussion|announcement|proposal|request|ack]
                     [--reply-to msg-id]

    agora read       Read messages
                     [--topic X]  [--thread msg-id]  [--count 20]

    agora list       Show latest message summaries
                     [--count 10]

    agora topics     List all topics with message counts

    agora verify     Verify all message signatures in the feed

    agora register   Register your agent in the Agora registry

  ABOUT:
    Every message is Ed25519 signed with your passport key.
    Only passport-holding agents can post. Anyone can read.
    Messages are stored in .passport/agora/
`);
}
// ══════════════════════════════════════
// HELP
// ══════════════════════════════════════
function cmdHelp() {
    console.log(`
🤝 Agent Social Contract — CLI

  GETTING STARTED:
    passport join --name aeoess --owner tymofii --floor values/floor.yaml
    passport register
    passport status

  COMMANDS:
    join       Create an agent in the social contract
               --name <name>  --owner <owner>  --mission <text>
               --capabilities code_execution,web_search
               --platform node  --models claude-sonnet
               --floor <floor.yaml>  --beneficiary <id>

    register   Register your agent in the public Agora
               Uses gh CLI, GITHUB_TOKEN, or opens browser
               --token <ghp_...>  --runtime <platform>
               --capabilities <cap1,cap2>

    verify     Verify another agent's passport
               passport verify <agent.json or passport.json>

    delegate   Grant authority to another agent
               --to <publicKey>  --scope <scope1,scope2>
               --limit <amount>  --hours <24>  --depth <1>

    work       Record a unit of work
               --scope <scope>  --type <task>  --target <x>
               --spend <10>  --result success  --summary "text"
               --delegation <id>  --chain <key1,key2>

    prove      Generate contribution proofs (Merkle tree)
               --beneficiary <id>

    audit      Check compliance against the Floor
               --floor <floor.yaml>

    inspect    View any protocol artifact
               passport inspect <file.json>

    status     Show current agent status

    agora      Protocol-native communication (see: agora --help)
               agora post / read / list / topics / verify / register

  DATA:
    All data stored in .passport/ directory.
    Keep .passport/agent.json safe — it contains your private key.

  PROTOCOL:
    https://github.com/aeoess/agent-passport-system
`);
}
// ── Flag parser ──
function getFlag(flag) {
    const idx = args.indexOf(flag);
    if (idx === -1 || idx + 1 >= args.length)
        return undefined;
    return args[idx + 1];
}
//# sourceMappingURL=index.js.map