// Sahh — product mockup components used across landing sections
// Each one is a static, but pixel-honest, snapshot of a real product surface.

// ============================================================
// DISH CARD — the centerpiece showing Sahh Score, calories, macros, tags
// ============================================================
const DishCard = ({ s, dir, dark = false, compact = false, scoreSize = 92 }) => {
  const ar = dir === "rtl";
  return (
    <div className="card" style={{
      padding: compact ? 18 : 24,
      background: dark ? "var(--green-3)" : "var(--white)",
      borderColor: dark ? "rgba(255,255,255,0.08)" : "var(--hair)",
      color: dark ? "#fff" : "var(--ink)",
      borderRadius: 22,
      position: "relative",
      overflow: "hidden",
    }}>
      {/* top row: dish + score */}
      <div className="row" style={{ alignItems: "flex-start", gap: 16, marginBottom: 18 }}>
        <div style={{ flex: 1, minWidth: 0 }}>
          <div style={{
            fontSize: 11,
            letterSpacing: "0.14em",
            textTransform: "uppercase",
            color: dark ? "var(--lime)" : "var(--green)",
            fontWeight: 600,
            marginBottom: 4,
            fontFamily: "var(--f-mono)",
          }}>Sahh · Verified</div>
          <div style={{
            fontFamily: "var(--f-display)",
            fontWeight: 700,
            fontSize: 22,
            letterSpacing: "-0.02em",
            lineHeight: 1.1,
            marginBottom: 4,
          }}>{s.dishName}</div>
          <div style={{ fontSize: 12, color: dark ? "rgba(255,255,255,0.6)" : "var(--ink-3)" }}>{s.restaurant}</div>
        </div>
        {/* Sahh score ring */}
        <SahhScoreRing value={parseInt(s.score)} size={scoreSize} dark={dark} />
      </div>

      {/* big calorie number */}
      <div style={{ display: "flex", alignItems: "baseline", gap: 12, marginBottom: 18 }}>
        <div className="h-display tnum" style={{
          fontSize: 56,
          color: dark ? "var(--lime)" : "var(--green)",
          lineHeight: 1,
        }}>{s.cal}</div>
        <div style={{ fontSize: 12, color: dark ? "rgba(255,255,255,0.6)" : "var(--ink-3)", letterSpacing: "0.04em" }}>
          {ar ? "سعرة · لكل حصة" : "kcal · per serving"}
        </div>
      </div>

      {/* macros */}
      <div style={{
        display: "grid",
        gridTemplateColumns: "1fr 1fr 1fr",
        gap: 10,
        paddingTop: 14,
        borderTop: dark ? "1px solid rgba(255,255,255,0.1)" : "1px solid var(--hair)",
        marginBottom: 14,
      }}>
        {[
          [ar ? "بروتين" : "Protein", s.protein],
          [ar ? "كارب" : "Carbs", s.carbs],
          [ar ? "دهون" : "Fat", s.fat],
        ].map(([k, v], i) => (
          <div key={i}>
            <div style={{
              fontSize: 10,
              color: dark ? "rgba(255,255,255,0.55)" : "var(--ink-3)",
              letterSpacing: "0.06em",
              textTransform: "uppercase",
              marginBottom: 4,
            }}>{k}</div>
            <div className="mono tnum" style={{
              fontWeight: 600,
              fontSize: 16,
              color: dark ? "#fff" : "var(--ink)",
            }}>{v}</div>
          </div>
        ))}
      </div>

      {/* tags */}
      <div style={{ display: "flex", gap: 6, flexWrap: "wrap" }}>
        {s.tags.map((tag, i) => (
          <span key={i} className="chip" style={{
            background: dark ? "rgba(197,245,96,0.14)" : "var(--lime-soft)",
            color: dark ? "var(--lime)" : "var(--green-3)",
            fontSize: 11,
            padding: "5px 10px",
          }}>{tag}</span>
        ))}
      </div>
    </div>
  );
};

