import { prisma } from "../prisma/db";

export async function updateUserFav(logto_uid: string, kaiqiu_uid: string) {
  const size = await prisma.logtoUserFav.count({ where: { logto_uid, kaiqiu_uid }});
  const isUnFav = size === 0;
  if (isUnFav) {
    await prisma.logtoUserFav.create({
      data: {
        logto_uid,
        kaiqiu_uid,
      },
    });
  }
}

export async function getUserFavList(logto_uid: string) {
  const result = await prisma.logtoUserFav.findMany({
    where: { logto_uid },
    select: { kaiqiu_uid: true },
  });
  return result.map(e => e.kaiqiu_uid);
}

export async function deleteUserFav(logto_uid: string, kaiqiu_uid: string) {
  await prisma.logtoUserFav.deleteMany({ where: { logto_uid, kaiqiu_uid }});
}

export async function isUserFav(logto_uid: string, kaiqiu_uid: string) {
  return (await prisma.logtoUserFav.count({ where: { kaiqiu_uid, logto_uid }})) > 0;
}
