// Student theme: Playground sidebar shell, mascot, and navigation components.
const StxpgRobot = () => (
  <svg
    className="stxpg-robot"
    viewBox="0 0 120 130"
    width="110"
    height="120"
    fill="none"
    xmlns="http://www.w3.org/2000/svg"
    aria-hidden="true"
  >
    {/* Antenna */}
    <line x1="60" y1="28" x2="60" y2="12" stroke="#f5c518" strokeWidth="4" strokeLinecap="round"/>
    <circle cx="60" cy="10" r="6" fill="#f5c518"/>
    <circle cx="60" cy="10" r="2.5" fill="#ffffff"/>

    {/* Head Ears */}
    <rect x="22" y="42" width="6" height="12" rx="3" fill="#a7f3d0"/>
    <rect x="92" y="42" width="6" height="12" rx="3" fill="#a7f3d0"/>

    {/* Head */}
    <rect x="26" y="28" width="68" height="42" rx="14" fill="#ffffff" stroke="#e9e3d3" strokeWidth="2"/>
    {/* Face Screen */}
    <rect x="34" y="35" width="52" height="28" rx="8" fill="#1b4d3e"/>
    {/* Happy Eyes */}
    <circle cx="48" cy="48" r="5" fill="#f5c518"/>
    <circle cx="50" cy="46" r="1.5" fill="#ffffff"/>
    <circle cx="72" cy="48" r="5" fill="#f5c518"/>
    <circle cx="74" cy="46" r="1.5" fill="#ffffff"/>
    {/* Smile */}
    <path d="M54 54 Q60 58 66 54" stroke="#a7f3d0" strokeWidth="2" strokeLinecap="round" fill="none"/>

    {/* Body */}
    <rect x="36" y="74" width="48" height="38" rx="12" fill="#ffffff" stroke="#e9e3d3" strokeWidth="2"/>
    {/* Body screen / badge */}
    <rect x="44" y="82" width="32" height="22" rx="6" fill="#a7f3d0"/>
    {/* Heart or Star on chest */}
    <path d="M60 88 L61.8 92.2 L66.4 92.5 L62.9 95.5 L64 100 L60 97.5 L56 100 L57.1 95.5 L53.6 92.5 L58.2 92.2 Z" fill="#1b4d3e"/>

    {/* Left Arm (resting) */}
    <path d="M36 82 C26 86 24 96 32 100" stroke="#f5c518" strokeWidth="4" strokeLinecap="round" fill="none"/>

    {/* Right Arm (waving) */}
    <path d="M84 82 C94 74 102 60 98 52" stroke="#f5c518" strokeWidth="4" strokeLinecap="round" fill="none"/>
    <circle cx="98" cy="52" r="4" fill="#f5c518"/>

    {/* Base / Wheel */}
    <rect x="48" y="114" width="24" height="8" rx="4" fill="#e5b50f"/>
    <ellipse cx="60" cy="124" rx="18" ry="4" fill="rgba(0,0,0,0.15)"/>
  </svg>
);

const StxpgHeroGreeting = ({ name, level }) => (
  <div className="stxpg-greeting-card">
    <div className="stxpg-greeting-text">
      <span className="stxpg-greeting-badge">READY TO LEARN</span>
      <h1 className="stx-page-title stxpg-greeting-title">
        Hey {name},
        <br/>
        let’s learn!
      </h1>
      <p className="stxpg-greeting-sub">Level {level} Explorer</p>
    </div>
    <div className="stxpg-greeting-mascot">
      <StxpgRobot/>
    </div>
  </div>
);

const StxpgSidebarNav = ({ route, go, logout }) => {
  const nav = useNav();
  const items = stxNavItems(nav);

  return (
    <aside className="stxpg-sidebar" aria-label="Sidebar navigation">
      <nav className="stxpg-sidebar-nav" aria-label="Main">
        <div className="stxpg-nav-list">
          {items.map((item) => {
            const active = item.key === "home"
              ? route === "/app/student"
              : route === item.path || route.startsWith(item.path + "/");
            const label = item.label === "Quests" ? "Quiz" : item.label;
            return (
              <button
                type="button"
                key={item.key}
                className={`stxpg-nav-item ${active ? "stxpg-nav-active" : ""}`}
                aria-current={active ? "page" : undefined}
                aria-label={label}
                title={label}
                onClick={() => go(item.path)}
              >
                <span className="stxpg-nav-icon">
                  {stxIcon(stxNavIcon[item.key] || "dot", 20)}
                </span>
                <b className="stxpg-nav-label">{label}</b>
              </button>
            );
          })}
        </div>
      </nav>
      <div className="stxpg-sidebar-footer">
        <button
          type="button"
          className={`stxpg-sidebar-profile-btn ${route === "/app/student/profile" ? "stxpg-nav-active" : ""}`}
          onClick={() => go("/app/student/profile")}
          aria-label="My Profile"
          title="My Profile"
        >
          <span className="stxpg-nav-icon">{stxIcon("user", 18)}</span>
          <b className="stxpg-nav-label">Profile</b>
        </button>
        <button
          type="button"
          className="stxpg-sidebar-logout"
          onClick={logout}
          aria-label="Sign out"
        >
          <span className="stxpg-nav-icon">{stxIcon("close", 18)}</span>
          <b className="stxpg-nav-label">Sign out</b>
        </button>
      </div>
    </aside>
  );
};

const StxpgTopBar = ({ session, logout, student, loading, route, go }) => {
  const streak = student.streak_days || 0;
  const coins = student.coins ?? 0;

  return (
    <header className="stxpg-topbar">
      <div className="stx-brand">
        <span className="stx-brand-tile">💡</span>
        <b>EpiqMinds</b>
        <StxChip>STUDENT</StxChip>
      </div>
      <div className="stxpg-topbar-actions">
        <div className="stxpg-topbar-stats">
          {loading ? (
            <StxSkeleton className="stx-chip-skeleton"/>
          ) : (
            <>
              <StxChip className="stxpg-stat-chip">
                {stxIcon("flame", 16)}
                <span className="stxpg-chip-text">{streak} day streak</span>
              </StxChip>
              <StxChip className="stxpg-stat-chip">
                {stxIcon("coin", 16)}
                <span className="stxpg-chip-text">{coins} coins</span>
              </StxChip>
            </>
          )}
        </div>
        {typeof NotificationBell !== "undefined" && (
          <div className="stxpg-bell-wrapper">
            <NotificationBell session={session}/>
          </div>
        )}
        <StxAccountMenu
          session={session}
          logout={logout}
          student={student}
          loading={loading}
          route={route}
          go={go}
          compact
          triggerVariant="avatar"
        />
      </div>
    </header>
  );
};

const StxpgShell = ({ session, logout, student, loading, route, go, children }) => (
  <div className="stxpg-shell">
    <StxpgTopBar
      session={session}
      logout={logout}
      student={student}
      loading={loading}
      route={route}
      go={go}
    />
    <div className="stxpg-layout">
      <StxpgSidebarNav route={route} go={go} logout={logout}/>
      <main className="stx-body stxpg-stage">
        {children}
      </main>
    </div>
    <StxNav placement="dock" route={route} go={go}/>
  </div>
);

Object.assign(window, {
  StxpgRobot,
  StxpgHeroGreeting,
  StxpgSidebarNav,
  StxpgTopBar,
  StxpgShell,
});
