/**
 * SubscriptionPool — multi-account subscription registry (P1.1).
 *
 * Part of the Subscription & Auth Standard
 * (docs/specs/_drafts/subscription-auth-standard-master-spec.md).
 *
 * The source of truth for "which subscriptions does this operator have, per
 * provider." Each account is a first-class registry entry keyed to its login
 * LOCATION — its per-account config home (e.g. CLAUDE_CONFIG_DIR) — NOT its
 * tokens. This is the load-bearing invariant behind decision 1A
 * ("re-enroll per machine now, architect for cross-machine sync later"):
 * because the registry only ever stores the config-home path, a future
 * cross-machine sync (decision 1B) is a clean bolt-on that ships each account's
 * credential blob over the existing E2E secret-sync — the registry itself never
 * has to change shape, and a leaked registry file never leaks a credential.
 *
 * Why never tokens: Anthropic prohibits Claude OAuth tokens in non-Claude-Code
 * tools and enforces it. The pool drives each account through its real
 * framework client pointed at that account's config home; instar never extracts
 * a token. Storing only the location keeps that invariant structural.
 *
 * File-backed JSON at `<stateDir>/subscription-pool.json`, atomic tmp+rename
 * writes, optimistic CAS via a per-record `version` field. Mirrors the
 * CommitmentTracker durable-registry pattern (a simpler single-writer form).
 *
 * Ships DARK: nothing instantiates a pool with accounts unless the operator
 * enrolls one. A pool of zero accounts is a no-op — single-account agents are
 * entirely unaffected.
 */
import fs from 'node:fs';
import path from 'node:path';
const ID_RE = /^[a-z0-9-]+$/;
const PROVIDERS = [
    'anthropic',
    'openai',
    'github-copilot',
    'google',
];
const FRAMEWORKS = [
    'claude-code',
    'codex-cli',
    'gemini-cli',
    'pi-cli',
];
const STATUSES = [
    'active',
    'warming',
    'rate-limited',
    'needs-reauth',
    'disabled',
];
/**
 * Field names that would smuggle a credential into the registry. Rejected at
 * add()/update() — the registry stores LOCATION, never secrets. This makes the
 * "never store tokens" invariant a structural guard, not a convention.
 */
