export type ModuleVisibility = { guests: boolean; loggedIn: boolean };
export interface LayoutModule {
  instanceId: string;
  type: string;
  config: Record<string, unknown>;
  visibility: ModuleVisibility;
}

const v = { guests: true, loggedIn: true } satisfies ModuleVisibility;

// Stable instanceIds — shared mobile/desktop, deterministic across migrations
const ID = {
  forYou: '00000000-0000-4000-8000-000000000001',
  nearYou: '00000000-0000-4000-8000-000000000002',
  hotDeals: '00000000-0000-4000-8000-000000000003',
  groupDeals: '00000000-0000-4000-8000-000000000004',
  whatsLeft: '00000000-0000-4000-8000-000000000005',
  catMeal: '00000000-0000-4000-8000-000000000010',
  catBakery: '00000000-0000-4000-8000-000000000011',
  catBeverage: '00000000-0000-4000-8000-000000000012',
  catDessert: '00000000-0000-4000-8000-000000000013',
  catPackaged: '00000000-0000-4000-8000-000000000014',
} as const;

// Actual deal_categories PKs + slugs (seeded via db:seed).
// Baked here so deal-row:category modules skip the resolveSlug DB call on cold SSR.
const CAT = {
  meal: { id: '00000000-0000-0000-0000-000000000801', slug: 'meal' },
  bakery: { id: '00000000-0000-0000-0000-000000000802', slug: 'bakery' },
  beverage: { id: '00000000-0000-0000-0000-000000000803', slug: 'beverage' },
  dessert: { id: '00000000-0000-0000-0000-000000000804', slug: 'dessert' },
  packaged: { id: '00000000-0000-0000-0000-000000000805', slug: 'packaged' },
} as const;

export const HOME_DEFAULT_LAYOUT: { mobile: LayoutModule[]; desktop: LayoutModule[] } = (() => {
  const shared: LayoutModule[] = [
    {
      instanceId: ID.forYou,
      type: 'deal-row:for-you',
      config: { title: { he: 'בשבילך', en: 'For You' }, limit: 10 },
      visibility: v,
    },
    {
      instanceId: ID.nearYou,
      type: 'deal-row:near-you',
      config: { title: { he: 'לידך', en: 'Near You' }, limit: 10 },
      visibility: v,
    },
    {
      instanceId: ID.hotDeals,
      type: 'deal-row:hot-deals',
      config: { title: { he: 'עסקאות חמות', en: 'Hot Deals' }, limit: 10 },
      visibility: v,
    },
    {
      instanceId: ID.groupDeals,
      type: 'deal-row:group-deals',
      config: { title: { he: 'עסקאות קבוצתיות', en: 'Group Deals' }, limit: 10 },
      visibility: v,
    },
    {
      instanceId: ID.whatsLeft,
      type: 'deal-row:whats-left',
      config: { title: { he: 'מה שנשאר', en: "What's Left" }, limit: 10 },
      visibility: v,
    },
    {
      instanceId: ID.catMeal,
      type: 'deal-row:category',
      config: {
        title: { he: 'ארוחה', en: 'Meal' },
        categoryId: CAT.meal.id,
        categorySlug: CAT.meal.slug,
        categoryName: { he: 'ארוחה', en: 'Meal' },
        limit: 10,
      },
      visibility: v,
    },
    {
      instanceId: ID.catBakery,
      type: 'deal-row:category',
      config: {
        title: { he: 'מאפייה', en: 'Bakery' },
        categoryId: CAT.bakery.id,
        categorySlug: CAT.bakery.slug,
        categoryName: { he: 'מאפייה', en: 'Bakery' },
        limit: 10,
      },
      visibility: v,
    },
    {
      instanceId: ID.catBeverage,
      type: 'deal-row:category',
      config: {
        title: { he: 'שתייה', en: 'Beverage' },
        categoryId: CAT.beverage.id,
        categorySlug: CAT.beverage.slug,
        categoryName: { he: 'שתייה', en: 'Beverage' },
        limit: 10,
      },
      visibility: v,
    },
    {
      instanceId: ID.catDessert,
      type: 'deal-row:category',
      config: {
        title: { he: 'קינוח', en: 'Dessert' },
        categoryId: CAT.dessert.id,
        categorySlug: CAT.dessert.slug,
        categoryName: { he: 'קינוח', en: 'Dessert' },
        limit: 10,
      },
      visibility: v,
    },
    {
      instanceId: ID.catPackaged,
      type: 'deal-row:category',
      config: {
        title: { he: 'ארוז', en: 'Packaged' },
        categoryId: CAT.packaged.id,
        categorySlug: CAT.packaged.slug,
        categoryName: { he: 'ארוז', en: 'Packaged' },
        limit: 10,
      },
      visibility: v,
    },
  ];
  return { mobile: shared, desktop: shared };
})();

/** UUID-v4 for the seeded version 1 `page_layouts.id` (deterministic for idempotency). */
export const HOME_DEFAULT_LAYOUT_ID = '00000000-0000-4000-8000-0000000000aa';
