import { getLogger } from "../../../logger.js";
import { runDatasetItemWorkflow } from "../workflow/functions/runDatasetItem.js";
import { start } from "workflow/api";

//#region src/domains/evals/services/datasetRun.ts
async function queueDatasetRunItems(params) {
	const { tenantId, projectId, datasetRunId, items, evaluatorIds, evaluationRunId } = params;
	const logger = getLogger("workflow-triggers");
	let queued = 0;
	let failed = 0;
	for (const item of items) {
		const payload = {
			tenantId,
			projectId,
			agentId: item.agentId,
			datasetItemId: item.id ?? "",
			datasetItemInput: item.input,
			datasetItemExpectedOutput: item.expectedOutput,
			datasetItemSimulationAgent: item.simulationAgent,
			datasetRunId,
			evaluatorIds,
			evaluationRunId
		};
		try {
			await start(runDatasetItemWorkflow, [payload]);
			queued++;
		} catch (err) {
			logger.error({
				err,
				datasetItemId: item.id,
				agentId: item.agentId
			}, "Failed to queue dataset item workflow");
			failed++;
		}
	}
	return {
		queued,
		failed
	};
}

//#endregion
export { queueDatasetRunItems };