import { buildPlaceAnalysis } from '../ontology/place-ontology.js';
import { prisma } from '../prisma.js';

export async function analyzeVoiceExperience(experienceId: string) {
  const experience = await prisma.voiceExperience.findUnique({
    where: { id: experienceId },
    include: { place: true },
  });

  if (!experience) {
    throw new Error(`Voice experience ${experienceId} was not found.`);
  }

  if (!experience.transcript) {
    throw new Error(`Voice experience ${experienceId} has no transcript.`);
  }

  await prisma.voiceExperience.update({
    where: { id: experienceId },
    data: { status: 'ANALYZING' },
  });

  const analysis = buildPlaceAnalysis({
    placeName: experience.place.name,
    transcript: experience.transcript,
  });

  await prisma.voiceExperience.update({
    where: { id: experienceId },
    data: {
      analysis,
      status: 'ANALYZED',
    },
  });

  return { tags: analysis.tags };
}
