import { internal } from '../../../../_generated/api';
import type { Id } from '../../../../_generated/dataModel';
import type { ActionCtx } from '../../../../_generated/server';
import type { ConvexJsonRecord } from '../../../../lib/validators/json';
import type { TableName, RecordProcessedResult } from './types';

export async function recordProcessed(
  ctx: ActionCtx,
  params: {
    organizationId: string;
    tableName: TableName;
    recordId: string;
    wfDefinitionId: string;
    metadata?: ConvexJsonRecord;
  },
): Promise<RecordProcessedResult> {
  const processingRecordId: Id<'workflowProcessingRecords'> =
    await ctx.runMutation(
      internal.workflows.processing_records.internal_mutations.recordProcessed,
      {
        organizationId: params.organizationId,
        tableName: params.tableName,
        recordId: params.recordId,
        wfDefinitionId: params.wfDefinitionId,
        recordCreationTime: Date.now(), // Auto-populated
        metadata: params.metadata,
      },
    );

  // Fetch and return the full created entity
  // Note: execute_action_node wraps this in output: { type: 'action', data: result }
  const createdRecord = await ctx.runQuery(
    internal.workflows.processing_records.internal_queries
      .getProcessingRecordById,
    { processingRecordId },
  );

  if (!createdRecord) {
    throw new Error(
      `Failed to retrieve processing record with ID "${processingRecordId}" after creation`,
    );
  }

  return createdRecord;
}
