import { db, userNotificationPrefs, eq } from "@epicure/db";

export type NotificationCategory =
  | "follow" | "comment" | "reply" | "reaction" | "rating" | "mention"
  | "leftoverExpiring" | "shoppingList";

export type NotificationPrefs = Record<NotificationCategory, boolean>;

const DEFAULT_PREFS: NotificationPrefs = {
  follow: true, comment: true, reply: true, reaction: true, rating: true, mention: true,
  leftoverExpiring: true, shoppingList: true,
};

export async function getNotificationPrefs(userId: string): Promise<NotificationPrefs> {
  const row = await db.query.userNotificationPrefs.findFirst({ where: eq(userNotificationPrefs.userId, userId) });
  if (!row) return { ...DEFAULT_PREFS };
  return {
    follow: row.follow, comment: row.comment, reply: row.reply, reaction: row.reaction,
    rating: row.rating, mention: row.mention, leftoverExpiring: row.leftoverExpiring, shoppingList: row.shoppingList,
  };
}

/** No row yet means every category defaults to on — same default the DB columns encode. */
export async function isNotificationCategoryEnabled(userId: string, category: NotificationCategory): Promise<boolean> {
  const prefs = await getNotificationPrefs(userId);
  return prefs[category];
}
