import { enqueueVoiceExperience } from '../hatchet/enqueue-voice-experience.js';
import { prisma } from '../prisma.js';

const minimumVoiceExperienceSeconds = 30;

export type CreateVoiceExperienceInput = {
  googlePlaceId: string;
  googleName: string;
  latitude: number;
  longitude: number;
  durationSeconds: number;
  audioObjectKey: string;
};

export async function createVoiceExperience(input: CreateVoiceExperienceInput) {
  if (input.durationSeconds < minimumVoiceExperienceSeconds) {
    throw new Error('Voice experience must be at least 30 seconds.');
  }

  const place = await prisma.place.upsert({
    where: { googlePlaceId: input.googlePlaceId },
    create: {
      googlePlaceId: input.googlePlaceId,
      name: input.googleName,
      latitude: input.latitude,
      longitude: input.longitude,
    },
    update: {
      name: input.googleName,
      latitude: input.latitude,
      longitude: input.longitude,
    },
  });

  const experience = await prisma.voiceExperience.create({
    data: {
      placeId: place.id,
      durationSeconds: input.durationSeconds,
      audioObjectKey: input.audioObjectKey,
      status: 'UPLOADED',
    },
    include: { place: true },
  });

  await enqueueVoiceExperience({ experienceId: experience.id });

  return experience;
}
