/* ============================================================================
   RECAP — shareable end-of-trip memorabilia. Editorial + Wrapped-style stats.
   ============================================================================ */
function RecapScreen({ state }) {
  const T = window.TRIP;
  const store = usePhotos();
  const visited = state.places.filter((p) => p.visited).length;
  const spent = state.expenses.reduce((s, e) => s + e.vnd, 0);
  const dishes = state.dishes.filter((d) => d.tried).length;
  const photos = store.list.length;

  const paid = {};
  T.people.forEach((p) => { paid[p.id] = 0; });
  state.expenses.forEach((e) => { paid[e.by] += e.vnd; });
  const topSpender = T.people.reduce((a, b) => (paid[b.id] > paid[a.id] ? b : a));

  // One playful award per traveller — safe for any party size.
  const TITLES = [
    { title: "The planner", note: "Kept the trip on track" },
    { title: "The foodie", note: "Found every bánh mì" },
    { title: "The navigator", note: "Never once got lost" },
    { title: "The shutterbug", note: "Camera always ready" },
  ];
  const crew = T.people.map((p, i) => {
    const t = TITLES[i % TITLES.length];
    const isTop = spent > 0 && p.id === topSpender.id;
    return { id: p.id, title: isTop ? "Treasurer" : t.title, note: isTop ? `Fronted ${T.fmtVND(paid[p.id])}` : t.note };
  });

  const [shared, setShared] = React.useState(false);

  return (
    <div style={{ background: "#06363a" }}>
      {/* ---------- COVER ---------- */}
      <div style={{ position: "relative", overflow: "hidden", padding: "30px 22px 30px",
        background: "linear-gradient(165deg, #052e31 0%, #0a5054 55%, #0c6064 100%)", color: "#fff" }}>
        <div style={{ position: "absolute", inset: 0, opacity: 0.5, pointerEvents: "none",
          background: "radial-gradient(circle at 88% 12%, rgba(234,163,0,0.5) 0 7px, transparent 8px), radial-gradient(circle at 74% 6%, rgba(255,140,0,0.4) 0 5px, transparent 6px), radial-gradient(circle at 14% 90%, rgba(234,163,0,0.3) 0 6px, transparent 7px)" }} />
        <div style={{ position: "relative", display: "flex", justifyContent: "space-between", alignItems: "flex-start" }}>
          <Overline color="rgba(255,255,255,0.7)">A trip to remember</Overline>
          <Stamp color="var(--color-marigold)" rotate={7}>Vietnam '26</Stamp>
        </div>
        <div style={{ position: "relative", marginTop: 14 }}>
          <Serif size={52} weight={700} color="#fff" italic style={{ display: "block", lineHeight: 1.02, letterSpacing: "-0.02em" }}>Hà Nội</Serif>
          <Serif size={26} weight={400} color="rgba(255,255,255,0.82)" style={{ display: "block", marginTop: 4 }}>A Việt Nam diary</Serif>
        </div>
        <div style={{ position: "relative", marginTop: 16, display: "flex", alignItems: "center", gap: 10 }}>
          <AvatarStack people={T.people} size={30} />
          <span style={{ fontSize: 13, color: "rgba(255,255,255,0.78)" }}>{T.meta.dates}</span>
        </div>
      </div>

      {/* ---------- BY THE NUMBERS ---------- */}
      <div style={{ background: "#faf7f0", padding: "22px 18px 26px" }}>
        <Overline color="var(--color-pumpkin)" style={{ textAlign: "center", marginBottom: 16 }}>By the numbers</Overline>
        <div style={{ display: "grid", gridTemplateColumns: "1fr 1fr", gap: 12 }}>
          <BigStat value={visited} label="places explored" color="var(--color-teal)" />
          <BigStat value={T.cities.length} label={T.cities.length === 1 ? "city explored" : "cities explored"} color="var(--color-pumpkin)" />
          <BigStat value={dishes} label="dishes devoured" color="var(--color-marigold)" />
          <BigStat value={photos} label="photos captured" color="var(--color-grape)" />
        </div>
        <div style={{ marginTop: 12, background: "#fff", borderRadius: 16, padding: "18px 18px", textAlign: "center",
          border: "1px solid rgba(0,0,0,0.06)", boxShadow: "var(--shadow2)" }}>
          <div style={{ fontSize: 12, fontWeight: 700, letterSpacing: "0.1em", textTransform: "uppercase", color: "var(--colorNeutralForeground3)" }}>Spent together</div>
          <div style={{ fontFamily: "var(--fontFamilySerif)", fontSize: 40, fontWeight: 700, color: "var(--color-darkTeal)", marginTop: 4, letterSpacing: "-0.02em" }}>{T.fmtVNDFull(spent)}</div>
          <div style={{ fontSize: 13, color: "var(--colorNeutralForeground3)" }}>≈ {T.toINR(spent)} · {T.fmtVND(spent / T.people.length)} each</div>
        </div>
      </div>

      {/* ---------- JOURNEY MAP ---------- */}
      <div style={{ background: "#06363a", padding: "26px 18px" }}>
        <Overline color="rgba(255,255,255,0.6)" style={{ marginBottom: 6 }}>Where we wandered</Overline>
        <div style={{ fontSize: 12.5, color: "rgba(255,255,255,0.55)", marginBottom: 14 }}>{T.cities.length > 1 ? `North to south · ${T.cities.length} cities · ` : ""}Photos pin to the map</div>
        <JourneyMap />
      </div>

      {/* ---------- PHOTO STRIP ---------- */}
      <div style={{ background: "#faf7f0", padding: "22px 18px" }}>
        <Overline color="var(--color-pumpkin)" style={{ marginBottom: 14 }}>Moments</Overline>
        {store.list.length === 0 ? (
          <div style={{ padding: "20px", borderRadius: 14, border: "1.5px dashed rgba(0,0,0,0.18)", textAlign: "center", background: "#fff" }}>
            <Icon name="camera" size={24} color="var(--color-pumpkin)" />
            <div style={{ fontSize: 13, color: "var(--colorNeutralForeground3)", marginTop: 6 }}>Add photos in the Photos tab and they'll appear here.</div>
          </div>
        ) : (
          <div style={{ display: "flex", gap: 10, overflowX: "auto", margin: "0 -18px", padding: "0 18px 6px" }}>
            {store.list.slice(0, 8).map((p, i) => (
              <div key={p.id} style={{ flex: "0 0 auto", width: 130, background: "#fff", padding: 7, paddingBottom: 26,
                borderRadius: 4, boxShadow: "var(--shadow8)", transform: `rotate(${i % 2 ? 1.5 : -1.5}deg)` }}>
                <img src={p.src} alt={p.caption || p.place} style={{ display: "block", width: "100%", height: "150px", objectFit: "cover" }} />
                <div style={{ fontFamily: "var(--fontFamilySerif)", fontStyle: "italic", fontSize: 12.5, textAlign: "center", marginTop: 8, color: "#333", whiteSpace: "nowrap", overflow: "hidden", textOverflow: "ellipsis" }}>{p.caption || p.place}</div>
              </div>
            ))}
          </div>
        )}
      </div>

      {/* ---------- THE CREW ---------- */}
      <div style={{ background: "#06363a", padding: "24px 22px" }}>
        <Overline color="rgba(255,255,255,0.6)" style={{ marginBottom: 16 }}>The crew</Overline>
        <div style={{ display: "flex", flexDirection: "column", gap: 12 }}>
          {crew.map((m, i) => {
            const p = T.people.find((x) => x.id === m.id);
            return (
              <div key={i} style={{ display: "flex", alignItems: "center", gap: 13 }}>
                <Avatar person={p} size={42} />
                <div style={{ flex: 1 }}>
                  <div style={{ fontSize: 15, fontWeight: 700, color: "#fff" }}>{p.name}</div>
                  <div style={{ fontSize: 12.5, color: "rgba(255,255,255,0.62)" }}>{m.note}</div>
                </div>
                <Chip style={{ background: "rgba(234,163,0,0.18)", color: "var(--color-marigold)" }}>{m.title}</Chip>
              </div>
            );
          })}
        </div>
      </div>

      {/* ---------- SHARE ---------- */}
      <div style={{ background: "linear-gradient(180deg, #06363a, #052528)", padding: "28px 22px 36px", textAlign: "center" }}>
        <Serif size={24} weight={600} italic color="#fff" style={{ display: "block" }}>Hẹn gặp lại, Việt Nam</Serif>
        <div style={{ fontSize: 13, color: "rgba(255,255,255,0.6)", marginTop: 6, marginBottom: 18 }}>Until next time — share the memories with the crew</div>
        <Btn kind="amber" icon={shared ? "checkmark" : "share"} onClick={() => { setShared(true); setTimeout(() => setShared(false), 2200); }} full style={{ maxWidth: 280, margin: "0 auto" }}>
          {shared ? "Link copied to clipboard" : "Share trip recap"}
        </Btn>
      </div>
    </div>
  );
}

function BigStat({ value, label, color }) {
  return (
    <div style={{ background: "#fff", borderRadius: 16, padding: "18px 14px", textAlign: "center",
      border: "1px solid rgba(0,0,0,0.06)", boxShadow: "var(--shadow2)" }}>
      <div style={{ fontFamily: "var(--fontFamilySerif)", fontSize: 44, fontWeight: 700, color, lineHeight: 1, letterSpacing: "-0.02em" }}>{value}</div>
      <div style={{ fontSize: 12.5, color: "var(--colorNeutralForeground3)", fontWeight: 600, marginTop: 6 }}>{label}</div>
    </div>
  );
}

Object.assign(window, { RecapScreen });