// ============================================================
// SAHH SCORE RING
// ============================================================
const SahhScoreRing = ({ value, size = 92, dark = false }) => {
  const r = size / 2 - 6;
  const cir = 2 * Math.PI * r;
  const off = cir * (1 - value / 100);
  const trackColor = dark ? "rgba(255,255,255,0.12)" : "var(--paper-2)";
  const fillColor = dark ? "var(--lime)" : "var(--green)";
  return (
    <div style={{ position: "relative", width: size, height: size, flexShrink: 0 }}>
      <svg width={size} height={size} style={{ transform: "rotate(-90deg)" }}>
        <circle cx={size/2} cy={size/2} r={r} fill="none" stroke={trackColor} strokeWidth="6" />
        <circle cx={size/2} cy={size/2} r={r} fill="none" stroke={fillColor} strokeWidth="6"
          strokeDasharray={cir} strokeDashoffset={off} strokeLinecap="round" />
      </svg>
      <div style={{
        position: "absolute", inset: 0,
        display: "flex", flexDirection: "column",
        alignItems: "center", justifyContent: "center",
        textAlign: "center", lineHeight: 1,
      }}>
        <div className="h-display tnum" style={{
          fontSize: size * 0.36,
          color: dark ? "#fff" : "var(--ink)",
          letterSpacing: "-0.04em",
        }}>{value}</div>
        <div style={{
          fontSize: 9,
          color: dark ? "rgba(255,255,255,0.55)" : "var(--ink-3)",
          letterSpacing: "0.12em",
          fontWeight: 600,
          textTransform: "uppercase",
          fontFamily: "var(--f-mono)",
          marginTop: 2,
        }}>Sahh</div>
      </div>
    </div>
  );
};

// ============================================================
// QR PATTERN
// ============================================================
const QRPattern = ({ size = 120, color = "currentColor", bg = "transparent" }) => {
  // Pseudo-random but stable QR pattern
  const grid = 21;
  const cells = [];
  // Hash-based pattern
  let seed = 12345;
  const rnd = () => { seed = (seed * 9301 + 49297) % 233280; return seed / 233280; };
  const filled = new Set();
  for (let y = 0; y < grid; y++) for (let x = 0; x < grid; x++) {
    if (rnd() > 0.5) filled.add(`${x},${y}`);
  }
  // Draw finder squares
  const isFinder = (x, y) =>
    (x < 7 && y < 7) || (x > grid - 8 && y < 7) || (x < 7 && y > grid - 8);
  for (let y = 0; y < grid; y++) for (let x = 0; x < grid; x++) {
    if (isFinder(x, y)) continue;
    if (filled.has(`${x},${y}`)) cells.push([x, y]);
  }
  const drawFinder = (ox, oy) => (
    <g key={`f-${ox}-${oy}`}>
      <rect x={ox} y={oy} width={7} height={7} fill={color} />
      <rect x={ox+1} y={oy+1} width={5} height={5} fill={bg === "transparent" ? "var(--white)" : bg} />
      <rect x={ox+2} y={oy+2} width={3} height={3} fill={color} />
    </g>
  );
  return (
    <svg width={size} height={size} viewBox={`0 0 ${grid} ${grid}`} style={{ shapeRendering: "crispEdges", display: "block" }}>
      {bg !== "transparent" && <rect width={grid} height={grid} fill={bg} />}
      {cells.map(([x, y], i) => <rect key={i} x={x} y={y} width={1} height={1} fill={color} />)}
      {drawFinder(0, 0)}
      {drawFinder(grid-7, 0)}
      {drawFinder(0, grid-7)}
    </svg>
  );
};

