/**
 * state-locator — single source of truth for every Nexus-owned filesystem path.
 *
 * Uninstall, cleanup, doctor, and startup hygiene all read from here so that no
 * heuristic ever disagrees with another about what "Nexus state" means.
 *
 * Paths fall into six scopes:
 *   state    — durable per-workspace state dir
 *   cache    — reusable caches (safe to drop)
 *   runtime  — live per-session working dirs (runs, sessions)
 *   log      — log files
 *   worktree — git worktrees used by phantom execution
 *   db       — sqlite databases (memory, synapse, architects)
 */
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import { resolveNexusStateDir } from '../engines/runtime-registry.js';
/** Well-known tmp prefixes owned by Nexus. */
export const NEXUS_TMP_PREFIXES = [
    'nexus-prime-runs',
    'nexus-prime-worktrees',
    'nexus-prime-runtime-skills',
    'nexus-prime-runtime-workflows',
    'nexus-prime-runtime-hooks',
    'nexus-prime-runtime-automations',
    'nexus-prime-sessions',
    'nexus-phantom',
];
/** Matches per-invocation tmp entries like `nexus-prime-feature-registry-*`. */
export const NEXUS_TMP_GLOB_PREFIXES = [
    'nexus-prime-feature-registry-',
    'nexus-reset-test-',
];
export function getNexusStateDir() {
    return resolveNexusStateDir();
}
export function getSetupMarkerPath() {
    return path.join(getNexusStateDir(), 'setup-marker-v6.json');
}
export function getInstallManifestPath() {
    return path.join(getNexusStateDir(), 'install-manifest.json');
}
/** Top-level tmp roots that Nexus writes into. */
export function getWorktreeRoots() {
    const tmp = os.tmpdir();
    return [
        path.join(tmp, 'nexus-prime-worktrees'),
        path.join(tmp, 'nexus-phantom'),
    ];
}
/** Top-level tmp roots for runs + session scratch. */
export function getRuntimeTmpRoots() {
    const tmp = os.tmpdir();
    return [
        path.join(tmp, 'nexus-prime-runs'),
        path.join(tmp, 'nexus-prime-sessions'),
        path.join(tmp, 'nexus-prime-runtime-skills'),
        path.join(tmp, 'nexus-prime-runtime-workflows'),
        path.join(tmp, 'nexus-prime-runtime-hooks'),
        path.join(tmp, 'nexus-prime-runtime-automations'),
    ];
}
/**
 * Resolve every persistent path Nexus owns under the state dir.
 *
 * This is the authoritative list used by uninstall --purge-all. It mirrors the
 * directory names that engines create lazily, so missing entries are normal.
 */
