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, ExternalAgentApiInsertSchema, ExternalAgentApiUpdateSchema, ExternalAgentListResponse, ExternalAgentResponse, PaginationQueryParamsSchema, TenantProjectIdParamsSchema, TenantProjectParamsSchema, commonGetErrorResponses, createApiError, createExternalAgent, deleteExternalAgent, generateId, getExternalAgent, listExternalAgentsPaginated, updateExternalAgent } from "@inkeep/agents-core";
import { createProtectedRoute } from "@inkeep/agents-core/middleware";

//#region src/domains/manage/routes/externalAgents.ts
const app = new OpenAPIHono();
app.openapi(createProtectedRoute({
	method: "get",
	path: "/",
	summary: "List External Agents",
	operationId: "list-external-agents",
	tags: ["External Agents"],
	permission: requireProjectPermission("view"),
	request: {
		params: TenantProjectParamsSchema,
		query: PaginationQueryParamsSchema
	},
	responses: {
		200: {
			description: "List of external agents retrieved successfully",
			content: { "application/json": { schema: ExternalAgentListResponse } }
		},
		...commonGetErrorResponses
	},
	...speakeasyOffsetLimitPagination
}), async (c) => {
	const db = c.get("db");
	const { tenantId, projectId } = c.req.valid("param");
	const { page, limit } = c.req.valid("query");
	const result = await listExternalAgentsPaginated(db)({
		scopes: {
			tenantId,
			projectId
		},
		pagination: {
			page,
			limit
		}
	});
	const dataWithType = {
		...result,
		data: result.data.map((agent) => ({
			...agent,
			type: "external"
		}))
	};
	return c.json(dataWithType);
});
app.openapi(createProtectedRoute({
	method: "get",
	path: "/{id}",
	summary: "Get External Agent",
	operationId: "get-external-agent-by-id",
	tags: ["External Agents"],
	permission: requireProjectPermission("view"),
	request: { params: TenantProjectIdParamsSchema },
	responses: {
		200: {
			description: "External agent found",
			content: { "application/json": { schema: ExternalAgentResponse } }
		},
		...commonGetErrorResponses
	}
}), async (c) => {
	const db = c.get("db");
	const { tenantId, projectId, id } = c.req.valid("param");
	const externalAgent = await getExternalAgent(db)({
		scopes: {
			tenantId,
			projectId
		},
		externalAgentId: id
	});
	if (!externalAgent) throw createApiError({
		code: "not_found",
		message: "External agent not found"
	});
	const agentWithType = {
		...externalAgent,
		type: "external"
	};
	return c.json({ data: agentWithType });
});
app.openapi(createProtectedRoute({
	method: "post",
	path: "/",
	summary: "Create External Agent",
	operationId: "create-external-agent",
	tags: ["External Agents"],
	permission: requireProjectPermission("edit"),
	request: {
		params: TenantProjectParamsSchema,
		body: { content: { "application/json": { schema: ExternalAgentApiInsertSchema } } }
	},
	responses: {
		201: {
			description: "External agent created successfully",
			content: { "application/json": { schema: ExternalAgentResponse } }
		},
		...commonGetErrorResponses
	}
}), async (c) => {
	const db = c.get("db");
	const { tenantId, projectId } = c.req.valid("param");
	const body = c.req.valid("json");
	const externalAgentData = {
		...body,
		tenantId,
		projectId,
		id: body.id ? String(body.id) : generateId(),
		credentialReferenceId: body.credentialReferenceId || void 0
	};
	const agentWithType = {
		...await createExternalAgent(db)(externalAgentData),
		type: "external"
	};
	return c.json({ data: agentWithType }, 201);
});
const updateExternalAgentRouteConfig = {
	path: "/{id}",
	summary: "Update External Agent",
	tags: ["External Agents"],
	permission: requireProjectPermission("edit"),
	request: {
		params: TenantProjectIdParamsSchema,
		body: { content: { "application/json": { schema: ExternalAgentApiUpdateSchema } } }
	},
	responses: {
		200: {
			description: "External agent updated successfully",
			content: { "application/json": { schema: ExternalAgentResponse } }
		},
		...commonGetErrorResponses
	}
};
const updateExternalAgentHandler = async (c) => {
	const db = c.get("db");
	const { tenantId, projectId, id } = c.req.valid("param");
	const body = c.req.valid("json");
	const updatedExternalAgent = await updateExternalAgent(db)({
		scopes: {
			tenantId,
			projectId
		},
		externalAgentId: id,
		data: body
	});
	if (!updatedExternalAgent) throw createApiError({
		code: "not_found",
		message: "External agent not found"
	});
	const agentWithType = {
		...updatedExternalAgent,
		type: "external"
	};
	return c.json({ data: agentWithType });
};
openapiRegisterPutPatchRoutesForLegacy(app, updateExternalAgentRouteConfig, updateExternalAgentHandler, { operationId: "update-external-agent" });
app.openapi(createProtectedRoute({
	method: "delete",
	path: "/{id}",
	summary: "Delete External Agent",
	operationId: "delete-external-agent",
	tags: ["External Agents"],
	permission: requireProjectPermission("edit"),
	request: { params: TenantProjectIdParamsSchema },
	responses: {
		204: { description: "External agent deleted successfully" },
		404: {
			description: "External agent not found",
			content: { "application/json": { schema: ErrorResponseSchema } }
		}
	}
}), async (c) => {
	const db = c.get("db");
	const { tenantId, projectId, id } = c.req.valid("param");
	if (!await deleteExternalAgent(db)({
		scopes: {
			tenantId,
			projectId
		},
		externalAgentId: id
	})) throw createApiError({
		code: "not_found",
		message: "External agent not found"
	});
	return c.body(null, 204);
});
var externalAgents_default = app;

//#endregion
export { externalAgents_default as default };