import { randomUUID } from "node:crypto";
import { openDatabase } from "../database.js";
/**
 * Creates a new project with a generated UUID.
 */
export function createProject(name, path) {
    const db = openDatabase();
    const projectId = randomUUID();
    db.prepare("INSERT INTO projects (project_id, name, path) VALUES (?, ?, ?)").run(projectId, name, path);
    return getProject(projectId);
}
/**
 * Returns a project by its ID, or null if not found.
 */
export function getProject(projectId) {
    const db = openDatabase();
    const row = db.prepare("SELECT * FROM projects WHERE project_id = ?").get(projectId);
    return row ?? null;
}
/**
 * Returns a project by its filesystem path, or null if not found.
 */
export function getProjectByPath(path) {
    const db = openDatabase();
    const row = db.prepare("SELECT * FROM projects WHERE path = ?").get(path);
    return row ?? null;
}
/**
 * Returns all registered projects.
 */
export function listProjects() {
    const db = openDatabase();
    return db.prepare("SELECT * FROM projects ORDER BY name").all();
}
/**
 * Updates the last_opened_at timestamp to the current time.
 */
export function updateProjectLastOpened(projectId) {
    const db = openDatabase();
    db.prepare("UPDATE projects SET last_opened_at = CURRENT_TIMESTAMP WHERE project_id = ?").run(projectId);
}
/**
 * Deletes a project. Related rows in claude_project_dirs, fs_state,
 * and command_runs are removed automatically via ON DELETE CASCADE.
 */
export function deleteProject(projectId) {
    const db = openDatabase();
    db.prepare("DELETE FROM projects WHERE project_id = ?").run(projectId);
}
//# sourceMappingURL=projects.js.map