import {
  assertResourceTenant
} from "./chunk-K7DLILHK.js";
import {
  jsonOk,
  routeWrap
} from "./chunk-QSHK4MUE.js";
import {
  getProxiedTenantId
} from "./chunk-DZIKCTP6.js";
import {
  getSession,
  updateSessionResources
} from "./chunk-SEGKFEG5.js";
import {
  countResources,
  createResource,
  deleteResource,
  getResource,
  init_session_resources,
  listResources,
  updateResource
} from "./chunk-UU3ECGO5.js";
import {
  getDb,
  init_client
} from "./chunk-TLEKKGH7.js";
import {
  badRequest,
  notFound
} from "./chunk-EZYKRG4W.js";

// src/handlers/anthropic-compat/resources.ts
import { z } from "zod";
init_client();
init_session_resources();
var MAX_RESOURCES_PER_SESSION = 100;
function assertSessionTenant(auth, sessionId) {
  const row = getDb().prepare(`SELECT tenant_id FROM sessions WHERE id = ?`).get(sessionId);
  if (row) {
    assertResourceTenant(auth, row.tenant_id, `session not found: ${sessionId}`);
    return;
  }
  const proxyTenant = getProxiedTenantId(sessionId);
  if (proxyTenant !== void 0) {
    assertResourceTenant(auth, proxyTenant, `session not found: ${sessionId}`);
    return;
  }
  throw notFound(`session not found: ${sessionId}`);
}
var AddResourceSchema = z.object({
  type: z.enum(["uri", "text", "file", "github_repository"]),
  uri: z.string().optional(),
  content: z.string().optional(),
  file_id: z.string().optional(),
  mount_path: z.string().optional(),
  url: z.string().optional(),
  repository_url: z.string().optional(),
  branch: z.string().optional(),
  commit: z.string().optional(),
  checkout: z.object({
    type: z.string(),
    name: z.string()
  }).optional()
});
function handleAddResource(request, sessionId) {
  return routeWrap(request, async ({ auth }) => {
    assertSessionTenant(auth, sessionId);
    const session = getSession(sessionId);
    if (!session) throw notFound(`session not found: ${sessionId}`);
    const body = await request.json();
    const parsed = AddResourceSchema.safeParse(body);
    if (!parsed.success) throw badRequest(parsed.error.message);
    const count = countResources(sessionId);
    if (count >= MAX_RESOURCES_PER_SESSION) {
      throw badRequest(`Maximum of ${MAX_RESOURCES_PER_SESSION} resources per session exceeded`);
    }
    const input = parsed.data;
    const resource = createResource(sessionId, input);
    const resources = session.resources ?? [];
    resources.push({
      type: input.type,
      uri: input.uri,
      content: input.content,
      file_id: input.file_id,
      mount_path: input.mount_path,
      repository_url: input.repository_url ?? input.url,
      branch: input.branch ?? input.checkout?.name,
      commit: input.commit
    });
    updateSessionResources(sessionId, JSON.stringify(resources));
    return jsonOk(resource, 201);
  });
}
function handleListResources(request, sessionId) {
  return routeWrap(request, async ({ auth }) => {
    assertSessionTenant(auth, sessionId);
    const session = getSession(sessionId);
    if (!session) throw notFound(`session not found: ${sessionId}`);
    const url = new URL(request.url);
    const limit = Number(url.searchParams.get("limit") || "100");
    const after_id = url.searchParams.get("after_id") || void 0;
    const before_id = url.searchParams.get("before_id") || void 0;
    const resources = listResources(sessionId, { limit, after_id, before_id });
    return jsonOk({ data: resources });
  });
}
function handleGetResource(request, sessionId, resourceId) {
  return routeWrap(request, async ({ auth }) => {
    assertSessionTenant(auth, sessionId);
    const resource = getResource(sessionId, resourceId);
    if (!resource) throw notFound(`resource not found: ${resourceId}`);
    return jsonOk(resource);
  });
}
var UpdateResourceSchema = z.object({
  mount_path: z.string().optional()
});
function handleUpdateResource(request, sessionId, resourceId) {
  return routeWrap(request, async ({ auth }) => {
    assertSessionTenant(auth, sessionId);
    const session = getSession(sessionId);
    if (!session) throw notFound(`session not found: ${sessionId}`);
    const body = await request.json();
    const parsed = UpdateResourceSchema.safeParse(body);
    if (!parsed.success) throw badRequest(parsed.error.message);
    const resource = updateResource(sessionId, resourceId, parsed.data);
    if (!resource) throw notFound(`resource not found: ${resourceId}`);
    return jsonOk(resource);
  });
}
function handleDeleteResource(request, sessionId, resourceId) {
  return routeWrap(request, async ({ auth }) => {
    assertSessionTenant(auth, sessionId);
    const session = getSession(sessionId);
    if (!session) throw notFound(`session not found: ${sessionId}`);
    const result = deleteResource(sessionId, resourceId);
    if (!result) throw notFound(`resource not found: ${resourceId}`);
    return jsonOk(result);
  });
}

export {
  handleAddResource,
  handleListResources,
  handleGetResource,
  handleUpdateResource,
  handleDeleteResource
};
