import runDbClient_default from "../../../data/db/runDbClient.js";
import { getLogger as getLogger$1 } from "../../../logger.js";
import { evaluateConversationWorkflow } from "../workflow/functions/evaluateConversation.js";
import "../workflow/index.js";
import { createEvaluationRun, filterConversationsForJob, generateId } from "@inkeep/agents-core";
import { start } from "workflow/api";

//#region src/domains/evals/services/evaluationJob.ts
const logger = getLogger$1("evaluation-job");
async function queueEvaluationJobConversations(params) {
	const { tenantId, projectId, evaluationJobConfigId, evaluatorIds, jobFilters, resolvedRef } = params;
	const conversations = await filterConversationsForJob(runDbClient_default)({
		scopes: {
			tenantId,
			projectId
		},
		jobFilters
	});
	if (conversations.length === 0) {
		logger.warn({
			tenantId,
			projectId,
			evaluationJobConfigId
		}, "No conversations found for job");
		return {
			conversationCount: 0,
			queued: 0,
			failed: 0,
			evaluationRunId: ""
		};
	}
	const evaluationRun = await createEvaluationRun(runDbClient_default)({
		id: generateId(),
		tenantId,
		projectId,
		evaluationJobConfigId,
		ref: resolvedRef
	});
	let queued = 0;
	let failed = 0;
	for (const conv of conversations) try {
		await start(evaluateConversationWorkflow, [{
			tenantId,
			projectId,
			conversationId: conv.id,
			evaluatorIds,
			evaluationRunId: evaluationRun.id
		}]);
		queued++;
	} catch (err) {
		logger.error({
			err,
			conversationId: conv.id
		}, "Failed to queue conversation evaluation");
		failed++;
	}
	return {
		conversationCount: conversations.length,
		queued,
		failed,
		evaluationRunId: evaluationRun.id
	};
}

//#endregion
export { queueEvaluationJobConversations };