/**
 * Startup logger for markus start command.
 * Writes all startup logs to ~/.markus/logs/ AND prints to console.
 * Log files are stored in ~/.markus/logs/.
 */
import { createWriteStream, existsSync, mkdirSync, appendFileSync } from 'node:fs';
import { join } from 'node:path';
import { homedir } from 'node:os';
const LOG_DIR = join(homedir(), '.markus', 'logs');
function ensureLogDir() {
    if (!existsSync(LOG_DIR)) {
        mkdirSync(LOG_DIR, { recursive: true, mode: 0o755 });
    }
}
export function getStartupLogPath() {
    const date = new Date().toISOString().slice(0, 10);
    return join(LOG_DIR, `startup-${date}.log`);
}
export function getLLMLogPath() {
    return join(LOG_DIR, 'llm.log');
}
let startupLogStream = null;
let startupLogPath = '';
let _suppressConsole = false;
export function setSuppressConsole(suppress) {
    _suppressConsole = suppress;
}
export function initStartupLogger() {
    ensureLogDir();
    startupLogPath = getStartupLogPath();
    startupLogStream = createWriteStream(startupLogPath, { flags: 'a', mode: 0o644 });
    return startupLogPath;
}
export function getStartupLogFile() {
    return startupLogPath;
}
const LEVEL_PREFIX = {
    OK: '[OK]',
    INFO: '[INFO]',
    WARN: '[WARN]',
    ERROR: '[ERROR]',
    FAIL: '[FAIL]',
    SKIP: '[SKIP]',
};
/**
 * Write a startup log entry to file AND console.
 */
export function startupLog(level, message, detail) {
    const ts = new Date().toISOString();
    const fileLine = `${ts} ${LEVEL_PREFIX[level]} ${message}${detail ? ' ' + detail : ''}\n`;
    if (startupLogStream) {
        startupLogStream.write(fileLine);
    }
    if (!_suppressConsole) {
        const consoleLine = `  ${LEVEL_PREFIX[level]} ${message}${detail ? ' ' + detail : ''}`;
        console.log(consoleLine);
    }
}
/**
 * Write a blank line to log file AND console.
 */
export function startupBlank() {
    if (startupLogStream) {
        startupLogStream.write('\n');
    }
    if (!_suppressConsole) {
        console.log('');
    }
}
/**
 * Write a section header to log file AND console.
 */
export function startupSection(title) {
    if (startupLogStream) {
        startupLogStream.write(`\n--- ${title} ---\n`);
    }
    if (!_suppressConsole) {
        console.log(`\n--- ${title} ---`);
    }
}
/**
 * Close the startup log stream (call on shutdown).
 */
export function closeStartupLogger() {
    if (startupLogStream) {
        startupLogStream.end();
        startupLogStream = null;
    }
}
/**
 * Append an LLM log entry (call for each LLM call).
 */
export function appendLLMLog(entry) {
    ensureLogDir();
    const line = JSON.stringify(entry) + '\n';
    try {
        appendFileSync(getLLMLogPath(), line, { mode: 0o644 });
    }
    catch {
        // non-fatal
    }
}
//# sourceMappingURL=logger.js.map