import {
  init_ids,
  newId
} from "./chunk-F4WUVOLE.js";
import {
  init_clock,
  nowMs,
  toIso
} from "./chunk-HFDLUBWN.js";
import {
  getDrizzle,
  init_drizzle
} from "./chunk-RII4M3J2.js";
import {
  schema_exports
} from "./chunk-DDJOPRB7.js";
import {
  __esm,
  __export
} from "./chunk-2ESYSVXG.js";

// src/db/session-resources.ts
var session_resources_exports = {};
__export(session_resources_exports, {
  countResources: () => countResources,
  createResource: () => createResource,
  deleteResource: () => deleteResource,
  getResource: () => getResource,
  listResources: () => listResources,
  updateResource: () => updateResource
});
import { eq, and, desc, gt, lt, sql } from "drizzle-orm";
function hydrate(row) {
  const record = {
    id: row.id,
    type: row.type,
    session_id: row.session_id,
    created_at: toIso(row.created_at),
    updated_at: toIso(row.updated_at)
  };
  if (row.file_id) record.file_id = row.file_id;
  if (row.mount_path) record.mount_path = row.mount_path;
  if (row.url) record.url = row.url;
  if (row.checkout_json) {
    try {
      const parsed = JSON.parse(row.checkout_json);
      if (row.type === "memory_store") {
        record.memory_store_id = parsed.memory_store_id;
        record.access = parsed.access;
        record.instructions = parsed.instructions;
      } else {
        record.checkout = parsed;
      }
    } catch {
    }
  }
  return record;
}
function createResource(sessionId, input) {
  const db = getDrizzle();
  const id = newId("sesrsc");
  const now = nowMs();
  let checkoutJson = null;
  if (input.type === "memory_store") {
    checkoutJson = JSON.stringify({
      memory_store_id: input.memory_store_id,
      access: input.access ?? "read_write",
      instructions: input.instructions
    });
  } else if (input.checkout) {
    checkoutJson = JSON.stringify(input.checkout);
  } else if (input.branch) {
    checkoutJson = JSON.stringify({ type: "branch", name: input.branch });
  } else if (input.commit) {
    checkoutJson = JSON.stringify({ type: "commit", name: input.commit });
  }
  const url = input.url ?? input.repository_url ?? input.uri ?? null;
  db.insert(schema_exports.sessionResources).values({
    id,
    session_id: sessionId,
    type: input.type,
    file_id: input.file_id ?? null,
    mount_path: input.mount_path ?? null,
    url,
    checkout_json: checkoutJson,
    created_at: now,
    updated_at: now
  }).run();
  return hydrate({
    id,
    session_id: sessionId,
    type: input.type,
    file_id: input.file_id ?? null,
    mount_path: input.mount_path ?? null,
    url,
    checkout_json: checkoutJson,
    created_at: now,
    updated_at: now
  });
}
function listResources(sessionId, opts) {
  const db = getDrizzle();
  const limit = opts?.limit ?? 100;
  const conditions = [eq(schema_exports.sessionResources.session_id, sessionId)];
  if (opts?.after_id) conditions.push(gt(schema_exports.sessionResources.id, opts.after_id));
  if (opts?.before_id) conditions.push(lt(schema_exports.sessionResources.id, opts.before_id));
  const rows = db.select().from(schema_exports.sessionResources).where(and(...conditions)).orderBy(desc(schema_exports.sessionResources.id)).limit(limit).all();
  return rows.map(hydrate);
}
function getResource(sessionId, resourceId) {
  const db = getDrizzle();
  const row = db.select().from(schema_exports.sessionResources).where(
    and(
      eq(schema_exports.sessionResources.session_id, sessionId),
      eq(schema_exports.sessionResources.id, resourceId)
    )
  ).get();
  return row ? hydrate(row) : null;
}
function deleteResource(sessionId, resourceId) {
  const db = getDrizzle();
  const row = db.select().from(schema_exports.sessionResources).where(
    and(
      eq(schema_exports.sessionResources.session_id, sessionId),
      eq(schema_exports.sessionResources.id, resourceId)
    )
  ).get();
  if (!row) return null;
  db.delete(schema_exports.sessionResources).where(eq(schema_exports.sessionResources.id, resourceId)).run();
  return { id: resourceId, type: "session_resource_deleted" };
}
function updateResource(sessionId, resourceId, fields) {
  const db = getDrizzle();
  const existing = db.select().from(schema_exports.sessionResources).where(
    and(
      eq(schema_exports.sessionResources.session_id, sessionId),
      eq(schema_exports.sessionResources.id, resourceId)
    )
  ).get();
  if (!existing) return null;
  const now = nowMs();
  const parts = { updated_at: now };
  if (fields.mount_path !== void 0) parts.mount_path = fields.mount_path;
  db.update(schema_exports.sessionResources).set(parts).where(eq(schema_exports.sessionResources.id, resourceId)).run();
  return getResource(sessionId, resourceId);
}
function countResources(sessionId) {
  const db = getDrizzle();
  const result = db.all(
    sql`SELECT COUNT(*) as cnt FROM session_resources WHERE session_id = ${sessionId}`
  );
  return result[0]?.cnt ?? 0;
}
var init_session_resources = __esm({
  "src/db/session-resources.ts"() {
    init_drizzle();
    init_ids();
    init_clock();
  }
});

export {
  createResource,
  listResources,
  getResource,
  deleteResource,
  updateResource,
  countResources,
  session_resources_exports,
  init_session_resources
};
