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

export type FeatureKey = "nutrition" | "pantry" | "mealPlan" | "shoppingLists" | "collections" | "messages" | "chatbots";

export type FeaturePrefs = Record<FeatureKey, boolean>;

const DEFAULT_PREFS: FeaturePrefs = {
  nutrition: true, pantry: true, mealPlan: true, shoppingLists: true, collections: true, messages: true, chatbots: true,
};

/** No row yet means every feature defaults to on — same default the DB columns encode. */
export async function getFeaturePrefs(userId: string): Promise<FeaturePrefs> {
  const row = await db.query.userFeaturePrefs.findFirst({ where: eq(userFeaturePrefs.userId, userId) });
  if (!row) return { ...DEFAULT_PREFS };
  return {
    nutrition: row.nutrition, pantry: row.pantry, mealPlan: row.mealPlan,
    shoppingLists: row.shoppingLists, collections: row.collections, messages: row.messages,
    chatbots: row.chatbots,
  };
}