// ============================================================
// DASHBOARD MOCKUP — the hero right + section 04
// ============================================================
const DashboardMockup = ({ dir, compact = false }) => {
  const ar = dir === "rtl";
  return (
    <div className="dash-frame">
      <div className="dash-screen">
        {/* topbar */}
        <div className="dash-topbar">
          <div className="dash-dot"/><div className="dash-dot"/><div className="dash-dot"/>
          <div style={{ marginInlineStart: 12, display: "flex", alignItems: "center", gap: 8 }}>
            <SahhMark size={16} color="var(--green)" />
            <span style={{ fontFamily: "var(--f-display)", fontWeight: 700, fontSize: 13, letterSpacing: "-0.02em" }}>Sahh</span>
            <span style={{ fontSize: 11, color: "var(--ink-3)" }}>/ {ar ? "نجد كيتشن" : "Najd Kitchen"}</span>
          </div>
          <div className="dash-tab">app.sahh.sa/dashboard</div>
        </div>
        {/* body */}
        <div style={{ display: "grid", gridTemplateColumns: "180px 1fr", minHeight: compact ? 380 : 480 }}>
          {/* sidebar */}
          <div style={{ background: "var(--paper)", borderInlineEnd: "1px solid var(--hair)", padding: 14 }}>
            <div style={{ fontSize: 10, color: "var(--ink-3)", textTransform: "uppercase", letterSpacing: "0.1em", fontWeight: 600, padding: "6px 8px", marginBottom: 4 }}>
              {ar ? "القائمة" : "Workspace"}
            </div>
            {[
              ["chart", ar ? "نظرة عامة" : "Overview", false],
              ["chef", ar ? "الأطباق" : "Dishes", true],
              ["qr", ar ? "قوائم QR" : "QR menus", false],
              ["users", ar ? "الفروع" : "Locations", false],
              ["trend", ar ? "التحليلات" : "Analytics", false],
            ].map(([icon, label, active], i) => (
              <div key={i} className="row" style={{
                padding: "7px 10px",
                borderRadius: 8,
                fontSize: 12,
                fontWeight: active ? 600 : 500,
                color: active ? "var(--green-3)" : "var(--ink-2)",
                background: active ? "var(--lime-soft)" : "transparent",
                marginBottom: 2,
              }}>
                <Icon name={icon} size={13} color={active ? "var(--green)" : "var(--ink-3)"} />
                <span>{label}</span>
              </div>
            ))}
            <div style={{ height: 1, background: "var(--hair)", margin: "10px 0" }} />
            <div className="row" style={{ padding: "6px 8px", gap: 8 }}>
              <SahhSeal size={28} />
              <div style={{ fontSize: 11 }}>
                <div style={{ fontWeight: 600 }}>{ar ? "معتمد" : "Verified"}</div>
                <div style={{ color: "var(--ink-3)", fontSize: 10 }}>MoH · {ar ? "نشط" : "Active"}</div>
              </div>
            </div>
          </div>

          {/* content */}
          <div style={{ padding: compact ? 16 : 22, background: "var(--white)" }}>
            <div className="row" style={{ justifyContent: "space-between", marginBottom: 14 }}>
              <div>
                <div style={{ fontSize: 11, color: "var(--ink-3)", marginBottom: 2 }}>
                  {ar ? "الأطباق" : "Dishes"} · {ar ? "كل الفروع" : "All locations"}
                </div>
                <div style={{ fontFamily: "var(--f-display)", fontWeight: 700, fontSize: 18, letterSpacing: "-0.02em" }}>
                  {ar ? "247 طبقاً معتمداً" : "247 dishes certified"}
                </div>
              </div>
              <div className="row" style={{ gap: 6 }}>
                <span className="chip chip-green" style={{ fontSize: 10 }}>+12 {ar ? "هذا الأسبوع" : "this week"}</span>
                <button style={{ padding: "5px 10px", borderRadius: 8, background: "var(--green)", color: "#fff", fontSize: 11, fontWeight: 600 }}>+ {ar ? "إضافة" : "Add dish"}</button>
              </div>
            </div>

            {/* KPI strip */}
            <div style={{ display: "grid", gridTemplateColumns: "repeat(4, 1fr)", gap: 8, marginBottom: 14 }}>
              {[
                [ar ? "متوسط مؤشر صح" : "Avg Sahh Score", "82", "+3"],
                [ar ? "مسحات اليوم" : "Scans today", "1,420", "+18%"],
                [ar ? "تباين السعرات" : "Calorie variance", "2.4%", "-0.8%"],
                [ar ? "شارات نشطة" : "Active QRs", "12", "stable"],
              ].map(([l, v, d], i) => (
                <div key={i} style={{ padding: 10, background: "var(--paper)", borderRadius: 10, border: "1px solid var(--hair)" }}>
                  <div style={{ fontSize: 9, color: "var(--ink-3)", letterSpacing: "0.06em", textTransform: "uppercase", fontWeight: 600 }}>{l}</div>
                  <div className="row" style={{ gap: 6, marginTop: 4, alignItems: "baseline" }}>
                    <div className="h-display tnum" style={{ fontSize: 18, color: "var(--green-3)" }}>{v}</div>
                    <div style={{ fontSize: 10, color: "var(--green)", fontWeight: 600 }}>{d}</div>
                  </div>
                </div>
              ))}
            </div>

            {/* dish list */}
            <div style={{ background: "var(--paper)", borderRadius: 10, border: "1px solid var(--hair)", overflow: "hidden" }}>
              <div style={{
                display: "grid",
                gridTemplateColumns: "1.6fr 60px 60px 70px 70px",
                gap: 8,
                padding: "8px 12px",
                fontSize: 9,
                fontWeight: 700,
                color: "var(--ink-3)",
                textTransform: "uppercase",
                letterSpacing: "0.08em",
                background: "var(--paper-2)",
                borderBottom: "1px solid var(--hair)",
              }}>
                <div>{ar ? "الطبق" : "Dish"}</div>
                <div>kcal</div>
                <div>{ar ? "بروتين" : "Protein"}</div>
                <div>{ar ? "مؤشر" : "Score"}</div>
                <div>{ar ? "الحالة" : "Status"}</div>
              </div>
              {[
                [ar ? "بول دجاج مشوي" : "Grilled Chicken Bowl", 520, "42g", 86, "ok"],
                [ar ? "سلمون مع كينوا" : "Salmon Quinoa", 612, "44g", 91, "ok"],
                [ar ? "شاورما لحم" : "Beef Shawarma", 720, "38g", 64, "warn"],
                [ar ? "سلطة كيل سيزر" : "Kale Caesar", 380, "18g", 78, "ok"],
                [ar ? "مندي دجاج" : "Chicken Mandi", 890, "52g", 58, "warn"],
              ].map(([name, kcal, p, score, st], i) => (
                <div key={i} style={{
                  display: "grid",
                  gridTemplateColumns: "1.6fr 60px 60px 70px 70px",
                  gap: 8,
                  padding: "9px 12px",
                  fontSize: 12,
                  alignItems: "center",
                  background: "var(--white)",
                  borderBottom: i < 4 ? "1px solid var(--hair)" : "none",
                }}>
                  <div style={{ fontWeight: 600 }}>{name}</div>
                  <div className="mono tnum">{kcal}</div>
                  <div className="mono tnum" style={{ color: "var(--ink-2)" }}>{p}</div>
                  <div>
                    <span style={{
                      display: "inline-flex",
                      alignItems: "center",
                      justifyContent: "center",
                      width: 32, height: 22,
                      borderRadius: 6,
                      fontWeight: 700,
                      fontSize: 11,
                      background: score >= 80 ? "var(--green-tint)" : score >= 65 ? "var(--lime-soft)" : "#FFF1E6",
                      color: score >= 80 ? "var(--green-2)" : score >= 65 ? "var(--green-3)" : "var(--warn)",
                      fontFamily: "var(--f-mono)",
                    }}>{score}</span>
                  </div>
                  <div>
                    <span className="chip chip-dot" style={{
                      fontSize: 10,
                      padding: "3px 8px",
                      background: st === "ok" ? "var(--green-tint)" : "#FFF1E6",
                      color: st === "ok" ? "var(--green-2)" : "var(--warn)",
                    }}>{st === "ok" ? (ar ? "معتمد" : "Live") : (ar ? "مراجعة" : "Review")}</span>
                  </div>
                </div>
              ))}
            </div>
          </div>
        </div>
      </div>
    </div>
  );
};

