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 { ErrorResponseSchema, PaginationQueryParamsSchema, SubAgentTeamAgentRelationApiInsertSchema, SubAgentTeamAgentRelationApiUpdateSchema, SubAgentTeamAgentRelationListResponse, SubAgentTeamAgentRelationResponse, TenantProjectAgentSubAgentIdParamsSchema, TenantProjectAgentSubAgentParamsSchema, commonGetErrorResponses, createApiError, createSubAgentTeamAgentRelation, deleteSubAgentTeamAgentRelation, deriveRelationId, getSubAgentTeamAgentRelationById, listSubAgentTeamAgentRelations, updateSubAgentTeamAgentRelation } from "@inkeep/agents-core";
import { createProtectedRoute } from "@inkeep/agents-core/middleware";

//#region src/domains/manage/routes/subAgentTeamAgentRelations.ts
const app = new OpenAPIHono();
app.openapi(createProtectedRoute({
	method: "get",
	path: "/",
	summary: "List Sub Agent Team Agent Relations",
	operationId: "list-sub-agent-team-agent-relations",
	tags: ["SubAgents"],
	permission: requireProjectPermission("view"),
	request: {
		params: TenantProjectAgentSubAgentParamsSchema,
		query: PaginationQueryParamsSchema
	},
	responses: {
		200: {
			description: "List of sub agent team agent relations retrieved successfully",
			content: { "application/json": { schema: SubAgentTeamAgentRelationListResponse } }
		},
		...commonGetErrorResponses
	},
	...speakeasyOffsetLimitPagination
}), async (c) => {
	const db = c.get("db");
	const { tenantId, projectId, agentId, subAgentId } = c.req.valid("param");
	const { page = 1, limit = 10 } = c.req.valid("query");
	const pageNum = Number(page);
	const limitNum = Math.min(Number(limit), 100);
	try {
		const result = await listSubAgentTeamAgentRelations(db)({
			scopes: {
				tenantId,
				projectId,
				agentId,
				subAgentId
			},
			pagination: {
				page: pageNum,
				limit: limitNum
			}
		});
		return c.json(result);
	} catch {
		throw createApiError({
			code: "internal_server_error",
			message: "Failed to retrieve sub agent team agent relations"
		});
	}
});
app.openapi(createProtectedRoute({
	method: "get",
	path: "/{id}",
	summary: "Get Sub Agent Team Agent Relation",
	operationId: "get-sub-agent-team-agent-relation-by-id",
	tags: ["SubAgents"],
	permission: requireProjectPermission("view"),
	request: { params: TenantProjectAgentSubAgentIdParamsSchema },
	responses: {
		200: {
			description: "Sub Agent team agent relation found",
			content: { "application/json": { schema: SubAgentTeamAgentRelationResponse } }
		},
		...commonGetErrorResponses
	}
}), async (c) => {
	const db = c.get("db");
	const { tenantId, projectId, agentId, subAgentId, id } = c.req.valid("param");
	const relation = await getSubAgentTeamAgentRelationById(db)({
		scopes: {
			tenantId,
			projectId,
			agentId,
			subAgentId
		},
		relationId: id
	});
	if (!relation) throw createApiError({
		code: "not_found",
		message: "Sub Agent Team Agent Relation not found"
	});
	return c.json({ data: relation });
});
app.openapi(createProtectedRoute({
	method: "post",
	path: "/",
	summary: "Create Sub Agent Team Agent Relation",
	operationId: "create-sub-agent-team-agent-relation",
	tags: ["SubAgents"],
	permission: requireProjectPermission("edit"),
	request: {
		params: TenantProjectAgentSubAgentParamsSchema,
		body: { content: { "application/json": { schema: SubAgentTeamAgentRelationApiInsertSchema } } }
	},
	responses: {
		201: {
			description: "Sub Agent Team Agent Relation created successfully",
			content: { "application/json": { schema: SubAgentTeamAgentRelationResponse } }
		},
		...commonGetErrorResponses
	}
}), async (c) => {
	const db = c.get("db");
	const { tenantId, projectId, agentId, subAgentId } = c.req.valid("param");
	const body = await c.req.valid("json");
	if ((await listSubAgentTeamAgentRelations(db)({
		scopes: {
			tenantId,
			projectId,
			agentId,
			subAgentId
		},
		pagination: {
			page: 1,
			limit: 1e3
		}
	})).data.some((relation$1) => relation$1.targetAgentId === body.targetAgentId && relation$1.subAgentId === subAgentId)) throw createApiError({
		code: "unprocessable_entity",
		message: `A relation between this sub-agent and team agent already exists`
	});
	const relation = await createSubAgentTeamAgentRelation(db)({
		scopes: {
			tenantId,
			projectId,
			agentId,
			subAgentId
		},
		relationId: deriveRelationId(tenantId, projectId, agentId, subAgentId, body.targetAgentId),
		data: {
			...body,
			headers: body.headers || null
		}
	});
	return c.json({ data: relation }, 201);
});
const updateSubAgentTeamAgentRelationRouteConfig = {
	path: "/{id}",
	summary: "Update Sub Agent Team Agent Relation",
	tags: ["SubAgents"],
	permission: requireProjectPermission("edit"),
	request: {
		params: TenantProjectAgentSubAgentIdParamsSchema,
		body: { content: { "application/json": { schema: SubAgentTeamAgentRelationApiUpdateSchema } } }
	},
	responses: {
		200: {
			description: "Sub Agent team agent relation updated successfully",
			content: { "application/json": { schema: SubAgentTeamAgentRelationResponse } }
		},
		...commonGetErrorResponses
	}
};
const updateSubAgentTeamAgentRelationHandler = async (c) => {
	const db = c.get("db");
	const { tenantId, projectId, agentId, subAgentId, id } = c.req.valid("param");
	const body = await c.req.valid("json");
	const updatedRelation = await updateSubAgentTeamAgentRelation(db)({
		scopes: {
			tenantId,
			projectId,
			agentId,
			subAgentId
		},
		relationId: id,
		data: body
	});
	if (!updatedRelation) throw createApiError({
		code: "not_found",
		message: "Sub Agent Team Agent Relation not found"
	});
	return c.json({ data: updatedRelation });
};
openapiRegisterPutPatchRoutesForLegacy(app, updateSubAgentTeamAgentRelationRouteConfig, updateSubAgentTeamAgentRelationHandler, { operationId: "update-sub-agent-team-agent-relation" });
app.openapi(createProtectedRoute({
	method: "delete",
	path: "/{id}",
	summary: "Delete Sub Agent Team Agent Relation",
	operationId: "delete-sub-agent-team-agent-relation",
	tags: ["SubAgents"],
	permission: requireProjectPermission("edit"),
	request: { params: TenantProjectAgentSubAgentIdParamsSchema },
	responses: {
		204: { description: "Sub Agent Team Agent Relation deleted successfully" },
		404: {
			description: "Sub Agent Team Agent Relation not found",
			content: { "application/json": { schema: ErrorResponseSchema } }
		}
	}
}), async (c) => {
	const db = c.get("db");
	const { tenantId, projectId, agentId, subAgentId, id } = c.req.valid("param");
	if (!await deleteSubAgentTeamAgentRelation(db)({
		scopes: {
			tenantId,
			projectId,
			agentId,
			subAgentId
		},
		relationId: id
	})) throw createApiError({
		code: "not_found",
		message: "Sub Agent Team Agent Relation not found"
	});
	return c.body(null, 204);
});
var subAgentTeamAgentRelations_default = app;

//#endregion
export { subAgentTeamAgentRelations_default as default };