import runDbClient_default from "../../../data/db/runDbClient.js";
import { sessionAuth } from "../../../middleware/sessionAuth.js";
import { openapiRegisterPutPatchRoutesForLegacy } from "../../../utils/openapiDualRoute.js";
import { OpenAPIHono } from "@hono/zod-openapi";
import { UserIdParamsSchema, UserProfileApiUpdateSchema, UserProfileSelectSchema, commonGetErrorResponses, createApiError, getUserProfile, upsertUserProfile } from "@inkeep/agents-core";
import { createProtectedRoute, inheritedManageTenantAuth } from "@inkeep/agents-core/middleware";

//#region src/domains/manage/routes/userProfile.ts
const app = new OpenAPIHono();
app.use("*", sessionAuth());
app.openapi(createProtectedRoute({
	method: "get",
	path: "/{userId}/profile",
	summary: "Get User Profile",
	description: "Get the profile for a specific user. Users can only access their own profile.",
	operationId: "get-user-profile",
	tags: ["User Profile"],
	permission: inheritedManageTenantAuth(),
	request: { params: UserIdParamsSchema },
	responses: {
		200: {
			description: "User profile",
			content: { "application/json": { schema: UserProfileSelectSchema } }
		},
		...commonGetErrorResponses
	}
}), async (c) => {
	const { userId } = c.req.valid("param");
	if (userId !== c.get("userId")) throw createApiError({
		code: "forbidden",
		message: "Cannot access another user's profile"
	});
	const profile = await getUserProfile(runDbClient_default)(userId);
	if (!profile) throw createApiError({
		code: "not_found",
		message: "User profile not found"
	});
	return c.json({
		id: profile.id,
		userId: profile.userId,
		timezone: profile.timezone,
		attributes: profile.attributes ?? {},
		createdAt: profile.createdAt,
		updatedAt: profile.updatedAt
	}, 200);
});
const upsertUserProfileRouteConfig = {
	path: "/{userId}/profile",
	summary: "Upsert User Profile",
	description: "Create or update the profile for a specific user. Users can only update their own profile.",
	tags: ["User Profile"],
	permission: inheritedManageTenantAuth(),
	request: {
		params: UserIdParamsSchema,
		body: { content: { "application/json": { schema: UserProfileApiUpdateSchema } } }
	},
	responses: {
		200: {
			description: "Updated user profile",
			content: { "application/json": { schema: UserProfileSelectSchema } }
		},
		...commonGetErrorResponses
	}
};
const upsertUserProfileHandler = async (c) => {
	const { userId } = c.req.valid("param");
	if (userId !== c.get("userId")) throw createApiError({
		code: "forbidden",
		message: "Cannot update another user's profile"
	});
	const body = c.req.valid("json");
	const updated = await upsertUserProfile(runDbClient_default)(userId, {
		...body,
		attributes: body.attributes ?? {}
	});
	return c.json({
		id: updated.id,
		userId: updated.userId,
		timezone: updated.timezone,
		attributes: updated.attributes ?? {},
		createdAt: updated.createdAt,
		updatedAt: updated.updatedAt
	}, 200);
};
openapiRegisterPutPatchRoutesForLegacy(app, upsertUserProfileRouteConfig, upsertUserProfileHandler, { operationId: "upsert-user-profile" });
var userProfile_default = app;

//#endregion
export { userProfile_default as default };