import { requireProjectPermission } from "../../../middleware/projectAccess.js";
import { OpenAPIHono, z } from "@hono/zod-openapi";
import { ComponentAssociationListResponse, DataComponentArrayResponse, ErrorResponseSchema, ExistsResponseSchema, RemovedResponseSchema, SubAgentDataComponentApiInsertSchema, SubAgentDataComponentResponse, TenantProjectAgentParamsSchema, TenantProjectAgentSubAgentParamsSchema, associateDataComponentWithAgent, commonGetErrorResponses, createApiError, getAgentsUsingDataComponent, getDataComponent, getDataComponentsForAgent, getSubAgentById, isDataComponentAssociatedWithAgent, removeDataComponentFromAgent } from "@inkeep/agents-core";
import { createProtectedRoute } from "@inkeep/agents-core/middleware";

//#region src/domains/manage/routes/subAgentDataComponents.ts
const app = new OpenAPIHono();
app.openapi(createProtectedRoute({
	method: "get",
	path: "/agent/{subAgentId}",
	summary: "Get Data Components for Agent",
	operationId: "get-data-components-for-agent",
	tags: ["Agents", "Data Components"],
	permission: requireProjectPermission("view"),
	request: { params: TenantProjectAgentSubAgentParamsSchema },
	responses: {
		200: {
			description: "Data components retrieved successfully",
			content: { "application/json": { schema: DataComponentArrayResponse } }
		},
		...commonGetErrorResponses
	}
}), async (c) => {
	const db = c.get("db");
	const { tenantId, projectId, agentId, subAgentId } = c.req.valid("param");
	const dataComponents = await getDataComponentsForAgent(db)({ scopes: {
		tenantId,
		projectId,
		agentId,
		subAgentId
	} });
	return c.json({ data: dataComponents });
});
app.openapi(createProtectedRoute({
	method: "get",
	path: "/component/{dataComponentId}/agents",
	summary: "Get Agents Using Data Component",
	operationId: "get-agents-using-data-component",
	tags: ["Agents", "Data Components"],
	permission: requireProjectPermission("view"),
	request: { params: TenantProjectAgentParamsSchema.extend({ dataComponentId: z.string() }) },
	responses: {
		200: {
			description: "Agents retrieved successfully",
			content: { "application/json": { schema: ComponentAssociationListResponse } }
		},
		...commonGetErrorResponses
	}
}), async (c) => {
	const db = c.get("db");
	const { tenantId, projectId, dataComponentId } = c.req.valid("param");
	const agents = await getAgentsUsingDataComponent(db)({
		scopes: {
			tenantId,
			projectId
		},
		dataComponentId
	});
	return c.json({ data: agents });
});
app.openapi(createProtectedRoute({
	method: "post",
	path: "/",
	summary: "Associate Data Component with Agent",
	operationId: "associate-data-component-with-agent",
	tags: ["Agents", "Data Components"],
	permission: requireProjectPermission("edit"),
	request: {
		params: TenantProjectAgentParamsSchema,
		body: { content: { "application/json": { schema: SubAgentDataComponentApiInsertSchema } } }
	},
	responses: {
		201: {
			description: "Agent data component association created successfully",
			content: { "application/json": { schema: SubAgentDataComponentResponse } }
		},
		409: {
			description: "Association already exists",
			content: { "application/json": { schema: ErrorResponseSchema } }
		},
		...commonGetErrorResponses
	}
}), async (c) => {
	const db = c.get("db");
	const { tenantId, projectId, agentId } = c.req.valid("param");
	const { subAgentId, dataComponentId } = c.req.valid("json");
	const [agent, dataComponent] = await Promise.all([getSubAgentById(db)({
		scopes: {
			tenantId,
			projectId,
			agentId
		},
		subAgentId
	}), getDataComponent(db)({
		scopes: {
			tenantId,
			projectId
		},
		dataComponentId
	})]);
	if (!agent) throw createApiError({
		code: "not_found",
		message: `Agent with id '${subAgentId}' not found`
	});
	if (!dataComponent) throw createApiError({
		code: "not_found",
		message: `Data component with id '${dataComponentId}' not found`
	});
	if (await isDataComponentAssociatedWithAgent(db)({
		scopes: {
			tenantId,
			projectId,
			agentId,
			subAgentId
		},
		dataComponentId
	})) throw createApiError({
		code: "conflict",
		message: "Agent data component association already exists"
	});
	const association = await associateDataComponentWithAgent(db)({
		scopes: {
			tenantId,
			projectId,
			agentId,
			subAgentId
		},
		dataComponentId
	});
	return c.json({ data: association }, 201);
});
app.openapi(createProtectedRoute({
	method: "delete",
	path: "/agent/{subAgentId}/component/{dataComponentId}",
	summary: "Remove Data Component from Agent",
	operationId: "remove-data-component-from-agent",
	tags: ["Agents", "Data Components"],
	permission: requireProjectPermission("edit"),
	request: { params: TenantProjectAgentSubAgentParamsSchema.extend({ dataComponentId: z.string() }) },
	responses: {
		200: {
			description: "Association removed successfully",
			content: { "application/json": { schema: RemovedResponseSchema } }
		},
		...commonGetErrorResponses
	}
}), async (c) => {
	const db = c.get("db");
	const { tenantId, projectId, agentId, subAgentId, dataComponentId } = c.req.valid("param");
	if (!await removeDataComponentFromAgent(db)({
		scopes: {
			tenantId,
			projectId,
			agentId,
			subAgentId
		},
		dataComponentId
	})) throw createApiError({
		code: "not_found",
		message: "Agent data component association not found"
	});
	return c.json({
		message: "Association removed successfully",
		removed: true
	});
});
app.openapi(createProtectedRoute({
	method: "get",
	path: "/agent/{subAgentId}/component/{dataComponentId}/exists",
	summary: "Check if Data Component is Associated with Agent",
	operationId: "check-data-component-agent-association",
	tags: ["Agents", "Data Components"],
	permission: requireProjectPermission("view"),
	request: { params: TenantProjectAgentSubAgentParamsSchema.extend({ dataComponentId: z.string() }) },
	responses: {
		200: {
			description: "Association status retrieved successfully",
			content: { "application/json": { schema: ExistsResponseSchema } }
		},
		...commonGetErrorResponses
	}
}), async (c) => {
	const db = c.get("db");
	const { tenantId, projectId, agentId, subAgentId, dataComponentId } = c.req.valid("param");
	const exists = await isDataComponentAssociatedWithAgent(db)({
		scopes: {
			tenantId,
			projectId,
			agentId,
			subAgentId
		},
		dataComponentId
	});
	return c.json({ exists });
});
var subAgentDataComponents_default = app;

//#endregion
export { subAgentDataComponents_default as default };