import { getLogger as getLogger$1 } from "../../../../logger.js";
import { requireProjectPermission } from "../../../../middleware/projectAccess.js";
import { OpenAPIHono, createRoute, z } from "@hono/zod-openapi";
import { TenantProjectParamsSchema, commonGetErrorResponses, createApiError, createEvaluationJobConfigEvaluatorRelation, deleteEvaluationJobConfigEvaluatorRelation, generateId, getEvaluationJobConfigEvaluatorRelations } from "@inkeep/agents-core";

//#region src/domains/manage/routes/evals/evaluationJobConfigEvaluatorRelations.ts
const app = new OpenAPIHono();
const logger = getLogger$1("evaluationJobConfigEvaluatorRelations");
app.use("/:configId/evaluators/:evaluatorId", async (c, next) => {
	if (["POST", "DELETE"].includes(c.req.method)) return requireProjectPermission("edit")(c, next);
	return next();
});
app.openapi(createRoute({
	method: "get",
	path: "/{configId}/evaluators",
	summary: "List Evaluators for Evaluation Job Config",
	operationId: "list-evaluation-job-config-evaluators",
	tags: ["Evaluations"],
	request: { params: TenantProjectParamsSchema.extend({ configId: z.string() }) },
	responses: {
		200: {
			description: "List of evaluator relations",
			content: { "application/json": { schema: z.array(z.any()) } }
		},
		...commonGetErrorResponses
	}
}), async (c) => {
	const db = c.get("db");
	const { tenantId, projectId, configId } = c.req.valid("param");
	try {
		const relations = await getEvaluationJobConfigEvaluatorRelations(db)({ scopes: {
			tenantId,
			projectId,
			evaluationJobConfigId: configId
		} });
		return c.json({ data: relations });
	} catch (error) {
		logger.error({
			error,
			tenantId,
			projectId,
			configId
		}, "Failed to list evaluator relations");
		return c.json(createApiError({
			code: "internal_server_error",
			message: "Failed to list evaluator relations"
		}), 500);
	}
});
app.openapi(createRoute({
	method: "post",
	path: "/{configId}/evaluators/{evaluatorId}",
	summary: "Add Evaluator to Evaluation Job Config",
	operationId: "add-evaluator-to-job-config",
	tags: ["Evaluations"],
	request: { params: TenantProjectParamsSchema.extend({
		configId: z.string(),
		evaluatorId: z.string()
	}) },
	responses: {
		201: {
			description: "Evaluator relation created",
			content: { "application/json": { schema: z.any() } }
		},
		...commonGetErrorResponses
	}
}), async (c) => {
	const db = c.get("db");
	const { tenantId, projectId, configId, evaluatorId } = c.req.valid("param");
	try {
		const id = generateId();
		const created = await createEvaluationJobConfigEvaluatorRelation(db)({
			id,
			tenantId,
			projectId,
			evaluationJobConfigId: configId,
			evaluatorId
		});
		logger.info({
			tenantId,
			projectId,
			configId,
			evaluatorId
		}, "Evaluator relation created");
		return c.json({ data: created }, 201);
	} catch (error) {
		logger.error({
			error,
			tenantId,
			projectId,
			configId,
			evaluatorId
		}, "Failed to create evaluator relation");
		return c.json(createApiError({
			code: "internal_server_error",
			message: "Failed to create evaluator relation"
		}), 500);
	}
});
app.openapi(createRoute({
	method: "delete",
	path: "/{configId}/evaluators/{evaluatorId}",
	summary: "Remove Evaluator from Evaluation Job Config",
	operationId: "remove-evaluator-from-job-config",
	tags: ["Evaluations"],
	request: { params: TenantProjectParamsSchema.extend({
		configId: z.string(),
		evaluatorId: z.string()
	}) },
	responses: {
		204: { description: "Evaluator relation deleted" },
		...commonGetErrorResponses
	}
}), async (c) => {
	const db = c.get("db");
	const { tenantId, projectId, configId, evaluatorId } = c.req.valid("param");
	try {
		if (!await deleteEvaluationJobConfigEvaluatorRelation(db)({ scopes: {
			tenantId,
			projectId,
			evaluationJobConfigId: configId,
			evaluatorId
		} })) return c.json(createApiError({
			code: "not_found",
			message: "Evaluator relation not found"
		}), 404);
		logger.info({
			tenantId,
			projectId,
			configId,
			evaluatorId
		}, "Evaluator relation deleted");
		return c.body(null, 204);
	} catch (error) {
		logger.error({
			error,
			tenantId,
			projectId,
			configId,
			evaluatorId
		}, "Failed to delete evaluator relation");
		return c.json(createApiError({
			code: "internal_server_error",
			message: "Failed to delete evaluator relation"
		}), 500);
	}
});
var evaluationJobConfigEvaluatorRelations_default = app;

//#endregion
export { evaluationJobConfigEvaluatorRelations_default as default };