// ============================================================
// QR CARD — what restaurants print on the table
// ============================================================
const QRCard = ({ s, dir }) => {
  const ar = dir === "rtl";
  return (
    <div style={{
      width: 320,
      background: "var(--white)",
      border: "1px solid var(--hair)",
      borderRadius: 22,
      padding: 28,
      boxShadow: "var(--sh)",
      textAlign: "center",
    }}>
      <div className="row" style={{ justifyContent: "center", gap: 8, marginBottom: 4 }}>
        <SahhMark size={28} color="var(--green)" />
        <span style={{ fontFamily: "var(--f-display)", fontWeight: 700, fontSize: 22, letterSpacing: "-0.035em", color: "var(--green-3)" }}>Sahh</span>
        <span style={{ fontFamily: "var(--f-ar)", fontSize: 16, color: "var(--ink-3)" }}>صحّ</span>
      </div>
      <div style={{ fontSize: 10, color: "var(--green)", letterSpacing: "0.18em", textTransform: "uppercase", fontWeight: 600, fontFamily: "var(--f-mono)", marginBottom: 18 }}>
        {ar ? "قائمة معتمدة" : "Verified menu"}
      </div>
      <div style={{
        background: "var(--paper)",
        borderRadius: 16,
        padding: 18,
        marginBottom: 16,
        border: "1px solid var(--hair)",
      }}>
        <QRPattern size={180} color="var(--green-3)" />
      </div>
      <div style={{ fontFamily: "var(--f-display)", fontWeight: 700, fontSize: 18, letterSpacing: "-0.02em", marginBottom: 4 }}>
        {ar ? "امسح لرؤية التغذية" : "Scan to see nutrition"}
      </div>
      <div style={{ fontSize: 12, color: "var(--ink-3)", marginBottom: 16 }}>
        {ar ? "كل طبق · سعرات وماكروز ومؤشر صح" : "Every dish · calories, macros & Sahh Score"}
      </div>
      <div className="row" style={{ justifyContent: "center", gap: 8, paddingTop: 14, borderTop: "1px solid var(--hair)" }}>
        <Icon name="shield" size={13} color="var(--green)" />
        <span style={{ fontSize: 11, color: "var(--ink-3)", letterSpacing: "0.04em" }}>
          {ar ? "موثوق · وزارة الصحة" : "Verified · Ministry of Health"}
        </span>
      </div>
    </div>
  );
};

window.DishCard = DishCard;
window.SahhScoreRing = SahhScoreRing;
window.QRPattern = QRPattern;
window.DashboardMockup = DashboardMockup;
window.QRCard = QRCard;
