import {
  getDatabase
} from "./chunk-FBGWG4N6.js";

// src/server/db/projects.ts
function createProject(name, workdir) {
  const db = getDatabase();
  const now = (/* @__PURE__ */ new Date()).toISOString();
  const id = crypto.randomUUID();
  db.prepare(
    `
    INSERT INTO projects (id, name, workdir, created_at, updated_at)
    VALUES (?, ?, ?, ?, ?)
  `
  ).run(id, name, workdir, now, now);
  return {
    id,
    name,
    workdir,
    createdAt: now,
    updatedAt: now
  };
}
function getProject(id) {
  const db = getDatabase();
  const row = db.prepare(
    `
    SELECT * FROM projects WHERE id = ?
  `
  ).get(id);
  if (!row) {
    return null;
  }
  return rowToProject(row);
}
function getProjectByWorkdir(workdir) {
  const db = getDatabase();
  const row = db.prepare(
    `
    SELECT * FROM projects WHERE workdir = ?
  `
  ).get(workdir);
  if (!row) {
    return null;
  }
  return rowToProject(row);
}
function listProjects() {
  const db = getDatabase();
  const rows = db.prepare(
    `
    SELECT * FROM projects ORDER BY updated_at DESC
  `
  ).all();
  return rows.map(rowToProject);
}
function updateProject(id, updates) {
  const db = getDatabase();
  const now = (/* @__PURE__ */ new Date()).toISOString();
  const sets = ["updated_at = ?"];
  const values = [now];
  if (updates.name !== void 0) {
    sets.push("name = ?");
    values.push(updates.name);
  }
  if (updates.customInstructions !== void 0) {
    sets.push("custom_instructions = ?");
    values.push(updates.customInstructions);
  }
  if (updates.dangerLevel !== void 0) {
    sets.push("danger_level = ?");
    values.push(updates.dangerLevel);
  }
  values.push(id);
  const result = db.prepare(
    `
    UPDATE projects SET ${sets.join(", ")} WHERE id = ?
  `
  ).run(...values);
  if (result.changes === 0) {
    return null;
  }
  return getProject(id);
}
function deleteProject(id) {
  const db = getDatabase();
  db.prepare("DELETE FROM projects WHERE id = ?").run(id);
}
function toggleStar(id, isStarred) {
  const db = getDatabase();
  db.prepare("UPDATE projects SET is_starred = ? WHERE id = ?").run(isStarred ? 1 : 0, id);
  return getProject(id);
}
function rowToProject(row) {
  return {
    id: row.id,
    name: row.name,
    workdir: row.workdir,
    ...row.custom_instructions ? { customInstructions: row.custom_instructions } : {},
    ...row.danger_level ? { dangerLevel: row.danger_level } : {},
    isStarred: !!row.is_starred,
    createdAt: row.created_at,
    updatedAt: row.updated_at
  };
}

export {
  createProject,
  getProject,
  getProjectByWorkdir,
  listProjects,
  updateProject,
  deleteProject,
  toggleStar
};
//# sourceMappingURL=chunk-XAMAYRDA.js.map