import { getLogger as getLogger$1 } from "../../../../logger.js";
import { requireProjectPermission } from "../../../../middleware/projectAccess.js";
import { OpenAPIHono, z } from "@hono/zod-openapi";
import { AgentEvaluatorRelationApiSelectSchema, ListResponseSchema, SingleResponseSchema, TenantProjectParamsSchema, commonGetErrorResponses, createAgentEvaluatorRelation, createApiError, deleteAgentEvaluatorRelation, generateId, getAgentEvaluatorRelationsByEvaluator, getAgentIdsForEvaluators, isUniqueConstraintError } from "@inkeep/agents-core";
import { createProtectedRoute } from "@inkeep/agents-core/middleware";

//#region src/domains/manage/routes/evals/agentEvaluatorRelations.ts
const app = new OpenAPIHono();
const logger = getLogger$1("agentEvaluatorRelations");
app.openapi(createProtectedRoute({
	method: "post",
	path: "/batch-agent-scopes",
	summary: "Batch get agent scopes for evaluators",
	operationId: "batch-get-evaluator-agent-scopes",
	tags: ["Evaluations"],
	permission: requireProjectPermission("view"),
	request: {
		params: TenantProjectParamsSchema,
		body: { content: { "application/json": { schema: z.object({ evaluatorIds: z.array(z.string()).min(1).max(200) }) } } }
	},
	responses: {
		200: {
			description: "Map of evaluator ID to array of scoped agent IDs",
			content: { "application/json": { schema: z.object({ data: z.record(z.string(), z.array(z.string())) }) } }
		},
		...commonGetErrorResponses
	}
}), async (c) => {
	const db = c.get("db");
	const { tenantId, projectId } = c.req.valid("param");
	const { evaluatorIds } = c.req.valid("json");
	try {
		const resultMap = await getAgentIdsForEvaluators(db)({
			scopes: {
				tenantId,
				projectId
			},
			evaluatorIds
		});
		const data = {};
		for (const [key, value] of resultMap) data[key] = value;
		return c.json({ data });
	} catch (error) {
		logger.error({
			error,
			tenantId,
			projectId,
			evaluatorIds
		}, "Failed to batch get evaluator agent scopes");
		return c.json(createApiError({
			code: "internal_server_error",
			message: "Failed to batch get evaluator agent scopes"
		}), 500);
	}
});
app.openapi(createProtectedRoute({
	method: "get",
	path: "/{evaluatorId}/agents",
	summary: "List Agents for Evaluator",
	operationId: "list-evaluator-agents",
	tags: ["Evaluations"],
	permission: requireProjectPermission("view"),
	request: { params: TenantProjectParamsSchema.extend({ evaluatorId: z.string() }) },
	responses: {
		200: {
			description: "List of agent relations for this evaluator",
			content: { "application/json": { schema: ListResponseSchema(AgentEvaluatorRelationApiSelectSchema) } }
		},
		...commonGetErrorResponses
	}
}), async (c) => {
	const db = c.get("db");
	const { tenantId, projectId, evaluatorId } = c.req.valid("param");
	try {
		const relations = await getAgentEvaluatorRelationsByEvaluator(db)({ scopes: {
			tenantId,
			projectId,
			evaluatorId
		} });
		return c.json({ data: relations });
	} catch (error) {
		logger.error({
			error,
			tenantId,
			projectId,
			evaluatorId
		}, "Failed to list evaluator agents");
		return c.json(createApiError({
			code: "internal_server_error",
			message: "Failed to list evaluator agents"
		}), 500);
	}
});
app.openapi(createProtectedRoute({
	method: "post",
	path: "/{evaluatorId}/agents/{agentId}",
	summary: "Add Agent to Evaluator",
	operationId: "add-agent-to-evaluator",
	tags: ["Evaluations"],
	permission: requireProjectPermission("edit"),
	request: { params: TenantProjectParamsSchema.extend({
		evaluatorId: z.string(),
		agentId: z.string()
	}) },
	responses: {
		201: {
			description: "Agent relation created",
			content: { "application/json": { schema: SingleResponseSchema(AgentEvaluatorRelationApiSelectSchema) } }
		},
		...commonGetErrorResponses
	}
}), async (c) => {
	const db = c.get("db");
	const { tenantId, projectId, evaluatorId, agentId } = c.req.valid("param");
	try {
		const id = generateId();
		const created = await createAgentEvaluatorRelation(db)({
			id,
			tenantId,
			projectId,
			evaluatorId,
			agentId
		});
		logger.info({
			tenantId,
			projectId,
			evaluatorId,
			agentId
		}, "Agent-evaluator relation created");
		return c.json({ data: created }, 201);
	} catch (error) {
		if (isUniqueConstraintError(error)) {
			const match = (await getAgentEvaluatorRelationsByEvaluator(db)({ scopes: {
				tenantId,
				projectId,
				evaluatorId
			} })).find((r) => r.agentId === agentId);
			if (match) return c.json({ data: match }, 200);
		}
		logger.error({
			error,
			tenantId,
			projectId,
			evaluatorId,
			agentId
		}, "Failed to create agent-evaluator relation");
		return c.json(createApiError({
			code: "internal_server_error",
			message: "Failed to add agent to evaluator"
		}), 500);
	}
});
app.openapi(createProtectedRoute({
	method: "delete",
	path: "/{evaluatorId}/agents/{agentId}",
	summary: "Remove Agent from Evaluator",
	operationId: "remove-agent-from-evaluator",
	tags: ["Evaluations"],
	permission: requireProjectPermission("edit"),
	request: { params: TenantProjectParamsSchema.extend({
		evaluatorId: z.string(),
		agentId: z.string()
	}) },
	responses: {
		204: { description: "Agent relation deleted" },
		...commonGetErrorResponses
	}
}), async (c) => {
	const db = c.get("db");
	const { tenantId, projectId, evaluatorId, agentId } = c.req.valid("param");
	try {
		if (!await deleteAgentEvaluatorRelation(db)({ scopes: {
			tenantId,
			projectId,
			agentId,
			evaluatorId
		} })) return c.json(createApiError({
			code: "not_found",
			message: "Agent-evaluator relation not found"
		}), 404);
		logger.info({
			tenantId,
			projectId,
			evaluatorId,
			agentId
		}, "Agent-evaluator relation deleted");
		return c.body(null, 204);
	} catch (error) {
		logger.error({
			error,
			tenantId,
			projectId,
			evaluatorId,
			agentId
		}, "Failed to delete agent-evaluator relation");
		return c.json(createApiError({
			code: "internal_server_error",
			message: "Failed to remove agent from evaluator"
		}), 500);
	}
});
var agentEvaluatorRelations_default = app;

//#endregion
export { agentEvaluatorRelations_default as default };