// =============================================================
// Sidebar / TopNav — desktop layout shell
// =============================================================

const NAV_ITEMS = [
  { id: "home",     label: "首页",    sub: "Home",         icon: "home" },
  { id: "album",    label: "相册",    sub: "Album",        icon: "album" },
  { id: "promises", label: "承诺",    sub: "Promises",     icon: "promise" },
  { id: "stories",  label: "故事本",  sub: "Stories",      icon: "story" },
];

const NavIcon = ({ name, on }) => {
  const c = on ? "#8B4F32" : "#B5946D";
  const stroke = { fill: "none", stroke: c, strokeWidth: 1.8, strokeLinecap: "round", strokeLinejoin: "round" };
  switch (name) {
    case "home":    return <svg width="22" height="22" viewBox="0 0 24 24" {...stroke}><path d="M3 11l9-7 9 7v9a2 2 0 0 1-2 2h-3v-6h-8v6H5a2 2 0 0 1-2-2z"/></svg>;
    case "album":   return <svg width="22" height="22" viewBox="0 0 24 24" {...stroke}><rect x="3" y="5" width="18" height="14" rx="3"/><circle cx="9" cy="11" r="2"/><path d="m21 17-5-5-9 9"/></svg>;
    case "promise": return <svg width="22" height="22" viewBox="0 0 24 24" {...stroke}><path d="M5 4h11l3 3v13a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V5a1 1 0 0 1 1-1z"/><path d="m9 13 2.2 2.2L16 11"/></svg>;
    case "story":   return <svg width="22" height="22" viewBox="0 0 24 24" {...stroke}><path d="M5 4h11a3 3 0 0 1 3 3v13H8a3 3 0 0 1-3-3z"/><path d="M5 17a3 3 0 0 0 3 3"/><path d="M9 8h7M9 12h5"/></svg>;
    default:        return null;
  }
};

