/**
 * Repository for terminal session tracking.
 */
import { randomUUID } from "node:crypto";
import { openDatabase } from "../database.js";
/**
 * Creates a new terminal session with a generated UUID.
 */
export function createTerminalSession(projectId, pid) {
    const db = openDatabase();
    const terminalId = randomUUID();
    db.prepare("INSERT INTO terminal_sessions (terminal_id, project_id, pid) VALUES (?, ?, ?)").run(terminalId, projectId, pid ?? null);
    return getTerminalSession(terminalId);
}
/**
 * Returns a terminal session by ID, or null if not found.
 */
export function getTerminalSession(terminalId) {
    const db = openDatabase();
    const row = db
        .prepare("SELECT * FROM terminal_sessions WHERE terminal_id = ?")
        .get(terminalId);
    return row ?? null;
}
/**
 * Lists active (unclosed) terminal sessions.
 * Optionally filters by project ID.
 */
export function listActiveSessions(projectId) {
    const db = openDatabase();
    if (projectId) {
        return db
            .prepare("SELECT * FROM terminal_sessions WHERE closed_at IS NULL AND project_id = ? ORDER BY created_at DESC")
            .all(projectId);
    }
    return db
        .prepare("SELECT * FROM terminal_sessions WHERE closed_at IS NULL ORDER BY created_at DESC")
        .all();
}
/**
 * Marks a terminal session as closed by setting closed_at to the current timestamp.
 */
export function closeTerminalSession(terminalId) {
    const db = openDatabase();
    db.prepare("UPDATE terminal_sessions SET closed_at = CURRENT_TIMESTAMP WHERE terminal_id = ?").run(terminalId);
}
//# sourceMappingURL=terminal-sessions.js.map