// ─────────────────────────────────────────────────────────
// ChefVerse · Photo placeholders
// Maps instructor names + course/module ids to stable Unsplash URLs.
// All sourced from unsplash.com (free, no auth) — images are pinned by photo id
// so they don't shuffle. Used for chef portraits and food covers.
// ─────────────────────────────────────────────────────────

// Helper — build a stable Unsplash URL.
const u = (id, w = 800, h = 600, crop = "faces") =>
  `https://images.unsplash.com/photo-${id}?auto=format&fit=crop&w=${w}&h=${h}&q=70${crop ? `&crop=${crop}` : ""}`;

// Chef portraits — keyed by instructor name (matches data.jsx).
const CHEF_PHOTOS = {
  "Marco Rivera":   u("1577219491135-ce391730fb2c", 240, 240),         // chef portrait
  "Ines Park":      u("1438761681033-6461ffad8d80", 240, 240),         // woman portrait
  "Tom Hartley":    u("1500648767791-00dcc994a43e", 240, 240),         // man portrait
  "Lila Tanaka":    u("1531123897727-8f129e1688ce", 240, 240),         // woman portrait
  "Élise Laurent":  u("1494790108377-be9c29b29330", 240, 240),         // woman smiling
  "Sara Halim":     u("1573496359142-b8d87734a5a2", 240, 240),         // woman portrait
};

// Generic fallback portrait pool — used for non-instructor avatars (community, recent submitters).
const COMMUNITY_FALLBACKS = [
  u("1507003211169-0a1dd7228f2d", 240, 240),
  u("1544005313-94ddf0286df2", 240, 240),
  u("1607746882042-944635dfe10e", 240, 240),
  u("1539571696357-5a69c17a67c6", 240, 240),
  u("1580489944761-15a19d654956", 240, 240),
  u("1502823403499-6ccfcf4fb453", 240, 240),
];

function chefPhotoFor(name) {
  if (!name) return null;
  if (CHEF_PHOTOS[name]) return CHEF_PHOTOS[name];
  // Hash the name into the fallback pool deterministically.
  let h = 0;
  for (let i = 0; i < name.length; i++) h = (h * 31 + name.charCodeAt(i)) | 0;
  return COMMUNITY_FALLBACKS[Math.abs(h) % COMMUNITY_FALLBACKS.length];
}

// Course/module food covers — keyed by course id, optionally by module index.
// Each course has an array; index 0 = course cover, 1..N = module covers.
const COURSE_PHOTOS = {
  fundamentals: [
    u("1504674900247-0877df9cc836", 1200, 700, "entropy"),  // plated dish, side light
    u("1565299624946-b28f40a0ae38", 1200, 700, "entropy"),  // pizza closeup
    u("1551183053-bf91a1d81141", 1200, 700, "entropy"),     // top-down pasta
    u("1555939594-58d7cb561ad1", 1200, 700, "entropy"),     // burger hero
    u("1546069901-ba9599a7e63c", 1200, 700, "entropy"),     // bowl food
    u("1540189549336-e6e99c3679fe", 1200, 700, "entropy"),  // salad
    u("1565958011703-44f9829ba187", 1200, 700, "entropy"),  // ramen
    u("1559847844-5315695dadae", 1200, 700, "entropy"),     // chef plating
    u("1414235077428-338989a2e8c0", 1200, 700, "entropy"),  // food top-down
  ],
  story: [
    u("1504674900247-0877df9cc836", 1200, 700, "entropy"),
    u("1551782450-a2132b4ba21d", 1200, 700, "entropy"),
    u("1551218808-94e220e084d2", 1200, 700, "entropy"),
    u("1555126634-323283e090fa", 1200, 700, "entropy"),
  ],
  voice: [
    u("1556909114-f6e7ad7d3136", 1200, 700, "entropy"),
    u("1565895405127-481853366cf8", 1200, 700, "entropy"),
  ],
  edit: [
    u("1432139509613-5c4255815697", 1200, 700, "entropy"),
    u("1555243896-c709bfa0b564", 1200, 700, "entropy"),
  ],
  brand: [
    u("1559847844-5315695dadae", 1200, 700, "entropy"),
    u("1556909114-f6e7ad7d3136", 1200, 700, "entropy"),
  ],
  analytics: [
    u("1546069901-ba9599a7e63c", 1200, 700, "entropy"),
    u("1565958011703-44f9829ba187", 1200, 700, "entropy"),
  ],
};

function courseCoverFor(courseId) {
  const arr = COURSE_PHOTOS[courseId];
  return arr ? arr[0] : null;
}

function moduleCoverFor(courseId, moduleIdx) {
  const arr = COURSE_PHOTOS[courseId];
  if (!arr) return null;
  // module covers start at index 1; loop if we run out
  return arr[(moduleIdx % (arr.length - 1)) + 1] || arr[0];
}

// Brief featured covers — generic food/cooking shots
const BRIEF_PHOTOS = [
  u("1565299624946-b28f40a0ae38", 1600, 700, "entropy"),
  u("1551183053-bf91a1d81141", 1600, 700, "entropy"),
  u("1555939594-58d7cb561ad1", 1600, 700, "entropy"),
  u("1414235077428-338989a2e8c0", 1600, 700, "entropy"),
];

function briefCoverFor(briefId) {
  if (!briefId) return BRIEF_PHOTOS[0];
  let h = 0;
  for (let i = 0; i < briefId.length; i++) h = (h * 31 + briefId.charCodeAt(i)) | 0;
  return BRIEF_PHOTOS[Math.abs(h) % BRIEF_PHOTOS.length];
}

window.PHOTOS = {
  chefPhotoFor,
  courseCoverFor,
  moduleCoverFor,
  briefCoverFor,
};

// Reusable avatar that shows a chef portrait when we have one for the name,
// otherwise falls back to colored initials.
function Avatar({ name, initials, cls = "", size }) {
  const url = chefPhotoFor(name);
  const style = size ? { width: size, height: size, fontSize: size <= 28 ? 11 : 12 } : undefined;
  if (url) {
    return (
      <div className={"av " + cls} style={style}>
        <img src={url} alt={name || "avatar"} loading="lazy"
             style={{ width: "100%", height: "100%", objectFit: "cover", borderRadius: "50%" }} />
      </div>
    );
  }
  return <div className={"av " + cls} style={style}>{initials || ""}</div>;
}
// Reusable cover-image overlay — drops a real photo on top of a gradient
// background. Parent must be position:relative; photo absolutely fills it.
function CoverImage({ src, alt = "", dim = 0, style }) {
  if (!src) return null;
  return (
    <>
      <img
        src={src}
        alt={alt}
        loading="lazy"
        style={{
          position: "absolute", inset: 0, width: "100%", height: "100%",
          objectFit: "cover", zIndex: 0,
          ...(style || {}),
        }}
      />
      {dim > 0 && (
        <div style={{
          position: "absolute", inset: 0, zIndex: 0,
          background: `linear-gradient(180deg, rgba(0,0,0,${dim * 0.3}) 0%, rgba(0,0,0,${dim}) 100%)`,
        }} />
      )}
    </>
  );
}
window.CoverImage = CoverImage;
