import { getLogger as getLogger$1 } from "../../../../logger.js";
import { requireProjectPermission } from "../../../../middleware/projectAccess.js";
import { OpenAPIHono, z } from "@hono/zod-openapi";
import { EvaluatorApiInsertSchema, EvaluatorApiSelectSchema, EvaluatorApiUpdateSchema, ListResponseSchema, SingleResponseSchema, TenantProjectParamsSchema, commonGetErrorResponses, createApiError, createEvaluator, deleteEvaluator, generateId, getEvaluatorById, getEvaluatorsByIds, listEvaluators, listEvaluatorsForAgent, updateEvaluator } from "@inkeep/agents-core";
import { createProtectedRoute } from "@inkeep/agents-core/middleware";

//#region src/domains/manage/routes/evals/evaluators.ts
const app = new OpenAPIHono();
const logger = getLogger$1("evaluators");
app.openapi(createProtectedRoute({
	method: "get",
	path: "/",
	summary: "List Evaluators",
	operationId: "list-evaluators",
	tags: ["Evaluations"],
	permission: requireProjectPermission("view"),
	request: {
		params: TenantProjectParamsSchema,
		query: z.object({ agentId: z.string().optional() })
	},
	responses: {
		200: {
			description: "List of evaluators",
			content: { "application/json": { schema: ListResponseSchema(EvaluatorApiSelectSchema) } }
		},
		...commonGetErrorResponses
	}
}), async (c) => {
	const db = c.get("db");
	const { tenantId, projectId } = c.req.valid("param");
	const { agentId } = c.req.valid("query");
	try {
		const evaluators = agentId ? await listEvaluatorsForAgent(db)({
			scopes: {
				tenantId,
				projectId
			},
			agentId
		}) : await listEvaluators(db)({ scopes: {
			tenantId,
			projectId
		} });
		return c.json({
			data: evaluators,
			pagination: {
				page: 1,
				limit: evaluators.length,
				total: evaluators.length,
				pages: 1
			}
		});
	} catch (error) {
		logger.error({
			error,
			tenantId,
			projectId
		}, "Failed to list evaluators");
		return c.json(createApiError({
			code: "internal_server_error",
			message: "Failed to list evaluators"
		}), 500);
	}
});
app.openapi(createProtectedRoute({
	method: "get",
	path: "/{evaluatorId}",
	summary: "Get Evaluator by ID",
	operationId: "get-evaluator",
	tags: ["Evaluations"],
	permission: requireProjectPermission("view"),
	request: { params: TenantProjectParamsSchema.extend({ evaluatorId: z.string() }) },
	responses: {
		200: {
			description: "Evaluator details",
			content: { "application/json": { schema: SingleResponseSchema(EvaluatorApiSelectSchema) } }
		},
		...commonGetErrorResponses
	}
}), async (c) => {
	const db = c.get("db");
	const { tenantId, projectId, evaluatorId } = c.req.valid("param");
	try {
		const evaluator = await getEvaluatorById(db)({ scopes: {
			tenantId,
			projectId,
			evaluatorId
		} });
		if (!evaluator) return c.json(createApiError({
			code: "not_found",
			message: "Evaluator not found"
		}), 404);
		return c.json({ data: evaluator });
	} catch (error) {
		logger.error({
			error,
			tenantId,
			projectId,
			evaluatorId
		}, "Failed to get evaluator");
		return c.json(createApiError({
			code: "internal_server_error",
			message: "Failed to get evaluator"
		}), 500);
	}
});
app.openapi(createProtectedRoute({
	method: "post",
	path: "/batch",
	summary: "Get Evaluators by IDs",
	operationId: "get-evaluators-batch",
	tags: ["Evaluations"],
	permission: requireProjectPermission("view"),
	request: {
		params: TenantProjectParamsSchema,
		body: { content: { "application/json": { schema: z.object({ evaluatorIds: z.array(z.string()).min(1, "At least one evaluator ID is required") }) } } }
	},
	responses: {
		200: {
			description: "List of evaluators",
			content: { "application/json": { schema: z.object({ data: z.array(EvaluatorApiSelectSchema) }) } }
		},
		...commonGetErrorResponses
	}
}), async (c) => {
	const db = c.get("db");
	const { tenantId, projectId } = c.req.valid("param");
	const { evaluatorIds } = c.req.valid("json");
	try {
		const evaluators = await getEvaluatorsByIds(db)({
			scopes: {
				tenantId,
				projectId
			},
			evaluatorIds
		});
		return c.json({ data: evaluators });
	} catch (error) {
		logger.error({
			error,
			tenantId,
			projectId,
			evaluatorIds
		}, "Failed to get evaluators batch");
		return c.json(createApiError({
			code: "internal_server_error",
			message: "Failed to get evaluators"
		}), 500);
	}
});
app.openapi(createProtectedRoute({
	method: "post",
	path: "/",
	summary: "Create Evaluator",
	operationId: "create-evaluator",
	tags: ["Evaluations"],
	permission: requireProjectPermission("edit"),
	request: {
		params: TenantProjectParamsSchema,
		body: { content: { "application/json": { schema: EvaluatorApiInsertSchema } } }
	},
	responses: {
		201: {
			description: "Evaluator created",
			content: { "application/json": { schema: SingleResponseSchema(EvaluatorApiSelectSchema) } }
		},
		...commonGetErrorResponses
	}
}), async (c) => {
	const db = c.get("db");
	const { tenantId, projectId } = c.req.valid("param");
	const evaluatorData = c.req.valid("json");
	try {
		const id = evaluatorData.id || generateId();
		const created = await createEvaluator(db)({
			...evaluatorData,
			id,
			tenantId,
			projectId
		});
		logger.info({
			tenantId,
			projectId,
			evaluatorId: id
		}, "Evaluator created");
		return c.json({ data: created }, 201);
	} catch (error) {
		logger.error({
			error,
			tenantId,
			projectId,
			evaluatorData
		}, "Failed to create evaluator");
		return c.json(createApiError({
			code: "internal_server_error",
			message: "Failed to create evaluator"
		}), 500);
	}
});
app.openapi(createProtectedRoute({
	method: "patch",
	path: "/{evaluatorId}",
	summary: "Update Evaluator",
	operationId: "update-evaluator",
	tags: ["Evaluations"],
	permission: requireProjectPermission("edit"),
	request: {
		params: TenantProjectParamsSchema.extend({ evaluatorId: z.string() }),
		body: { content: { "application/json": { schema: EvaluatorApiUpdateSchema } } }
	},
	responses: {
		200: {
			description: "Evaluator updated",
			content: { "application/json": { schema: SingleResponseSchema(EvaluatorApiSelectSchema) } }
		},
		...commonGetErrorResponses
	}
}), async (c) => {
	const db = c.get("db");
	const { tenantId, projectId, evaluatorId } = c.req.valid("param");
	const updateData = c.req.valid("json");
	try {
		const updated = await updateEvaluator(db)({
			scopes: {
				tenantId,
				projectId,
				evaluatorId
			},
			data: updateData
		});
		if (!updated) return c.json(createApiError({
			code: "not_found",
			message: "Evaluator not found"
		}), 404);
		logger.info({
			tenantId,
			projectId,
			evaluatorId
		}, "Evaluator updated");
		return c.json({ data: updated });
	} catch (error) {
		logger.error({
			error,
			tenantId,
			projectId,
			evaluatorId
		}, "Failed to update evaluator");
		return c.json(createApiError({
			code: "internal_server_error",
			message: "Failed to update evaluator"
		}), 500);
	}
});
app.openapi(createProtectedRoute({
	method: "delete",
	path: "/{evaluatorId}",
	summary: "Delete Evaluator",
	operationId: "delete-evaluator",
	tags: ["Evaluations"],
	permission: requireProjectPermission("edit"),
	request: { params: TenantProjectParamsSchema.extend({ evaluatorId: z.string() }) },
	responses: {
		204: { description: "Evaluator deleted" },
		...commonGetErrorResponses
	}
}), async (c) => {
	const db = c.get("db");
	const { tenantId, projectId, evaluatorId } = c.req.valid("param");
	try {
		if (!await deleteEvaluator(db)({ scopes: {
			tenantId,
			projectId,
			evaluatorId
		} })) return c.json(createApiError({
			code: "not_found",
			message: "Evaluator not found"
		}), 404);
		logger.info({
			tenantId,
			projectId,
			evaluatorId
		}, "Evaluator deleted");
		return c.body(null, 204);
	} catch (error) {
		logger.error({
			error,
			tenantId,
			projectId,
			evaluatorId
		}, "Failed to delete evaluator");
		return c.json(createApiError({
			code: "internal_server_error",
			message: "Failed to delete evaluator"
		}), 500);
	}
});
var evaluators_default = app;

//#endregion
export { evaluators_default as default };