export const PHOTO_MANIFEST_PATH = '/assets/photos/manifest.json';

export type HomepageHeroPhotoSlot = {
  localPath?: string | null;
  remoteCandidate?: string | null;
  fallback?: string[];
};

export type PhotoManifest = {
  assets?: {
    photos?: {
      hero?: {
        homepage?: {
          primary?: HomepageHeroPhotoSlot;
        };
      };
    };
  };
};

/**
 * Wave 2.5 helper:
 * Fetch manifest and resolve semantic photo slots with local placeholder fallback.
 */
export async function loadPhotoManifest<T = unknown>(): Promise<T> {
  const response = await fetch(PHOTO_MANIFEST_PATH);
  if (!response.ok) {
    throw new Error(`Failed to load photo manifest: ${response.status}`);
  }
  return (await response.json()) as T;
}

export function resolveHomepageHeroCandidates(
  manifest: PhotoManifest,
  defaultFallbacks: string[] = ['/assets/food/placeholder-recipe.svg']
): string[] {
  const slot = manifest.assets?.photos?.hero?.homepage?.primary;
  const resolved = [slot?.localPath, slot?.remoteCandidate, ...(slot?.fallback ?? []), ...defaultFallbacks]
    .filter((value): value is string => typeof value === 'string' && value.trim().length > 0);

  return Array.from(new Set(resolved));
}
