import { prisma } from '../prisma.js';

function serializeVoiceExperience(
  experience: Awaited<ReturnType<typeof prisma.voiceExperience.findMany>>[number],
) {
  return {
    ...experience,
    createdAt: experience.createdAt.toISOString(),
  };
}

export async function listPlaces() {
  const places = await prisma.place.findMany({
    include: {
      experiences: {
        orderBy: { createdAt: 'desc' },
        take: 10,
        include: { user: true },
      },
    },
    orderBy: { updatedAt: 'desc' },
  });

  return places.map((place) => ({
    ...place,
    experiences: place.experiences.map(serializeVoiceExperience),
  }));
}

export async function listVoiceExperiences() {
  const experiences = await prisma.voiceExperience.findMany({
    include: { place: true, user: true },
    orderBy: { createdAt: 'desc' },
    take: 100,
  });

  return experiences.map(serializeVoiceExperience);
}