export function enumerateStatePaths(stateDir = getNexusStateDir()) {
    return [
        { path: path.join(stateDir, 'memory.db'), scope: 'db', optional: true },
        { path: path.join(stateDir, 'memory.db-wal'), scope: 'db', optional: true },
        { path: path.join(stateDir, 'memory.db-shm'), scope: 'db', optional: true },
        { path: path.join(stateDir, 'ngram-index.db'), scope: 'db', optional: true },
        { path: path.join(stateDir, 'ngram-index.db-wal'), scope: 'db', optional: true },
        { path: path.join(stateDir, 'ngram-index.db-shm'), scope: 'db', optional: true },
        { path: path.join(stateDir, '.synapse'), scope: 'db', optional: true },
        { path: path.join(stateDir, '.architects'), scope: 'db', optional: true },
        { path: path.join(stateDir, 'runs'), scope: 'runtime', optional: true },
        { path: path.join(stateDir, 'sessions'), scope: 'runtime', optional: true },
        { path: path.join(stateDir, 'runtime-registry'), scope: 'runtime', optional: true },
        { path: path.join(stateDir, 'events.jsonl'), scope: 'runtime', optional: true },
        { path: path.join(stateDir, 'dashboard-state.json'), scope: 'runtime', optional: true },
        { path: path.join(stateDir, 'cache'), scope: 'cache', optional: true },
        { path: path.join(stateDir, 'logs'), scope: 'log', optional: true },
        { path: path.join(stateDir, 'workspaces'), scope: 'state', optional: true },
        { path: path.join(stateDir, 'bootstrap-receipts'), scope: 'state', optional: true },
        { path: path.join(stateDir, 'setup-marker-v6.json'), scope: 'state', optional: true },
        { path: path.join(stateDir, 'install-manifest.json'), scope: 'state', optional: true },
        { path: path.join(stateDir, 'license.key'), scope: 'state', optional: true },
        { path: path.join(stateDir, 'trial.json'), scope: 'state', optional: true },
        { path: path.join(stateDir, 'nexus-daemon.lock.json'), scope: 'runtime', optional: true },
    ];
}
export function enumerateNgramArchives(stateDir = getNexusStateDir()) {
    const out = [];
    try {
        if (!fs.existsSync(stateDir))
            return out;
        for (const entry of fs.readdirSync(stateDir)) {
            if (!entry.startsWith('ngram-index.db.oversize.'))
                continue;
            const full = path.join(stateDir, entry);
            try {
                const st = fs.statSync(full);
                if (st.isFile())
                    out.push({ path: full, bytes: st.size, modifiedAt: st.mtimeMs });
            }
            catch {
                // unreadable — skip
            }
        }
    }
    catch {
        // unreadable state dir — return what we have
    }
    return out;
}
export function enumerateRegistrationTargets(workspaceRoot, home = os.homedir()) {
    const claudeDesktopConfig = process.platform === 'darwin'
        ? path.join(home, 'Library', 'Application Support', 'Claude', 'claude_desktop_config.json')
        : process.platform === 'win32'
            ? path.join(process.env.APPDATA || path.join(home, 'AppData', 'Roaming'), 'Claude', 'claude_desktop_config.json')
            : path.join(home, '.claude', 'claude_desktop_config.json');
    const codexRoot = process.env.CODEX_HOME?.trim() || path.join(home, '.codex');
    return [
        { id: 'claude-workspace', filePath: path.join(workspaceRoot, '.claude', 'settings.json'), jsonPath: ['hooks'] },
        { id: 'claude-workspace', filePath: path.join(workspaceRoot, '.mcp.json'), jsonPath: ['mcpServers', 'nexus-prime'] },
        { id: 'claude-home', filePath: path.join(home, '.claude', 'settings.json'), jsonPath: ['hooks'] },
        { id: 'claude-desktop', filePath: claudeDesktopConfig, jsonPath: ['mcpServers', 'nexus-prime'] },
        { id: 'vscode-workspace', filePath: path.join(workspaceRoot, '.vscode', 'settings.json'), jsonPath: ['mcpServers', 'nexus-prime'] },
        { id: 'cursor-workspace', filePath: path.join(workspaceRoot, '.cursor', 'mcp.json'), jsonPath: ['mcpServers', 'nexus-prime'] },
        { id: 'cursor-home', filePath: path.join(home, '.cursor', 'mcp.json'), jsonPath: ['mcpServers', 'nexus-prime'] },
        { id: 'opencode', filePath: path.join(home, '.config', 'opencode', 'opencode.json'), jsonPath: ['mcp', 'nexus-prime'] },
        { id: 'windsurf', filePath: path.join(home, '.windsurf', 'mcp.json'), jsonPath: ['mcpServers', 'nexus-prime'] },
        { id: 'antigravity', filePath: path.join(home, '.antigravity', 'mcp.json'), jsonPath: ['mcpServers', 'nexus-prime'] },
        { id: 'openclaw', filePath: path.join(home, '.openclaw', 'openclaw.json'), jsonPath: ['mcpServers', 'nexus-prime'] },
        { id: 'hermes', filePath: path.join(home, '.hermes', 'mcp.json'), jsonPath: ['mcpServers', 'nexus-prime'] },
        { id: 'nanoclaw', filePath: path.join(home, '.nanoclaw', 'mcp.json'), jsonPath: ['mcpServers', 'nexus-prime'] },
        { id: 'picoclaw', filePath: path.join(home, '.picoclaw', 'mcp.json'), jsonPath: ['mcpServers', 'nexus-prime'] },
        { id: 'aider', filePath: path.join(home, '.aider', 'mcp.json'), jsonPath: ['mcpServers', 'nexus-prime'] },
        { id: 'continue', filePath: path.join(home, '.continue', 'config.json'), jsonPath: ['mcpServers', 'nexus-prime'] },
        { id: 'cline', filePath: path.join(home, '.vscode', 'cline-mcp.json'), jsonPath: ['mcpServers', 'nexus-prime'] },
        { id: 'codex-json', filePath: path.join(codexRoot, 'config.json'), jsonPath: ['mcpServers', 'nexus-prime'] },
        { id: 'codex-toml', filePath: path.join(codexRoot, 'config.toml'), jsonPath: ['mcp_servers', 'nexus-prime'] },
    ];
}
