import { analyzeVoiceExperience } from '../../jobs/analyze-voice-experience.js';
import { markVoiceExperienceFailed } from '../../jobs/mark-voice-experience-failed.js';
import { transcribeVoiceExperience } from '../../jobs/transcribe-voice-experience.js';
import { hatchet } from '../hatchet-client.js';
import { workflowNames } from '../workflow-names.js';

export type ProcessVoiceExperienceInput = {
  experienceId: string;
};

type ProcessVoiceExperienceOutput = {
  transcribe_voice_experience: {
    transcript: string;
  };
  analyze_voice_experience_with_ontology: {
    tags: string[];
  };
};

export const processVoiceExperienceWorkflow = hatchet.workflow<
  ProcessVoiceExperienceInput,
  ProcessVoiceExperienceOutput
>({
  name: workflowNames.processVoiceExperience,
});

const transcribeTask = processVoiceExperienceWorkflow.task({
  name: 'transcribe_voice_experience',
  fn: async (input) => transcribeVoiceExperience(input.experienceId),
});

processVoiceExperienceWorkflow.task({
  name: 'analyze_voice_experience_with_ontology',
  parents: [transcribeTask],
  fn: async (input) => analyzeVoiceExperience(input.experienceId),
});

processVoiceExperienceWorkflow.onFailure({
  fn: async (input) => markVoiceExperienceFailed(input.experienceId),
});
