/**
 * Bootstrap brain state loading for agent startup.
 * Calls other core modules directly (not engine functions).
 * @task T4784
 */
import { existsSync, readFileSync } from 'node:fs';
import { join } from 'node:path';
import { getAccessor } from '../store/data-accessor.js';
/** Build brain state for agent bootstrapping. */
export async function buildBrainState(projectRoot, opts, accessor) {
    const speed = opts?.speed || 'fast';
    const brain = {
        _meta: {
            speed,
            generatedAt: new Date().toISOString(),
            version: '1.0.0',
        },
    };
    // --- Session (from SQLite, ADR-006/ADR-020) ---
    const acc = accessor ?? (await getAccessor(projectRoot));
    try {
        const sessions = await acc.loadSessions();
        const activeSession = sessions.find((s) => s.status === 'active');
        if (activeSession) {
            brain.session = {
                id: activeSession.id,
                name: activeSession.name || activeSession.id,
                status: activeSession.status,
                startedAt: activeSession.startedAt,
            };
        }
    }
    catch {
        // skip
    }
    // --- Tasks & Progress ---
    const { tasks } = await acc.queryTasks({});
    brain.progress = {
        total: tasks.length,
        done: tasks.filter((t) => t.status === 'done').length,
        active: tasks.filter((t) => t.status === 'active').length,
        blocked: tasks.filter((t) => t.status === 'blocked').length,
        pending: tasks.filter((t) => t.status === 'pending').length,
    };
    // --- Current Task (from focus or session) ---
    const focus = await acc.getMetaValue('focus_state');
    const focusTaskId = focus?.currentTask ?? null;
    if (focusTaskId) {
        const task = tasks.find((t) => t.id === focusTaskId);
        if (task) {
            brain.currentTask = { id: task.id, title: task.title, status: task.status };
        }
    }
    // --- Next Suggestion (simple: pick first pending task with all deps met) ---
    const completedIds = new Set(tasks.filter((t) => t.status === 'done').map((t) => t.id));
    const readyTasks = tasks.filter((t) => {
        if (t.status !== 'pending')
            return false;
        const deps = t.depends ?? [];
        return deps.every((d) => completedIds.has(d));
    });
    if (readyTasks.length > 0) {
        // Sort by priority
        const priorityOrder = { critical: 0, high: 1, medium: 2, low: 3 };
        readyTasks.sort((a, b) => {
            const aPri = priorityOrder[a.priority ?? 'medium'] ?? 2;
            const bPri = priorityOrder[b.priority ?? 'medium'] ?? 2;
            return aPri - bPri;
        });
        const next = readyTasks[0];
        brain.nextSuggestion = { id: next.id, title: next.title, score: 1 };
    }
    // --- Full tier: decisions, blockers, contextDrift ---
    if (speed === 'full' || speed === 'complete') {
        // Blockers
        const blockedTasks = tasks.filter((t) => t.status === 'blocked' || t.depends?.some((d) => !completedIds.has(d)));
        brain.blockers = blockedTasks.slice(0, 10).map((b) => ({
            taskId: b.id,
            title: b.title,
            blockedBy: b.depends || [],
        }));
        // Recent decisions (from decision-log in .cleo)
        try {
            const decisionLogPath = join(projectRoot, '.cleo', 'decision-log.jsonl');
            if (existsSync(decisionLogPath)) {
                const content = readFileSync(decisionLogPath, 'utf-8').trim();
                if (content) {
                    const entries = content
                        .split('\n')
                        .filter((l) => l.trim())
                        .map((l) => {
                        try {
                            return JSON.parse(l);
                        }
                        catch {
                            return null;
                        }
                    })
                        .filter(Boolean);
                    const recent = entries.slice(-5);
                    brain.recentDecisions = recent.map((d) => ({
                        id: d.id ?? '',
                        decision: d.decision ?? '',
                        timestamp: d.timestamp ?? '',
                    }));
                }
            }
        }
        catch {
            // skip
        }
        // Context drift (from context state file)
        try {
            const contextStatePath = join(projectRoot, '.cleo', '.context-state.json');
            if (existsSync(contextStatePath)) {
                const state = JSON.parse(readFileSync(contextStatePath, 'utf-8'));
                const percentage = state.contextWindow?.percentage ?? 0;
                const factors = [];
                if (percentage > 80)
                    factors.push('high_context_usage');
                if (percentage > 50)
                    factors.push('moderate_context_usage');
                brain.contextDrift = { score: percentage, factors };
            }
        }
        catch {
            // skip
        }
    }
    return brain;
}
//# sourceMappingURL=bootstrap.js.map