import { prisma } from "@/lib/prisma";

export const reviewRepo = {
  async upsert(data: {
    tripId: string;
    userId: string;
    rating: number;
    comment: string | null;
  }) {
    return prisma.tripReview.upsert({
      where: {
        tripId_userId: { tripId: data.tripId, userId: data.userId },
      },
      create: {
        tripId: data.tripId,
        userId: data.userId,
        rating: data.rating,
        comment: data.comment,
      },
      update: {
        rating: data.rating,
        comment: data.comment,
      },
    });
  },

  /**
   * Semua review untuk trip yang di-host organizer ini, terbaru di depan.
   * Dipakai di profil publik organizer untuk menampilkan testimoni.
   */
  async findByOrganizer(organizerId: string, limit = 20) {
    return prisma.tripReview.findMany({
      where: { trip: { organizerId } },
      orderBy: { createdAt: "desc" },
      take: limit,
      select: {
        id: true,
        rating: true,
        comment: true,
        createdAt: true,
        user: { select: { id: true, name: true, image: true } },
        trip: { select: { id: true, title: true, destination: true } },
      },
    });
  },
};
