"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.run = run;
const promises_1 = __importDefault(require("node:fs/promises"));
const node_path_1 = __importDefault(require("node:path"));
const node_os_1 = __importDefault(require("node:os"));
const node_fs_1 = __importDefault(require("node:fs"));
function getClaudeDesktopConfigPath() {
    const platform = process.platform;
    if (platform === "darwin") {
        return node_path_1.default.join(node_os_1.default.homedir(), "Library", "Application Support", "Claude", "claude_desktop_config.json");
    }
    else if (platform === "win32") {
        return node_path_1.default.join(node_os_1.default.homedir(), "AppData", "Roaming", "Claude", "claude_desktop_config.json");
    }
    return node_path_1.default.join(node_os_1.default.homedir(), ".config", "Claude", "claude_desktop_config.json");
}
async function readLastIngestDate(docuDir) {
    try {
        const log = await promises_1.default.readFile(node_path_1.default.join(docuDir, "log.md"), "utf8");
        const match = log.match(/\[(\d{4}-\d{2}-\d{2}[^\]]*)\]\s+ingest/);
        return match ? match[1] : "never";
    }
    catch {
        return "never";
    }
}
async function countWikiPages(docuDir) {
    const counts = { entities: 0, concepts: 0, timelines: 0, syntheses: 0 };
    for (const cat of Object.keys(counts)) {
        try {
            const files = await promises_1.default.readdir(node_path_1.default.join(docuDir, "wiki", cat));
            counts[cat] = files.filter((f) => f.endsWith(".md")).length;
        }
        catch {
            // Directory may not exist
        }
    }
    return counts;
}
async function run() {
    const configPath = getClaudeDesktopConfigPath();
    const projectDir = process.cwd();
    const docuDir = node_path_1.default.join(projectDir, ".docuflow");
    // Check MCP registration
    let registered = false;
    try {
        const raw = await promises_1.default.readFile(configPath, "utf8");
        const config = JSON.parse(raw);
        registered = Boolean(config?.mcpServers?.docuflow);
    }
    catch {
        // Config doesn't exist
    }
    // Count spec files
    let specCount = 0;
    try {
        const entries = await promises_1.default.readdir(node_path_1.default.join(docuDir, "specs"));
        specCount = entries.filter((e) => e.endsWith(".md")).length;
    }
    catch {
        // .docuflow/specs doesn't exist
    }
    // Count sources
    let sourceCount = 0;
    try {
        const entries = await promises_1.default.readdir(node_path_1.default.join(docuDir, "sources"));
        sourceCount = entries.filter((e) => e.endsWith(".md") || e.endsWith(".txt")).length;
    }
    catch {
        // .docuflow/sources doesn't exist
    }
    // Check for CLAUDE.md
    const claudeMdExists = node_fs_1.default.existsSync(node_path_1.default.join(projectDir, "CLAUDE.md"));
    // Count wiki pages per category
    const wikiCounts = await countWikiPages(docuDir);
    const totalWikiPages = Object.values(wikiCounts).reduce((a, b) => a + b, 0);
    // Last ingest date
    const lastIngest = await readLastIngestDate(docuDir);
    // Get CLI version from package.json
    let version = "unknown";
    try {
        const pkgPath = require.resolve("@doquflow/cli/package.json");
        const pkg = JSON.parse(await promises_1.default.readFile(pkgPath, "utf8"));
        version = pkg.version;
    }
    catch {
        try {
            const pkgPath = node_path_1.default.resolve(__dirname, "..", "..", "package.json");
            const pkg = JSON.parse(await promises_1.default.readFile(pkgPath, "utf8"));
            version = pkg.version;
        }
        catch {
            // Can't resolve version
        }
    }
    console.log("\nDocuflow status");
    console.log("───────────────────────────────────────────");
    console.log(`  Version:          ${version}`);
    console.log(`  MCP registered:   ${registered ? "✓ yes" : "✗ no"}`);
    console.log(`  CLAUDE.md:        ${claudeMdExists ? "✓ present" : "✗ missing"}`);
    console.log(`  Config file:      ${configPath}`);
    console.log("");
    console.log("  Specs written:    " + specCount);
    console.log("  Sources:          " + sourceCount);
    console.log("  Last ingest:      " + lastIngest);
    console.log("");
    console.log("  Wiki pages:       " + totalWikiPages + " total");
    if (totalWikiPages > 0) {
        console.log(`    entities:       ${wikiCounts.entities}`);
        console.log(`    concepts:       ${wikiCounts.concepts}`);
        console.log(`    syntheses:      ${wikiCounts.syntheses}`);
        console.log(`    timelines:      ${wikiCounts.timelines}`);
    }
    console.log("───────────────────────────────────────────");
    if (!registered) {
        console.log('\n  ⚠  Run "docuflow init" to register MCP server.');
    }
    if (!claudeMdExists) {
        console.log('\n  ⚠  Run "docuflow init" to generate CLAUDE.md.');
    }
    if (totalWikiPages === 0 && sourceCount === 0) {
        console.log('\n  💡 Run "docuflow suggest" to see what to document first.');
    }
    console.log();
}
