import runDbClient_default from "../../../data/db/runDbClient.js";
import { requireProjectPermission } from "../../../middleware/projectAccess.js";
import { openapiRegisterPutPatchRoutesForLegacy } from "../../../utils/openapiDualRoute.js";
import { speakeasyOffsetLimitPagination } from "../../../utils/speakeasy.js";
import { OpenAPIHono, z } from "@hono/zod-openapi";
import { AppApiCreationResponseSchema, AppApiInsertSchema, AppApiUpdateSchema, AppListResponse, AppResponse, ErrorResponseSchema, PaginationQueryParamsSchema, TenantProjectIdParamsSchema, TenantProjectParamsSchema, WebClientConfigSchema, commonGetErrorResponses, createApiError, createApp, deleteAppForProject, generateAppCredential, getAppByIdForProject, listAppsPaginated, sanitizeAppConfig, updateAppForProject } from "@inkeep/agents-core";
import { createProtectedRoute } from "@inkeep/agents-core/middleware";

//#region src/domains/manage/routes/apps.ts
const app = new OpenAPIHono();
app.openapi(createProtectedRoute({
	method: "get",
	permission: requireProjectPermission("view"),
	path: "/",
	summary: "List Apps",
	description: "List all app credentials for a project",
	operationId: "list-apps",
	tags: ["Apps"],
	request: {
		params: TenantProjectParamsSchema,
		query: PaginationQueryParamsSchema.extend({ type: z.enum(["web_client", "api"]).optional().describe("Filter by app type") })
	},
	responses: {
		200: {
			description: "List of apps retrieved successfully",
			content: { "application/json": { schema: AppListResponse } }
		},
		...commonGetErrorResponses
	},
	...speakeasyOffsetLimitPagination
}), async (c) => {
	const { tenantId, projectId } = c.req.valid("param");
	const page = Number(c.req.query("page")) || 1;
	const limit = Math.min(Number(c.req.query("limit")) || 10, 100);
	const type = c.req.query("type");
	const result = await listAppsPaginated(runDbClient_default)({
		scopes: {
			tenantId,
			projectId
		},
		pagination: {
			page,
			limit
		},
		type
	});
	const sanitizedData = result.data.map((app$1) => sanitizeAppConfig(app$1));
	return c.json({
		data: sanitizedData,
		pagination: result.pagination
	});
});
app.openapi(createProtectedRoute({
	method: "get",
	path: "/{id}",
	summary: "Get App",
	description: "Get a specific app credential by ID",
	operationId: "get-app-by-id",
	tags: ["Apps"],
	permission: requireProjectPermission("view"),
	request: { params: TenantProjectIdParamsSchema },
	responses: {
		200: {
			description: "App found",
			content: { "application/json": { schema: AppResponse } }
		},
		...commonGetErrorResponses
	}
}), async (c) => {
	const { tenantId, projectId, id } = c.req.valid("param");
	const appRecord = await getAppByIdForProject(runDbClient_default)({
		scopes: {
			tenantId,
			projectId
		},
		id
	});
	if (!appRecord) throw createApiError({
		code: "not_found",
		message: "App not found"
	});
	return c.json({ data: sanitizeAppConfig(appRecord) });
});
app.openapi(createProtectedRoute({
	method: "post",
	path: "/",
	summary: "Create App",
	description: "Create a new app credential.",
	operationId: "create-app",
	tags: ["Apps"],
	permission: requireProjectPermission("edit"),
	request: {
		params: TenantProjectParamsSchema,
		body: { content: { "application/json": { schema: AppApiInsertSchema } } }
	},
	responses: {
		201: {
			description: "App created successfully",
			content: { "application/json": { schema: AppApiCreationResponseSchema } }
		},
		...commonGetErrorResponses
	}
}), async (c) => {
	const { tenantId, projectId } = c.req.valid("param");
	const body = c.req.valid("json");
	const credential = generateAppCredential();
	const result = await createApp(runDbClient_default)({
		...body,
		tenantId,
		projectId,
		id: credential.id,
		defaultProjectId: body.defaultAgentId ? body.defaultProjectId ?? projectId : null,
		enabled: body.enabled ?? true
	});
	return c.json({ data: { app: sanitizeAppConfig(result) } }, 201);
});
const updateAppRouteConfig = {
	path: "/{id}",
	summary: "Update App",
	description: "Update an app credential configuration",
	tags: ["Apps"],
	permission: requireProjectPermission("edit"),
	request: {
		params: TenantProjectIdParamsSchema,
		body: { content: { "application/json": { schema: AppApiUpdateSchema } } }
	},
	responses: {
		200: {
			description: "App updated successfully",
			content: { "application/json": { schema: AppResponse } }
		},
		...commonGetErrorResponses
	}
};
const updateAppHandler = async (c) => {
	const { tenantId, projectId, id } = c.req.valid("param");
	const data = { ...c.req.valid("json") };
	if ("defaultAgentId" in data) data.defaultProjectId = data.defaultAgentId ? data.defaultProjectId ?? projectId : null;
	if (data.config && data.config.type === "web_client") {
		const parsed = WebClientConfigSchema.safeParse(data.config);
		if (!parsed.success) throw createApiError({
			code: "bad_request",
			message: `Invalid web client config: ${parsed.error.issues.map((i) => i.message).join(", ")}`
		});
		const existingApp = await getAppByIdForProject(runDbClient_default)({
			scopes: {
				tenantId,
				projectId
			},
			id
		});
		if (existingApp?.config?.type === "web_client") {
			const existingWc = existingApp.config.webClient;
			const incomingWc = parsed.data.webClient;
			data.config = {
				type: "web_client",
				webClient: {
					...existingWc,
					allowedDomains: incomingWc.allowedDomains ?? existingWc.allowedDomains,
					...incomingWc.allowAnonymous !== void 0 && { allowAnonymous: incomingWc.allowAnonymous },
					...incomingWc.audience !== void 0 && { audience: incomingWc.audience }
				}
			};
		}
	}
	const updatedApp = await updateAppForProject(runDbClient_default)({
		scopes: {
			tenantId,
			projectId
		},
		id,
		data
	});
	if (!updatedApp) throw createApiError({
		code: "not_found",
		message: "App not found"
	});
	return c.json({ data: sanitizeAppConfig(updatedApp) });
};
openapiRegisterPutPatchRoutesForLegacy(app, updateAppRouteConfig, updateAppHandler, { operationId: "update-app" });
app.openapi(createProtectedRoute({
	method: "delete",
	path: "/{id}",
	summary: "Delete App",
	description: "Delete an app credential permanently",
	operationId: "delete-app",
	tags: ["Apps"],
	permission: requireProjectPermission("edit"),
	request: { params: TenantProjectIdParamsSchema },
	responses: {
		204: { description: "App deleted successfully" },
		404: {
			description: "App not found",
			content: { "application/json": { schema: ErrorResponseSchema } }
		}
	}
}), async (c) => {
	const { tenantId, projectId, id } = c.req.valid("param");
	if (!await deleteAppForProject(runDbClient_default)({
		scopes: {
			tenantId,
			projectId
		},
		id
	})) throw createApiError({
		code: "not_found",
		message: "App not found"
	});
	return c.body(null, 204);
});
var apps_default = app;

//#endregion
export { apps_default as default };