/**
 * Public API for the site image catalog.
 * Import from here (or @/lib/images re-export) everywhere you need an image URL.
 */
import type { Category } from '@/domain/entities';
import {
  IMAGE_FOLDER,
  siteImage,
  BRAND_IMAGES,
  PAGE_IMAGES,
  CATEGORY_IMAGES,
  PRODUCT_IMAGES,
  OFFER_IMAGE_REFS,
  IMAGE_QUALITY,
  type ProductImageId,
  type OfferImageId,
} from './catalog';

export {
  IMAGE_FOLDER,
  siteImage,
  BRAND_IMAGES,
  PAGE_IMAGES,
  CATEGORY_IMAGES,
  PRODUCT_IMAGES,
  OFFER_IMAGE_REFS,
  IMAGE_QUALITY,
  type ProductImageId,
  type OfferImageId,
};

/** Resolved product image fields for Product entity */
export function getProductImages(productId: string): {
  image: string;
  images: string[];
} {
  const entry = PRODUCT_IMAGES[productId as ProductImageId];
  if (!entry) {
    throw new Error(
      `[site-images] Missing product image for "${productId}". Add it to infrastructure/images/catalog.ts`
    );
  }
  return {
    image: entry.main,
    images: entry.gallery ?? [entry.main],
  };
}

/** Single URL for a weekly offer card */
export function getOfferImage(offerId: string): string {
  const ref = OFFER_IMAGE_REFS[offerId as OfferImageId];
  if (!ref) {
    throw new Error(
      `[site-images] Missing offer image ref for "${offerId}". Add it to infrastructure/images/catalog.ts`
    );
  }
  if (ref.type === 'product') {
    return getProductImages(ref.id).image;
  }
  return CATEGORY_IMAGES[ref.id];
}

/** Category tile image */
export function getCategoryImage(category: Category): string {
  return CATEGORY_IMAGES[category];
}

/**
 * Legacy shape used across components — built from the catalog above.
 * Prefer getProductImages / PAGE_IMAGES / BRAND_IMAGES in new code.
 */
export const IMAGES = {
  logo: BRAND_IMAGES.logo,
  hero: PAGE_IMAGES.hero,
  aboutMeat: PAGE_IMAGES.aboutMeat,
  categories: CATEGORY_IMAGES,
  products: Object.fromEntries(
    Object.entries(PRODUCT_IMAGES).map(([id, entry]) => [id, entry.main])
  ) as Record<ProductImageId, string>,
} as const;