"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.doBranches = doBranches;
exports.doSwitchBack = doSwitchBack;
exports.doLessons = doLessons;
exports.doEvolution = doEvolution;
exports.doFocus = doFocus;
exports.doWhy = doWhy;
const utils_1 = require("../utils");
const project = (0, utils_1.getProjectName)();
function doBranches() {
    (0, utils_1.printHeader)('Cell Branches');
    try {
        const { listBranchSnapshots } = require('../../../03-knowledge/decisions/branch-snapshot');
        const snapshots = listBranchSnapshots(project);
        if (snapshots.length === 0) {
            (0, utils_1.printDim)('No branch snapshots found. Run: cell scan');
            return;
        }
        for (const s of snapshots) {
            const lastActive = s.lastActiveAt ? new Date(s.lastActiveAt) : new Date();
            const daysSince = Math.floor((Date.now() - lastActive.getTime()) / 86400000);
            const status = daysSince <= 1 ? '🟢' : daysSince <= 7 ? '🟡' : '🔴';
            console.log(`  ${status} ${s.branch} (${daysSince}d ago)`);
            if (s.goal)
                (0, utils_1.printDim)(`    Goal: ${s.goal}`);
            if (s.filesTouched && s.filesTouched.length > 0) {
                (0, utils_1.printDim)(`    Files: ${s.filesTouched.slice(0, 3).join(', ')}${s.filesTouched.length > 3 ? '...' : ''}`);
            }
        }
        console.log();
    }
    catch (e) {
        (0, utils_1.printWarning)('Failed to list branches: ' + String(e));
    }
}
function doSwitchBack(args) {
    const branch = args[1] || '';
    if (!branch) {
        (0, utils_1.printWarning)('Usage: cell switch-back <branch>');
        return;
    }
    (0, utils_1.printHeader)(`Switch Back: ${branch}`);
    try {
        const { getBranchContextSummary, formatBranchContextSummary } = require('../../../03-knowledge/decisions/branch-snapshot');
        const summary = getBranchContextSummary(project, branch);
        if (!summary) {
            (0, utils_1.printDim)(`No context found for branch: ${branch}`);
            (0, utils_1.printDim)('Run: cell scan to capture current branch context');
            return;
        }
        console.log(formatBranchContextSummary(summary));
        console.log();
        (0, utils_1.printDim)(`To restore: git checkout ${branch}`);
        (0, utils_1.printDim)('Then: cell context (to load branch-specific context)');
    }
    catch (e) {
        (0, utils_1.printWarning)('Failed to get branch context: ' + String(e));
    }
}
function doLessons(args) {
    (0, utils_1.printHeader)('Cell Engineering Lessons');
    try {
        const { getEngineeringLessons } = require('../../../03-knowledge/decisions/failure-memory');
        const lessons = getEngineeringLessons(project, 20);
        if (lessons.length === 0) {
            (0, utils_1.printDim)('No lessons recorded yet. Use Cell more to build engineering memory.');
            return;
        }
        for (const l of lessons.slice(0, 10)) {
            const severity = l.failureRate >= 0.8 ? '🔴' : l.failureRate >= 0.6 ? '🟠' : '🟡';
            console.log(`  ${severity} ${l.description}`);
            (0, utils_1.printDim)(`    Category: ${l.category} | Language: ${l.language} | Failure Rate: ${Math.round(l.failureRate * 100)}%`);
            if (l.alternative)
                (0, utils_1.printDim)(`    Alternative: ${l.alternative}`);
        }
        console.log();
    }
    catch (e) {
        (0, utils_1.printWarning)('Failed to load lessons: ' + String(e));
    }
}
function doEvolution(args) {
    (0, utils_1.printHeader)('Cell Evolution');
    try {
        const { listBranchSnapshots } = require('../../../03-knowledge/decisions/branch-snapshot');
        const snapshots = listBranchSnapshots(project);
        console.log(utils_1.C.bold(`  Project: ${project}`));
        console.log(`  Branches tracked: ${snapshots.length}`);
        if (snapshots.length > 0) {
            const active = snapshots.filter((s) => {
                const lastActive = s.lastActiveAt ? new Date(s.lastActiveAt) : new Date();
                return (Date.now() - lastActive.getTime()) < 7 * 86400000;
            });
            console.log(`  Active (last 7d): ${active.length}`);
        }
        console.log();
        (0, utils_1.printDim)('Use: cell branches to see detailed branch history');
    }
    catch (e) {
        (0, utils_1.printWarning)('Failed to show evolution: ' + String(e));
    }
}
function doFocus(args) {
    (0, utils_1.printHeader)('Cell Focus');
    try {
        const { listBranchSnapshots } = require('../../../03-knowledge/decisions/branch-snapshot');
        const snapshots = listBranchSnapshots(project);
        const active = snapshots.filter((s) => {
            const lastActive = s.lastActiveAt ? new Date(s.lastActiveAt) : new Date();
            return (Date.now() - lastActive.getTime()) < 1 * 86400000;
        });
        const stale = snapshots.filter((s) => {
            const lastActive = s.lastActiveAt ? new Date(s.lastActiveAt) : new Date();
            const days = (Date.now() - lastActive.getTime()) / 86400000;
            return days > 1 && days <= 7;
        });
        const abandoned = snapshots.filter((s) => {
            const lastActive = s.lastActiveAt ? new Date(s.lastActiveAt) : new Date();
            return (Date.now() - lastActive.getTime()) > 7 * 86400000;
        });
        console.log(utils_1.C.bold('  Focus Report:'));
        console.log();
        console.log(`  🟢 Active: ${active.length} branches`);
        for (const s of active) {
            console.log(`    - ${s.branch}`);
        }
        console.log();
        console.log(`  🟡 Stale: ${stale.length} branches`);
        for (const s of stale) {
            console.log(`    - ${s.branch}`);
        }
        console.log();
        console.log(`  🔴 Abandoned: ${abandoned.length} branches`);
        for (const s of abandoned) {
            console.log(`    - ${s.branch}`);
        }
        console.log();
    }
    catch (e) {
        (0, utils_1.printWarning)('Failed to show focus: ' + String(e));
    }
}
function doWhy(args) {
    const topic = args.slice(1).join(' ');
    if (!topic) {
        (0, utils_1.printWarning)('Usage: cell why <topic>');
        return;
    }
    (0, utils_1.printHeader)(`Cell Why: ${topic}`);
    try {
        const { getDb } = require('../../../06-memory/database/database');
        const db = getDb();
        const rows = db.prepare(`SELECT type, summary, topic, importance, created_at FROM memory_events WHERE project = ? AND (LOWER(summary) LIKE ? OR LOWER(topic) LIKE ?) ORDER BY importance DESC, created_at DESC LIMIT 20`).all(project, `%${topic.toLowerCase()}%`, `%${topic.toLowerCase()}%`);
        if (rows.length === 0) {
            (0, utils_1.printDim)(`No decisions found for "${topic}"`);
            return;
        }
        for (const r of rows) {
            console.log(`  [${r.type}] ${r.summary}`);
            (0, utils_1.printDim)(`    ${r.created_at} | Importance: ${r.importance}`);
        }
        console.log();
    }
    catch (e) {
        (0, utils_1.printWarning)('Failed to search decisions: ' + String(e));
    }
}
//# sourceMappingURL=branch.js.map