const FORBIDDEN_CREDENTIAL_FIELDS = [
    'accesstoken',
    'refreshtoken',
    'token',
    'apikey',
    'api_key',
    'credential',
    'credentials',
    'secret',
    'password',
    'oauth',
];
export class ValidationError extends Error {
    constructor(message) {
        super(message);
        this.name = 'ValidationError';
    }
}
export class SubscriptionPool {
    storePath;
    store;
    constructor(config) {
        this.storePath = path.join(config.stateDir, 'subscription-pool.json');
        this.store = this.load();
    }
    // ── Reads ────────────────────────────────────────────────────────
    /** All accounts (a shallow copy — callers can't mutate the store). */
    list() {
        return this.store.accounts.map((a) => ({ ...a }));
    }
    /** One account by id, or null. */
    get(id) {
        const found = this.store.accounts.find((a) => a.id === id);
        return found ? { ...found } : null;
    }
    /** Count of accounts. A pool of 0 is the dark/no-op default. */
    size() {
        return this.store.accounts.length;
    }
    // ── Writes ───────────────────────────────────────────────────────
    /**
     * Add a new account. Throws ValidationError on bad input or duplicate id.
     * `rawExtra` (if provided) is scanned for credential-bearing field names and
     * rejected — the registry never stores tokens.
     */
    add(input, rawExtra) {
        this.assertNoCredentialFields(input);
        if (rawExtra)
            this.assertNoCredentialFields(rawExtra);
        const id = (input.id ?? '').trim();
        if (!id)
            throw new ValidationError('id is required');
        if (!ID_RE.test(id)) {
            throw new ValidationError('id must match ^[a-z0-9-]+$');
        }
        if (this.store.accounts.some((a) => a.id === id)) {
            throw new ValidationError(`account ${id} already exists`);
        }
        const nickname = (input.nickname ?? '').trim();
        if (!nickname)
            throw new ValidationError('nickname is required');
        if (!PROVIDERS.includes(input.provider)) {
            throw new ValidationError(`provider must be one of: ${PROVIDERS.join(', ')}`);
        }
        if (!FRAMEWORKS.includes(input.framework)) {
            throw new ValidationError(`framework must be one of: ${FRAMEWORKS.join(', ')}`);
        }
        const configHome = (input.configHome ?? '').trim();
        if (!configHome)
            throw new ValidationError('configHome is required');
        const status = input.status ?? 'active';
        if (!STATUSES.includes(status)) {
            throw new ValidationError(`status must be one of: ${STATUSES.join(', ')}`);
        }
        const account = {
            id,
            nickname,
            ...(input.email?.trim() ? { email: input.email.trim() } : {}),
            provider: input.provider,
            framework: input.framework,
            configHome,
            status,
            lastQuota: null,
            enrolledAt: new Date().toISOString(),
            version: 1,
        };
        this.store.accounts.push(account);
        this.save();
        return { ...account };
    }
    /**
     * Patch a mutable account. Returns the updated account, or null if not found.
     * id/provider/enrolledAt are immutable here; version auto-increments (CAS).
     * Throws ValidationError on bad field values or credential-bearing input.
     */
    update(id, patch, rawExtra) {
        this.assertNoCredentialFields(patch);
        if (rawExtra)
            this.assertNoCredentialFields(rawExtra);
        const acct = this.store.accounts.find((a) => a.id === id);
        if (!acct)
            return null;
        if (patch.nickname !== undefined) {
            const nn = patch.nickname.trim();
            if (!nn)
                throw new ValidationError('nickname cannot be empty');
            acct.nickname = nn;
        }
        if (patch.framework !== undefined) {
            if (!FRAMEWORKS.includes(patch.framework)) {
                throw new ValidationError(`framework must be one of: ${FRAMEWORKS.join(', ')}`);
            }
            acct.framework = patch.framework;
        }
        if (patch.configHome !== undefined) {
            const ch = patch.configHome.trim();
            if (!ch)
                throw new ValidationError('configHome cannot be empty');
            acct.configHome = ch;
        }
        if (patch.status !== undefined) {
            if (!STATUSES.includes(patch.status)) {
                throw new ValidationError(`status must be one of: ${STATUSES.join(', ')}`);
            }
            acct.status = patch.status;
        }
        if (patch.lastQuota !== undefined) {
            acct.lastQuota = patch.lastQuota;
        }
        if (patch.lastUsedAt !== undefined) {
            acct.lastUsedAt = patch.lastUsedAt;
        }
        if (patch.lastRefreshAt !== undefined) {
            acct.lastRefreshAt = patch.lastRefreshAt;
        }
        if (patch.email !== undefined) {
            const em = patch.email.trim();
            acct.email = em || undefined;
        }
        acct.version += 1;
        this.save();
        return { ...acct };
    }
    /** Remove an account. Returns true if one was removed. */
    remove(id) {
        const before = this.store.accounts.length;
        this.store.accounts = this.store.accounts.filter((a) => a.id !== id);
        const removed = this.store.accounts.length < before;
        if (removed)
            this.save();
        return removed;
    }
    // ── Health ───────────────────────────────────────────────────────
    getHealth() {
        const total = this.store.accounts.length;
        const usable = this.store.accounts.filter((a) => a.status === 'active' || a.status === 'warming').length;
        return {
            status: 'healthy',
            message: `${total} account(s), ${usable} usable`,
            lastCheck: new Date().toISOString(),
        };
    }
    // ── Persistence ──────────────────────────────────────────────────
    assertNoCredentialFields(obj) {
        if (!obj || typeof obj !== 'object')
            return;
        for (const key of Object.keys(obj)) {
            if (FORBIDDEN_CREDENTIAL_FIELDS.includes(key.toLowerCase())) {
                throw new ValidationError(`the registry stores login LOCATION, never credentials — field "${key}" is not allowed`);
            }
        }
    }
    load() {
        try {
            if (fs.existsSync(this.storePath)) {
                const data = JSON.parse(fs.readFileSync(this.storePath, 'utf-8'));
                if (data && data.version === 1 && Array.isArray(data.accounts)) {
                    // Backfill version field on any pre-CAS record (defensive).
                    for (const a of data.accounts) {
                        if (typeof a.version !== 'number')
                            a.version = 1;
                    }
                    return data;
                }
            }
        }
        catch {
            // @silent-fallback-ok — corrupt/unreadable store starts fresh; the
            // registry is metadata only, never credentials, so a fresh start loses
            // nothing irrecoverable (the operator re-enrolls / accounts re-detect).
        }
        return { version: 1, accounts: [], lastModified: new Date().toISOString() };
    }
    save() {
        this.store.lastModified = new Date().toISOString();
        try {
            const dir = path.dirname(this.storePath);
            fs.mkdirSync(dir, { recursive: true });
            const tmpPath = `${this.storePath}.${process.pid}.tmp`;
            fs.writeFileSync(tmpPath, JSON.stringify(this.store, null, 2) + '\n');
            fs.renameSync(tmpPath, this.storePath);
        }
        catch {
            // @silent-fallback-ok — state persistence failure; the in-memory store
            // remains authoritative for this process and the next write retries.
        }
    }
}
//# sourceMappingURL=SubscriptionPool.js.map