import runDbClient_default from "../../../data/db/runDbClient.js";
import { sessionAuth } from "../../../middleware/sessionAuth.js";
import { createApiError, getUserOrganizationsFromDb } from "@inkeep/agents-core";
import { Hono } from "hono";

//#region src/domains/manage/routes/userOrganizations.ts
const userOrganizationsRoutes = new Hono();
userOrganizationsRoutes.use("*", sessionAuth());
userOrganizationsRoutes.get("/", async (c) => {
	const userId = c.req.param("userId");
	const authenticatedUserId = c.get("userId");
	if (!userId) throw createApiError({
		code: "bad_request",
		message: "User ID is required"
	});
	if (userId !== authenticatedUserId) throw createApiError({
		code: "forbidden",
		message: "Cannot access another user's organizations"
	});
	const userOrganizations = (await getUserOrganizationsFromDb(runDbClient_default)(userId)).map((org) => ({
		...org,
		createdAt: org.createdAt.toISOString()
	}));
	return c.json(userOrganizations);
});
var userOrganizations_default = userOrganizationsRoutes;

//#endregion
export { userOrganizations_default as default };