import { requireProjectPermission } from "../../../middleware/projectAccess.js";
import { openapiRegisterPutPatchRoutesForLegacy } from "../../../utils/openapiDualRoute.js";
import { speakeasyOffsetLimitPagination } from "../../../utils/speakeasy.js";
import { OpenAPIHono } from "@hono/zod-openapi";
import { DataComponentApiInsertSchema, DataComponentApiUpdateSchema, DataComponentListResponse, DataComponentResponse, ErrorResponseSchema, PaginationQueryParamsSchema, TenantProjectIdParamsSchema, TenantProjectParamsSchema, commonGetErrorResponses, createApiError, createDataComponent, deleteDataComponent, getDataComponent, listDataComponentsPaginated, updateDataComponent, validatePropsAsJsonSchema } from "@inkeep/agents-core";
import { createProtectedRoute } from "@inkeep/agents-core/middleware";

//#region src/domains/manage/routes/dataComponents.ts
const app = new OpenAPIHono();
app.openapi(createProtectedRoute({
	method: "get",
	path: "/",
	summary: "List Data Components",
	operationId: "list-data-components",
	tags: ["Data Components"],
	permission: requireProjectPermission("view"),
	request: {
		params: TenantProjectParamsSchema,
		query: PaginationQueryParamsSchema
	},
	responses: {
		200: {
			description: "List of data components retrieved successfully",
			content: { "application/json": { schema: DataComponentListResponse } }
		},
		...commonGetErrorResponses
	},
	...speakeasyOffsetLimitPagination
}), async (c) => {
	const db = c.get("db");
	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 result = await listDataComponentsPaginated(db)({
		scopes: {
			tenantId,
			projectId
		},
		pagination: {
			page,
			limit
		}
	});
	return c.json(result);
});
app.openapi(createProtectedRoute({
	method: "get",
	path: "/{id}",
	summary: "Get Data Component",
	operationId: "get-data-component-by-id",
	tags: ["Data Components"],
	permission: requireProjectPermission("view"),
	request: { params: TenantProjectIdParamsSchema },
	responses: {
		200: {
			description: "Data component found",
			content: { "application/json": { schema: DataComponentResponse } }
		},
		...commonGetErrorResponses
	}
}), async (c) => {
	const db = c.get("db");
	const { tenantId, projectId, id } = c.req.valid("param");
	const dataComponent = await getDataComponent(db)({
		scopes: {
			tenantId,
			projectId
		},
		dataComponentId: id
	});
	if (!dataComponent) throw createApiError({
		code: "not_found",
		message: "Data component not found"
	});
	return c.json({ data: dataComponent });
});
app.openapi(createProtectedRoute({
	method: "post",
	path: "/",
	summary: "Create Data Component",
	operationId: "create-data-component",
	tags: ["Data Components"],
	permission: requireProjectPermission("edit"),
	request: {
		params: TenantProjectParamsSchema,
		body: { content: { "application/json": { schema: DataComponentApiInsertSchema } } }
	},
	responses: {
		201: {
			description: "Data component created successfully",
			content: { "application/json": { schema: DataComponentResponse } }
		},
		...commonGetErrorResponses
	}
}), async (c) => {
	const db = c.get("db");
	const { tenantId, projectId } = c.req.valid("param");
	const body = c.req.valid("json");
	if (body.props) {
		const propsValidation = validatePropsAsJsonSchema(body.props);
		if (!propsValidation.isValid) throw createApiError({
			code: "bad_request",
			message: `Invalid props schema: ${propsValidation.errors.map((e) => `${e.field}: ${e.message}`).join(", ")}`
		});
	}
	const dataComponentData = {
		...body,
		tenantId,
		projectId
	};
	const dataComponent = await createDataComponent(db)(dataComponentData);
	return c.json({ data: dataComponent }, 201);
});
const updateDataComponentRouteConfig = {
	path: "/{id}",
	summary: "Update Data Component",
	tags: ["Data Components"],
	permission: requireProjectPermission("edit"),
	request: {
		params: TenantProjectIdParamsSchema,
		body: { content: { "application/json": { schema: DataComponentApiUpdateSchema } } }
	},
	responses: {
		200: {
			description: "Data component updated successfully",
			content: { "application/json": { schema: DataComponentResponse } }
		},
		...commonGetErrorResponses
	}
};
const updateDataComponentHandler = async (c) => {
	const db = c.get("db");
	const { tenantId, projectId, id } = c.req.valid("param");
	const body = c.req.valid("json");
	if (body.props !== void 0 && body.props !== null) {
		const propsValidation = validatePropsAsJsonSchema(body.props);
		if (!propsValidation.isValid) throw createApiError({
			code: "bad_request",
			message: `Invalid props schema: ${propsValidation.errors.map((e) => `${e.field}: ${e.message}`).join(", ")}`
		});
	}
	const updatedDataComponent = await updateDataComponent(db)({
		scopes: {
			tenantId,
			projectId
		},
		dataComponentId: id,
		data: body
	});
	if (!updatedDataComponent) throw createApiError({
		code: "not_found",
		message: "Data component not found"
	});
	return c.json({ data: updatedDataComponent });
};
openapiRegisterPutPatchRoutesForLegacy(app, updateDataComponentRouteConfig, updateDataComponentHandler, { operationId: "update-data-component" });
app.openapi(createProtectedRoute({
	method: "delete",
	path: "/{id}",
	summary: "Delete Data Component",
	operationId: "delete-data-component",
	tags: ["Data Components"],
	permission: requireProjectPermission("edit"),
	request: { params: TenantProjectIdParamsSchema },
	responses: {
		204: { description: "Data component deleted successfully" },
		404: {
			description: "Data component not found",
			content: { "application/json": { schema: ErrorResponseSchema } }
		}
	}
}), async (c) => {
	const db = c.get("db");
	const { tenantId, projectId, id } = c.req.valid("param");
	if (!await deleteDataComponent(db)({
		scopes: {
			tenantId,
			projectId
		},
		dataComponentId: id
	})) throw createApiError({
		code: "not_found",
		message: "Data component not found"
	});
	return c.body(null, 204);
});
var dataComponents_default = app;

//#endregion
export { dataComponents_default as default };