import { requireProjectPermission } from "../../../middleware/projectAccess.js";
import { OpenAPIHono, z } from "@hono/zod-openapi";
import { ArtifactComponentArrayResponse, ComponentAssociationListResponse, ErrorResponseSchema, ExistsResponseSchema, RemovedResponseSchema, SubAgentArtifactComponentApiInsertSchema, SubAgentArtifactComponentResponse, TenantProjectAgentParamsSchema, TenantProjectAgentSubAgentParamsSchema, associateArtifactComponentWithAgent, commonGetErrorResponses, createApiError, getAgentsUsingArtifactComponent, getArtifactComponentById, getArtifactComponentsForAgent, getSubAgentById, isArtifactComponentAssociatedWithAgent, removeArtifactComponentFromAgent } from "@inkeep/agents-core";
import { createProtectedRoute } from "@inkeep/agents-core/middleware";

//#region src/domains/manage/routes/subAgentArtifactComponents.ts
const app = new OpenAPIHono();
app.openapi(createProtectedRoute({
	method: "get",
	path: "/agent/{subAgentId}",
	summary: "Get Artifact Components for Agent",
	operationId: "get-artifact-components-for-agent",
	tags: ["Agents", "Artifact Components"],
	permission: requireProjectPermission("view"),
	request: { params: TenantProjectAgentSubAgentParamsSchema },
	responses: {
		200: {
			description: "Artifact components retrieved successfully",
			content: { "application/json": { schema: ArtifactComponentArrayResponse } }
		},
		...commonGetErrorResponses
	}
}), async (c) => {
	const db = c.get("db");
	const { tenantId, projectId, agentId, subAgentId } = c.req.valid("param");
	const artifactComponents = await getArtifactComponentsForAgent(db)({ scopes: {
		tenantId,
		projectId,
		agentId,
		subAgentId
	} });
	return c.json({ data: artifactComponents });
});
app.openapi(createProtectedRoute({
	method: "get",
	path: "/component/{artifactComponentId}/agents",
	summary: "Get Agents Using Artifact Component",
	operationId: "get-agents-using-artifact-component",
	tags: ["Agents", "Artifact Components"],
	permission: requireProjectPermission("view"),
	request: { params: TenantProjectAgentParamsSchema.extend({ artifactComponentId: z.string() }) },
	responses: {
		200: {
			description: "Agents retrieved successfully",
			content: { "application/json": { schema: ComponentAssociationListResponse } }
		},
		...commonGetErrorResponses
	}
}), async (c) => {
	const db = c.get("db");
	const { tenantId, projectId, artifactComponentId } = c.req.valid("param");
	const agents = await getAgentsUsingArtifactComponent(db)({
		scopes: {
			tenantId,
			projectId
		},
		artifactComponentId
	});
	return c.json({ data: agents });
});
app.openapi(createProtectedRoute({
	method: "post",
	path: "/",
	summary: "Associate Artifact Component with Agent",
	operationId: "associate-artifact-component-with-agent",
	tags: ["Agents", "Artifact Components"],
	permission: requireProjectPermission("edit"),
	request: {
		params: TenantProjectAgentParamsSchema,
		body: { content: { "application/json": { schema: SubAgentArtifactComponentApiInsertSchema } } }
	},
	responses: {
		201: {
			description: "Agent artifact component association created successfully",
			content: { "application/json": { schema: SubAgentArtifactComponentResponse } }
		},
		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, artifactComponentId } = c.req.valid("json");
	const agent = await getSubAgentById(db)({
		scopes: {
			tenantId,
			projectId,
			agentId
		},
		subAgentId
	});
	const artifactComponent = await getArtifactComponentById(db)({
		scopes: {
			tenantId,
			projectId
		},
		id: artifactComponentId
	});
	if (!agent) throw createApiError({
		code: "not_found",
		message: `Agent with id '${subAgentId}' not found`
	});
	if (!artifactComponent) throw createApiError({
		code: "not_found",
		message: `Artifact component with id '${artifactComponentId}' not found`
	});
	if (await isArtifactComponentAssociatedWithAgent(db)({
		scopes: {
			tenantId,
			projectId,
			agentId,
			subAgentId
		},
		artifactComponentId
	})) throw createApiError({
		code: "conflict",
		message: "Agent artifact component association already exists"
	});
	const association = await associateArtifactComponentWithAgent(db)({
		scopes: {
			tenantId,
			projectId,
			agentId,
			subAgentId
		},
		artifactComponentId
	});
	return c.json({ data: association }, 201);
});
app.openapi(createProtectedRoute({
	method: "delete",
	path: "/agent/{subAgentId}/component/{artifactComponentId}",
	summary: "Remove Artifact Component from Agent",
	operationId: "remove-artifact-component-from-agent",
	tags: ["Agents", "Artifact Components"],
	permission: requireProjectPermission("edit"),
	request: { params: TenantProjectAgentSubAgentParamsSchema.extend({ artifactComponentId: 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, artifactComponentId } = c.req.valid("param");
	if (!await removeArtifactComponentFromAgent(db)({
		scopes: {
			tenantId,
			projectId,
			agentId,
			subAgentId
		},
		artifactComponentId
	})) throw createApiError({
		code: "not_found",
		message: "Agent artifact component association not found"
	});
	return c.json({
		message: "Association removed successfully",
		removed: true
	});
});
app.openapi(createProtectedRoute({
	method: "get",
	path: "/agent/{subAgentId}/component/{artifactComponentId}/exists",
	summary: "Check if Artifact Component is Associated with Agent",
	operationId: "check-artifact-component-agent-association",
	tags: ["Agents", "Artifact Components"],
	permission: requireProjectPermission("view"),
	request: { params: TenantProjectAgentSubAgentParamsSchema.extend({ artifactComponentId: 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, artifactComponentId } = c.req.valid("param");
	const exists = await isArtifactComponentAssociatedWithAgent(db)({
		scopes: {
			tenantId,
			projectId,
			agentId,
			subAgentId
		},
		artifactComponentId
	});
	return c.json({ exists });
});
var subAgentArtifactComponents_default = app;

//#endregion
export { subAgentArtifactComponents_default as default };