const Sidebar = ({ active, onNav, onSettings, settings }) => {
  const togetherDays = window.xm.util.daysSince(settings.togetherDate);
  return (
    <aside style={{
      width: 224, flexShrink: 0,
      background: "linear-gradient(180deg, #FBE3B0 0%, #FFF8DD 60%, #FFFDF6 100%)",
      borderRight: "1px solid rgba(217,162,86,0.32)",
      padding: "24px 16px", display: "flex", flexDirection: "column", gap: 18,
      position: "sticky", top: 0, height: "100vh", zIndex: 10,
    }}>
      {/* Brand */}
      <button onClick={() => onNav("home")} style={{ display: "flex", alignItems: "center", gap: 10, background: "transparent", border: "none", cursor: "pointer", padding: 4 }}>
        <img src="../assets/app-icon.svg" style={{ width: 44, height: 44, filter: "drop-shadow(0 2px 6px rgba(184,133,46,0.25))" }} alt=""/>
        <div style={{ textAlign: "left" }}>
          <div style={{ font: "700 24px/1 'Jiangcheng Yuan', 'PingFang SC', sans-serif", color: "#4A2A1A", letterSpacing: "0.06em" }}>小抹</div>
          <div style={{ font: "600 11px 'Caveat', cursive", color: "#8B4F32", letterSpacing: "0.04em", marginTop: 3 }}>our little universe</div>
        </div>
      </button>

      {/* Day count card */}
      <div style={{
        background: "linear-gradient(135deg,#FFFDF6 0%,#FFF1D4 100%)",
        borderRadius: 18, padding: "14px 16px",
        boxShadow: "0 2px 10px rgba(217,162,86,0.20), inset 0 0 0 1px rgba(255,255,255,0.7)",
        border: "1px solid rgba(217,162,86,0.20)",
      }}>
        <div style={{ font: "600 10px 'Caveat', cursive", color: "#8B6B43", letterSpacing: "0.16em", textTransform: "uppercase" }}>Together</div>
        <div style={{ display: "flex", alignItems: "baseline", gap: 6, marginTop: 2 }}>
          <span style={{ font: "700 32px/1 'Quicksand', sans-serif", color: "#B8852E", fontVariantNumeric: "tabular-nums" }}>{togetherDays}</span>
          <span style={{ font: "500 13px 'Jiangcheng Yuan', sans-serif", color: "#6B4A2E" }}>天 ♡</span>
        </div>
      </div>

      {/* Nav */}
      <nav style={{ display: "flex", flexDirection: "column", gap: 4 }}>
        {NAV_ITEMS.map((item) => {
          const on = active === item.id;
          return (
            <button key={item.id} onClick={() => onNav(item.id)} style={{
              display: "flex", alignItems: "center", gap: 12,
              padding: "11px 14px", borderRadius: 16,
              background: on ? "#FFFDF6" : "transparent",
              boxShadow: on ? "0 4px 14px rgba(184,133,46,0.18), inset 0 0 0 1px rgba(255,255,255,0.7), inset 0 0 0 2px rgba(217,162,86,0.30)" : "none",
              border: "none", cursor: "pointer", textAlign: "left",
              transition: "all 180ms",
            }}>
              <NavIcon name={item.icon} on={on}/>
              <div>
                <div style={{ font: "600 15px 'Jiangcheng Yuan', 'PingFang SC', sans-serif", color: on ? "#4A2A1A" : "#6B4A2E", letterSpacing: "0.04em" }}>{item.label}</div>
                <div style={{ font: "600 10px 'Caveat', cursive", color: on ? "#B8852E" : "#B5946D", letterSpacing: "0.04em" }}>{item.sub}</div>
              </div>
            </button>
          );
        })}
      </nav>

      <div style={{ marginTop: "auto", display: "flex", flexDirection: "column", gap: 10 }}>
        <div style={{ height: 12, background: "url('../assets/wavy-divider.svg') center/100% 100% no-repeat", opacity: 0.5, filter: "hue-rotate(30deg) saturate(0.7)" }}/>
        <button onClick={onSettings} style={{
          display: "flex", alignItems: "center", gap: 10,
          padding: "10px 12px", borderRadius: 14,
          background: "transparent", border: "none", cursor: "pointer", color: "#8B6B43",
          font: "500 13px 'Jiangcheng Yuan', 'PingFang SC', sans-serif",
        }}>
          <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><circle cx="12" cy="12" r="3"/><path d="M19.4 15a1.7 1.7 0 0 0 .3 1.8l.1.1a2 2 0 1 1-2.8 2.8l-.1-.1a1.7 1.7 0 0 0-1.8-.3 1.7 1.7 0 0 0-1 1.5V21a2 2 0 1 1-4 0v-.1a1.7 1.7 0 0 0-1.1-1.5 1.7 1.7 0 0 0-1.8.3l-.1.1a2 2 0 1 1-2.8-2.8l.1-.1a1.7 1.7 0 0 0 .3-1.8 1.7 1.7 0 0 0-1.5-1H3a2 2 0 1 1 0-4h.1a1.7 1.7 0 0 0 1.5-1.1 1.7 1.7 0 0 0-.3-1.8l-.1-.1a2 2 0 1 1 2.8-2.8l.1.1a1.7 1.7 0 0 0 1.8.3H9a1.7 1.7 0 0 0 1-1.5V3a2 2 0 1 1 4 0v.1a1.7 1.7 0 0 0 1 1.5 1.7 1.7 0 0 0 1.8-.3l.1-.1a2 2 0 1 1 2.8 2.8l-.1.1a1.7 1.7 0 0 0-.3 1.8V9a1.7 1.7 0 0 0 1.5 1H21a2 2 0 1 1 0 4h-.1a1.7 1.7 0 0 0-1.5 1z"/></svg>
          设置
        </button>
        <div style={{ font: "600 12px 'Caveat', cursive", color: "#B5946D", letterSpacing: "0.04em", textAlign: "center", padding: "0 4px" }}>made with ♡</div>
      </div>
    </aside>
  );
};

Object.assign(window, { Sidebar, NAV_ITEMS });
