// pages/dashboard.jsx — student dashboard: home, live, recordings list, player, profile

const { useState: useS_D } = React;

// ───── Shared shell ──────────────────────────────────────────
function DashShell({ go, current, isLoggedIn, children, setTweak }) {
  // If not logged in, you'd normally redirect to /login.
  // For the prototype, we render a friendly redirect prompt.
  if (!isLoggedIn) return <LoggedOutGate go={go} setTweak={setTweak} />;

  return (
    <div className="shell-with-sidebar">
      <aside className="sidebar">
        <div style={{ padding: "0 6px 12px" }}>
          <Brand onClick={() => go("/")} />
        </div>
        <div style={{ height: 12 }}/>
        <button className={`nav-link ${current === "home"       ? "active" : ""}`} onClick={() => go("/dashboard")}><Icon name="home" size={16}/> Dashboard</button>
        <button className={`nav-link ${current === "live"       ? "active" : ""}`} onClick={() => go("/dashboard/live")}><Icon name="video" size={16}/> Live class</button>
        <button className={`nav-link ${current === "recordings" ? "active" : ""}`} onClick={() => go("/dashboard/recordings")}><Icon name="layers" size={16}/> Recordings</button>
        <button className={`nav-link ${current === "profile"    ? "active" : ""}`} onClick={() => go("/dashboard/profile")}><Icon name="user" size={16}/> Profile</button>

        <div className="nav-section">Admin (preview)</div>
        <button className="nav-link" onClick={() => go("/admin")}><Icon name="settings" size={16}/> Admin panel</button>

        <div className="sidebar-foot">
          <div style={{ padding: "10px 14px", display: "flex", alignItems: "center", gap: 10, borderTop: "1px solid var(--line)", marginTop: 12 }}>
            <image-slot id="me-avatar" placeholder="You" shape="circle" style={{ width: 36, height: 36 }}></image-slot>
            <div style={{ minWidth: 0, flex: 1 }}>
              <div style={{ fontSize: 13, fontWeight: 600, whiteSpace: "nowrap", overflow: "hidden", textOverflow: "ellipsis" }}>Ananya Krishnan</div>
              <div style={{ fontSize: 11, color: "var(--ink-3)" }}>ISC-XII · Class 12</div>
            </div>
            <button title="Log out" className="nav-link" style={{ width: "auto", padding: 8, flexShrink: 0 }} onClick={() => { setTweak("authState", "logged_out"); go("/"); }}>
              <Icon name="logout" size={14}/>
            </button>
          </div>
        </div>
      </aside>
      <main className="main-area">{children}</main>
    </div>
  );
}

const LoggedOutGate = ({ go, setTweak }) => (
  <div style={{ minHeight: "100vh", display: "flex", alignItems: "center", justifyContent: "center", background: "var(--paper)" }}>
    <div className="card" style={{ maxWidth: 420, textAlign: "center", padding: 40 }}>
      <div style={{ width: 64, height: 64, borderRadius: "50%", background: "var(--blue-50)", color: "var(--blue)", display: "inline-flex", alignItems: "center", justifyContent: "center", marginBottom: 20 }}>
        <Icon name="lock" size={26}/>
      </div>
      <h3>Login required</h3>
      <p className="muted" style={{ marginTop: 8 }}>You need to be logged in to see the dashboard.</p>
      <div className="row gap-12" style={{ justifyContent: "center", marginTop: 24 }}>
        <button className="btn btn-primary" onClick={() => go("/login")}>Log in</button>
        <button className="btn btn-secondary" onClick={() => { setTweak("authState", "logged_in"); }}>Use Tweak: log me in</button>
      </div>
    </div>
  </div>
);

// ───── Dashboard home ────────────────────────────────────────
function DashboardHome({ go, isLoggedIn, enrollment, setTweak }) {
  return (
    <DashShell go={go} current="home" isLoggedIn={isLoggedIn} setTweak={setTweak}>
      <div className="page-head">
        <div>
          <div className="eyebrow">Tuesday, Aug 12</div>
          <h2 style={{ marginTop: 8 }}>Welcome back, Ananya.</h2>
        </div>
        <div className="row gap-12 center">
          <span className="pill pill-mono">ISC-XII · Class 12</span>
          <button className="btn btn-secondary btn-sm" onClick={() => go("/dashboard/profile")}>
            <Icon name="user" size={14}/> Profile
          </button>
        </div>
      </div>

      <div style={{ display: "grid", gridTemplateColumns: "2fr 1fr", gap: 24 }}>
        {/* Enrollment status */}
        <EnrollmentCard enrollment={enrollment} go={go} />

        {/* Next class */}
        <NextClassCard enrollment={enrollment} go={go} />
      </div>

      <div style={{ marginTop: 24, display: "grid", gridTemplateColumns: "2fr 1fr", gap: 24 }}>
        {/* Recent recordings */}
        <div className="card" style={{ padding: 0, overflow: "hidden" }}>
          <div className="row between center" style={{ padding: "20px 24px", borderBottom: "1px solid var(--line-2)" }}>
            <h3>Recent recordings</h3>
            <button className="btn btn-ghost btn-sm" onClick={() => go("/dashboard/recordings")}>View all <Icon name="arrow-right" size={14}/></button>
          </div>
          {enrollment === "enrolled" ? (
            <div>
              {RECORDINGS.slice(0, 3).map((r, i) => (
                <button key={r.id} onClick={() => go(`/dashboard/recordings/${r.id}`)}
                  style={{ all: "unset", cursor: "pointer", width: "100%", display: "flex", gap: 16, padding: 20, borderBottom: i < 2 ? "1px solid var(--line-2)" : "none", alignItems: "center" }}
                  onMouseEnter={e => e.currentTarget.style.background = "var(--paper)"}
                  onMouseLeave={e => e.currentTarget.style.background = ""}>
                  <div className="video-frame" style={{ width: 140, aspectRatio: "16/9", flexShrink: 0, borderRadius: 8 }}>
                    <Icon name="play" size={24}/>
                    <div className="video-meta"><span style={{ background: "rgba(0,0,0,.6)", padding: "2px 6px", borderRadius: 3, fontSize: 11 }}>{r.duration}</span></div>
                  </div>
                  <div style={{ flex: 1, minWidth: 0 }}>
                    <div style={{ fontFamily: "var(--font-mono)", fontSize: 11, color: "var(--ink-3)", letterSpacing: "0.06em", textTransform: "uppercase" }}>{r.chapter}</div>
                    <div style={{ fontWeight: 600, marginTop: 4 }}>{r.title}</div>
                    <div style={{ fontSize: 13, color: "var(--ink-3)", marginTop: 4, display: "flex", gap: 12 }}>
                      <span><Icon name="calendar" size={12}/> {r.date}</span>
                      <span><Icon name="clock" size={12}/> {r.duration}</span>
                    </div>
                  </div>
                  <Icon name="chevron-right" size={16}/>
                </button>
              ))}
            </div>
          ) : (
            <EmptyOrLockedRecordings enrollment={enrollment} go={go} compact/>
          )}
        </div>

        {/* Right column — sidebar widgets */}
        <div className="col gap-16">
          <div className="card">
            <h4 style={{ marginBottom: 16 }}>This week</h4>
            <div className="col gap-12">
              {[
                { day: "Mon 11", title: "Inheritance — Live", time: "7:00 PM", done: true },
                { day: "Thu 14", title: "Classes & Objects", time: "7:00 PM", done: true },
                { day: "Mon 18", title: "Polymorphism", time: "7:00 PM", done: false },
                { day: "Thu 21", title: "OOP Recap & Practice", time: "7:00 PM", done: false },
              ].map((s) => (
                <div key={s.day} className="row gap-12 center">
                  <span style={{ width: 36, fontFamily: "var(--font-mono)", fontSize: 11, color: "var(--ink-3)", textTransform: "uppercase" }}>{s.day}</span>
                  <span className="dot" style={{ background: s.done ? "var(--green)" : "var(--blue)" }}/>
                  <div style={{ flex: 1, minWidth: 0 }}>
                    <div style={{ fontSize: 13, fontWeight: 500, whiteSpace: "nowrap", overflow: "hidden", textOverflow: "ellipsis", color: s.done ? "var(--ink-3)" : "var(--ink)" }}>{s.title}</div>
                    <div style={{ fontSize: 11, color: "var(--ink-3)" }}>{s.time}</div>
                  </div>
                </div>
              ))}
            </div>
          </div>

          <div className="card card-dark">
            <div className="eyebrow" style={{ color: "rgba(255,255,255,.7)" }}>Tip of the day</div>
            <p style={{ marginTop: 12, fontSize: 15, color: "rgba(255,255,255,.9)" }}>
              In Python, lists are mutable but tuples are not. If you find yourself wanting to "change" a tuple — use a list.
            </p>
          </div>
        </div>
      </div>
    </DashShell>
  );
}

const EnrollmentCard = ({ enrollment, go }) => {
  if (enrollment === "enrolled") {
    return (
      <div className="card" style={{ padding: 28, background: "var(--blue)", color: "#fff", borderColor: "var(--blue)" }}>
        <div className="row between center" style={{ marginBottom: 20 }}>
          <span className="pill" style={{ background: "rgba(255,255,255,.15)", color: "#fff" }}>
            <span className="dot dot-green"/> Active enrolment
          </span>
          <span style={{ fontFamily: "var(--font-mono)", fontSize: 12, color: "rgba(255,255,255,.7)" }}>Expires Feb 04, 2026</span>
        </div>
        <h2 style={{ color: "#fff", fontSize: 32 }}>ISC-XII Aug 2025</h2>
        <p style={{ color: "rgba(255,255,255,.7)", marginTop: 8, maxWidth: 480 }}>ISC Class 12 — Computer Science · Live every Mon & Thu, 7:00 PM IST</p>
        <div className="row gap-12" style={{ marginTop: 24 }}>
          <button className="btn" style={{ background: "#fff", color: "var(--blue)" }} onClick={() => go("/dashboard/live")}>
            <Icon name="video" size={16}/> Join next class
          </button>
          <button className="btn btn-ghost" style={{ background: "rgba(255,255,255,.1)", color: "#fff" }} onClick={() => go("/dashboard/recordings")}>
            <Icon name="layers" size={16}/> All recordings
          </button>
        </div>
      </div>
    );
  }
  if (enrollment === "expired") {
    return (
      <div className="card" style={{ padding: 28, background: "var(--amber-100)", borderColor: "var(--amber)" }}>
        <div className="row between center" style={{ marginBottom: 16 }}>
          <span className="pill pill-amber pill-dot">Expired</span>
          <span style={{ fontFamily: "var(--font-mono)", fontSize: 12, color: "#92400E" }}>Ended Feb 04, 2025</span>
        </div>
        <h2 style={{ fontSize: 28 }}>Your enrolment has expired.</h2>
        <p style={{ marginTop: 8, color: "#78350F", maxWidth: 480 }}>Recordings are still visible for the next 30 days. Renew to keep attending live classes and accessing new uploads.</p>
        <div className="row gap-12" style={{ marginTop: 20 }}>
          <button className="btn btn-primary" onClick={() => go("/signup")}>Renew enrolment</button>
          <button className="btn btn-secondary" onClick={() => go("/dashboard/recordings")}>View recordings</button>
        </div>
      </div>
    );
  }
  // not enrolled
  return (
    <div className="card" style={{ padding: 28, borderStyle: "dashed", borderWidth: 1, borderColor: "var(--line)" }}>
      <span className="pill pill-gray">Not enrolled</span>
      <h2 style={{ marginTop: 12, fontSize: 28 }}>You're not enrolled yet.</h2>
      <p className="muted" style={{ marginTop: 8, maxWidth: 480 }}>Enrol in the Aug 2025 batch to unlock live classes and recordings. ₹1800/month — cancel anytime.</p>
      <div className="row gap-12" style={{ marginTop: 20 }}>
        <button className="btn btn-primary" onClick={() => go("/signup")}>Enrol now <Icon name="arrow-right" size={16}/></button>
        <button className="btn btn-secondary" onClick={() => go("/preview")}>Watch free preview</button>
      </div>
    </div>
  );
};

const NextClassCard = ({ enrollment, go }) => {
  const locked = enrollment !== "enrolled";
  return (
    <div className="card" style={{ padding: 24, position: "relative", overflow: "hidden" }}>
      <div className="eyebrow">Next class</div>
      <h3 style={{ marginTop: 12 }}>Polymorphism</h3>
      <div className="row gap-16" style={{ marginTop: 16, fontSize: 14, color: "var(--ink-2)" }}>
        <span><Icon name="calendar" size={14}/> Mon, Aug 18</span>
        <span><Icon name="clock" size={14}/> 7:00 PM IST</span>
      </div>
      <div style={{ marginTop: 8, fontSize: 14, color: "var(--ink-3)" }}>
        Chapter 2 · Object Oriented Programming
      </div>
      <button className="btn btn-primary btn-block" style={{ marginTop: 20 }} disabled={locked} onClick={() => go("/dashboard/live")}>
        <Icon name="video" size={16}/> {locked ? "Enrol to join" : "Join via Zoom"}
      </button>
      {locked && (
        <div style={{ marginTop: 12, fontSize: 12, color: "var(--ink-3)", textAlign: "center" }}>
          <Icon name="lock" size={12}/> Available after enrolment
        </div>
      )}
    </div>
  );
};

// ───── Live page ─────────────────────────────────────────────
function DashboardLive({ go, isLoggedIn, enrollment, setTweak }) {
  const locked = enrollment !== "enrolled";
  return (
    <DashShell go={go} current="live" isLoggedIn={isLoggedIn} setTweak={setTweak}>
      <div className="page-head">
        <div>
          <div className="eyebrow">Live class</div>
          <h2 style={{ marginTop: 8 }}>ISC-XII Aug 2025</h2>
        </div>
        <div className="row gap-12">
          <span className="pill pill-mono pill-green pill-dot">Active batch</span>
        </div>
      </div>

      <div className="locked-wrap" style={{ minHeight: 600 }}>
        <div className={locked ? "locked-blur" : ""}>
          <div style={{ display: "grid", gridTemplateColumns: "1.4fr 1fr", gap: 24, alignItems: "flex-start" }}>
            <div className="card" style={{ padding: 32 }}>
              <div className="row between center" style={{ marginBottom: 24 }}>
                <h3>Next live session</h3>
                <span className="pill pill-mono pill-gray">In 3 days</span>
              </div>

              <div style={{ padding: 24, background: "var(--paper)", borderRadius: 16 }}>
                <div className="row gap-16 center">
                  <div style={{ width: 64, height: 64, borderRadius: 12, background: "var(--blue)", color: "#fff", display: "flex", flexDirection: "column", alignItems: "center", justifyContent: "center" }}>
                    <div style={{ fontFamily: "var(--font-mono)", fontSize: 10, letterSpacing: "0.08em", textTransform: "uppercase" }}>Aug</div>
                    <div style={{ fontFamily: "var(--font-mono)", fontSize: 22, fontWeight: 600 }}>18</div>
                  </div>
                  <div>
                    <h4>Polymorphism — OOP</h4>
                    <div style={{ fontSize: 14, color: "var(--ink-2)", marginTop: 4 }}>Monday · 7:00–8:30 PM IST</div>
                  </div>
                </div>
                <button className="btn btn-primary btn-lg btn-block" style={{ marginTop: 24 }}>
                  <Icon name="video" size={18}/> Join via Google Meet
                </button>
                <div style={{ marginTop: 12, fontFamily: "var(--font-mono)", fontSize: 12, color: "var(--ink-3)", textAlign: "center", wordBreak: "break-all" }}>meet.google.com/abc-xyz-pqr</div>
              </div>

              <div style={{ marginTop: 32 }}>
                <h4 style={{ marginBottom: 16 }}>Weekly schedule</h4>
                <div className="col gap-8">
                  {[
                    { day: "Monday",    time: "7:00 – 8:30 PM IST", note: "Theory + worked examples" },
                    { day: "Thursday",  time: "7:00 – 8:30 PM IST", note: "Practice + doubts" },
                    { day: "Saturday",  time: "10:00 – 11:00 AM",   note: "Office hours (optional)" },
                  ].map((s) => (
                    <div key={s.day} className="row between center" style={{ padding: "14px 16px", borderRadius: 12, background: "var(--paper)" }}>
                      <div>
                        <div style={{ fontWeight: 600 }}>{s.day}</div>
                        <div style={{ fontSize: 13, color: "var(--ink-3)", marginTop: 2 }}>{s.note}</div>
                      </div>
                      <div style={{ fontFamily: "var(--font-mono)", fontSize: 13, color: "var(--ink-2)" }}>{s.time}</div>
                    </div>
                  ))}
                </div>
              </div>
            </div>

            <div className="col gap-16">
              <div className="card">
                <h4>Course details</h4>
                <div style={{ marginTop: 16, display: "grid", gridTemplateColumns: "1fr 1fr", gap: 16 }}>
                  <KV label="Batch"        value="ISC-XII Aug 2025" />
                  <KV label="Students"     value="38 enrolled" />
                  <KV label="Started"      value="Aug 04, 2025" />
                  <KV label="Ends"         value="Feb 04, 2026" />
                </div>
              </div>

              <div className="card" style={{ padding: 20, background: "var(--blue-50)", borderColor: "var(--blue-100)" }}>
                <div className="row gap-12 center" style={{ alignItems: "flex-start" }}>
                  <span style={{ color: "var(--blue)", marginTop: 2 }}><Icon name="alert" size={20}/></span>
                  <div>
                    <div style={{ fontWeight: 600, color: "var(--ink)" }}>Missed a class?</div>
                    <p style={{ marginTop: 6, fontSize: 13, color: "var(--ink-2)" }}>The recording will be uploaded within 24 hours. Find it in the Recordings tab.</p>
                    <button className="btn btn-secondary btn-sm" style={{ marginTop: 12 }} onClick={() => go("/dashboard/recordings")}>
                      Go to recordings <Icon name="arrow-right" size={12}/>
                    </button>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
        {locked && (
          <LockedOverlay
            title="Enrol to join live classes"
            subtitle="The Google Meet link is hidden until your enrolment is active. Enrol once — recordings unlock too."
            onEnrol={() => go("/signup")}
          />
        )}
      </div>
    </DashShell>
  );
}

// ───── Recordings list ───────────────────────────────────────
function DashboardRecordings({ go, isLoggedIn, enrollment, setTweak }) {
  const locked = enrollment !== "enrolled" && enrollment !== "expired";
  // group by chapter
  const grouped = useMemo(() => {
    const m = new Map();
    RECORDINGS.forEach(r => { if (!m.has(r.chapter)) m.set(r.chapter, []); m.get(r.chapter).push(r); });
    return Array.from(m.entries());
  }, []);

  return (
    <DashShell go={go} current="recordings" isLoggedIn={isLoggedIn} setTweak={setTweak}>
      <div className="page-head">
        <div>
          <div className="eyebrow">Recordings · ISC-XII Aug 2025</div>
          <h2 style={{ marginTop: 8 }}>All sessions</h2>
        </div>
        <div className="row gap-12 center">
          <div style={{ position: "relative" }}>
            <Icon name="search" size={14} style={{ position: "absolute", left: 12, top: "50%", transform: "translateY(-50%)", color: "var(--ink-3)" }}/>
            <input className="input" placeholder="Search recordings" style={{ paddingLeft: 36, width: 240 }} />
          </div>
          <button className="btn btn-secondary btn-sm"><Icon name="filter" size={14}/> Filter</button>
        </div>
      </div>

      {enrollment === "expired" && (
        <div className="card" style={{ marginBottom: 24, padding: 16, background: "var(--amber-100)", borderColor: "var(--amber)" }}>
          <div className="row between center" style={{ flexWrap: "wrap", gap: 12 }}>
            <div className="row gap-12 center">
              <Icon name="alert" size={18}/>
              <div>
                <div style={{ fontWeight: 600 }}>Your enrolment has expired</div>
                <div style={{ fontSize: 13, color: "#78350F" }}>You can keep watching recordings for 30 days. Renew to access new uploads.</div>
              </div>
            </div>
            <button className="btn btn-primary btn-sm" onClick={() => go("/signup")}>Renew enrolment</button>
          </div>
        </div>
      )}

      <div className="locked-wrap" style={{ minHeight: 600 }}>
        <div className={locked ? "locked-blur" : ""}>
          <div className="col gap-32">
            {grouped.map(([chapter, items]) => (
              <div key={chapter}>
                <div className="row between center" style={{ marginBottom: 16 }}>
                  <h3>{chapter}</h3>
                  <span style={{ fontSize: 13, color: "var(--ink-3)" }}>{items.length} recording{items.length > 1 ? "s" : ""}</span>
                </div>
                <div style={{ display: "grid", gridTemplateColumns: "repeat(auto-fill, minmax(280px, 1fr))", gap: 16 }}>
                  {items.map((r) => (
                    <button key={r.id} onClick={() => go(`/dashboard/recordings/${r.id}`)}
                      style={{ all: "unset", cursor: "pointer", display: "block" }}>
                      <div className="card" style={{ padding: 0, overflow: "hidden", transition: "box-shadow .15s" }}
                           onMouseEnter={e => e.currentTarget.style.boxShadow = "var(--shadow-lg)"}
                           onMouseLeave={e => e.currentTarget.style.boxShadow = ""}>
                        <div className="video-frame" style={{ aspectRatio: "16/9", borderRadius: 0 }}>
                          <button className="play-btn" tabIndex={-1} style={{ width: 56, height: 56, fontSize: 20 }}><Icon name="play" size={18}/></button>
                          <div className="video-meta"><span style={{ background: "rgba(0,0,0,.6)", padding: "3px 8px", borderRadius: 4, fontSize: 11 }}>{r.duration}</span></div>
                        </div>
                        <div style={{ padding: 16 }}>
                          <div style={{ fontWeight: 600, fontSize: 15 }}>{r.title}</div>
                          <div className="row between" style={{ marginTop: 8, fontSize: 12, color: "var(--ink-3)", fontFamily: "var(--font-mono)" }}>
                            <span>{r.date}</span>
                            <span>{r.duration}</span>
                          </div>
                        </div>
                      </div>
                    </button>
                  ))}
                </div>
              </div>
            ))}
          </div>
        </div>
        {locked && (
          <LockedOverlay
            title="Enrol to access recordings"
            subtitle="Every recording from ISC-XII Aug 2025 unlocks the moment your payment goes through."
            onEnrol={() => go("/signup")}
          />
        )}
      </div>
    </DashShell>
  );
}

// ───── Recording player ──────────────────────────────────────
function DashboardRecordingPlayer({ go, parts, isLoggedIn, enrollment, setTweak }) {
  const id = parts[2];
  const idx = RECORDINGS.findIndex(r => r.id === id);
  const rec = RECORDINGS[idx] || RECORDINGS[0];
  const prev = idx > 0 ? RECORDINGS[idx - 1] : null;
  const next = idx < RECORDINGS.length - 1 ? RECORDINGS[idx + 1] : null;

  return (
    <DashShell go={go} current="recordings" isLoggedIn={isLoggedIn} setTweak={setTweak}>
      <button className="btn btn-ghost btn-sm" style={{ marginBottom: 24, padding: "0 8px", height: 32 }} onClick={() => go("/dashboard/recordings")}>
        <Icon name="arrow-left" size={14}/> All recordings
      </button>

      <div className="video-frame" style={{ aspectRatio: "16/9" }}>
        <image-slot id={`rec-${rec.id}`} placeholder="Recording — drop video poster" shape="rect" style={{ position: "absolute", inset: 0, width: "100%", height: "100%" }}></image-slot>
        <button className="play-btn" style={{ position: "relative" }}><Icon name="play" size={28} /></button>
        <div style={{ position: "absolute", left: 24, right: 24, bottom: 24, color: "#fff", display: "flex", alignItems: "center", gap: 12, fontFamily: "var(--font-mono)", fontSize: 13 }}>
          <span>0:00</span>
          <div style={{ flex: 1, height: 4, background: "rgba(255,255,255,.2)", borderRadius: 2, position: "relative" }}>
            <div style={{ position: "absolute", left: 0, top: 0, bottom: 0, width: "32%", background: "#fff", borderRadius: 2 }}/>
          </div>
          <span>{rec.duration}</span>
        </div>
      </div>

      <div style={{ marginTop: 24, display: "grid", gridTemplateColumns: "1fr 280px", gap: 32, alignItems: "flex-start" }}>
        <div>
          <div className="row gap-8 center" style={{ marginBottom: 12 }}>
            <span className="pill pill-mono">{rec.chapter}</span>
            <span className="pill pill-mono pill-gray">{rec.date}</span>
            <span className="pill pill-mono pill-gray">{rec.duration}</span>
          </div>
          <h2 style={{ fontSize: 32 }}>{rec.title}</h2>
          <p className="muted" style={{ marginTop: 12, fontSize: 16, maxWidth: 640 }}>
            Live session recorded on {rec.date}. Covers the core concepts of {rec.chapter.toLowerCase()} with worked Python examples and a Q&A at the end.
          </p>

          <div className="row between center" style={{ marginTop: 40, paddingTop: 24, borderTop: "1px solid var(--line)" }}>
            <button className="btn btn-secondary" disabled={!prev} onClick={() => prev && go(`/dashboard/recordings/${prev.id}`)}>
              <Icon name="arrow-left" size={14}/> Previous
            </button>
            <button className="btn btn-primary" disabled={!next} onClick={() => next && go(`/dashboard/recordings/${next.id}`)}>
              Next recording <Icon name="arrow-right" size={14}/>
            </button>
          </div>
        </div>

        <aside>
          <div style={{ fontSize: 13, color: "var(--ink-3)", letterSpacing: "0.04em" }}>Up next in chapter</div>
          <div style={{ marginTop: 12, display: "flex", flexDirection: "column", gap: 8 }}>
            {RECORDINGS.filter(r => r.chapter === rec.chapter && r.id !== rec.id).slice(0, 4).map((r) => (
              <button key={r.id} onClick={() => go(`/dashboard/recordings/${r.id}`)}
                style={{ all: "unset", cursor: "pointer", display: "flex", alignItems: "center", gap: 12, padding: 10, borderRadius: 8 }}
                onMouseEnter={e => e.currentTarget.style.background = "var(--paper)"}
                onMouseLeave={e => e.currentTarget.style.background = ""}>
                <span style={{ width: 36, height: 36, borderRadius: 6, background: "var(--blue-50)", color: "var(--blue)", display: "flex", alignItems: "center", justifyContent: "center", flexShrink: 0 }}>
                  <Icon name="play" size={14}/>
                </span>
                <div style={{ flex: 1, minWidth: 0 }}>
                  <div style={{ fontSize: 13, fontWeight: 500, color: "var(--ink)", whiteSpace: "nowrap", overflow: "hidden", textOverflow: "ellipsis" }}>{r.title}</div>
                  <div style={{ fontSize: 11, color: "var(--ink-3)", fontFamily: "var(--font-mono)" }}>{r.date} · {r.duration}</div>
                </div>
              </button>
            ))}
          </div>
        </aside>
      </div>
    </DashShell>
  );
}

// ───── Profile ───────────────────────────────────────────────
function DashboardProfile({ go, isLoggedIn, setTweak }) {
  const [edit, setEdit] = useS_D(false);
  const [form, setForm] = useS_D({ name: "Ananya Krishnan", phone: "+91 98765 43210", cls: "12", board: "ISC" });
  const u = (k, v) => setForm(p => ({ ...p, [k]: v }));

  return (
    <DashShell go={go} current="profile" isLoggedIn={isLoggedIn} setTweak={setTweak}>
      <div className="page-head">
        <div>
          <div className="eyebrow">Profile</div>
          <h2 style={{ marginTop: 8 }}>Your details</h2>
        </div>
        {!edit && <button className="btn btn-secondary" onClick={() => setEdit(true)}><Icon name="edit" size={14}/> Edit</button>}
      </div>

      <div style={{ display: "grid", gridTemplateColumns: "1fr 320px", gap: 24, alignItems: "flex-start" }}>
        <div className="card" style={{ padding: 32 }}>
          <div className="row gap-24 center" style={{ marginBottom: 32 }}>
            <image-slot id="profile-photo" placeholder="Profile photo" shape="circle" style={{ width: 96, height: 96 }}></image-slot>
            <div>
              <h3>{form.name}</h3>
              <div className="row gap-8" style={{ marginTop: 8 }}>
                <span className="pill pill-mono">{form.board}-{form.cls === "10" ? "X" : "XII"}</span>
                <span className="pill pill-mono pill-gray">Class {form.cls}</span>
              </div>
            </div>
          </div>

          <div style={{ display: "grid", gridTemplateColumns: "1fr 1fr", gap: 20 }}>
            <Field label="Full name" disabled={!edit} value={form.name} onChange={v => u("name", v)} />
            <Field label="Email" readOnly value="ananya.k@gmail.com" hint="Email can't be changed — contact support" />
            <Field label="Phone" disabled={!edit} value={form.phone} onChange={v => u("phone", v)} />
            <SelectField label="Course" disabled={!edit} value={form.cls} onChange={v => u("cls", v === "10" ? "10" : "12") || u("board", v === "10" ? "ICSE" : "ISC")}
              options={[["10","Class 10 (ICSE) — Computer Applications"],["12","Class 12 (ISC) — Computer Science"]]} />
          </div>

          {edit && (
            <div className="row gap-12" style={{ marginTop: 32 }}>
              <button className="btn btn-primary" onClick={() => setEdit(false)}>Save changes</button>
              <button className="btn btn-ghost" onClick={() => setEdit(false)}>Cancel</button>
            </div>
          )}
        </div>

        <div className="col gap-16">
          <div className="card">
            <h4>Enrolment</h4>
            <div style={{ marginTop: 12, fontFamily: "var(--font-mono)", fontSize: 12, color: "var(--ink-3)" }}>ISC-XII AUG 2025</div>
            <span className="pill pill-green pill-dot" style={{ marginTop: 12 }}>Active</span>
            <div style={{ marginTop: 16, fontSize: 13, color: "var(--ink-2)" }}>Expires Feb 04, 2026</div>
          </div>
          <div className="card">
            <h4>Account</h4>
            <button className="nav-link" style={{ marginTop: 12, padding: "10px 0" }}><Icon name="lock" size={14}/> Change password</button>
            <button className="nav-link" style={{ padding: "10px 0" }}><Icon name="download" size={14}/> Download invoices</button>
            <button className="nav-link" style={{ padding: "10px 0", color: "var(--red)" }}><Icon name="trash" size={14}/> Delete account</button>
          </div>
        </div>
      </div>
    </DashShell>
  );
}

const Field = ({ label, value, onChange, disabled, readOnly, hint }) => (
  <div className="field">
    <label className="field-label">{label}</label>
    <input className="input" value={value} onChange={e => onChange && onChange(e.target.value)} disabled={disabled} readOnly={readOnly}
      style={{ background: (disabled || readOnly) ? "var(--paper)" : "var(--white)", color: (disabled || readOnly) ? "var(--ink-2)" : "var(--ink)" }} />
    {hint && <div style={{ fontSize: 12, color: "var(--ink-3)" }}>{hint}</div>}
  </div>
);
const SelectField = ({ label, value, onChange, disabled, options }) => (
  <div className="field">
    <label className="field-label">{label}</label>
    <select className="select" value={value} onChange={e => onChange(e.target.value)} disabled={disabled}
      style={{ background: disabled ? "var(--paper)" : "var(--white)", color: disabled ? "var(--ink-2)" : "var(--ink)" }}>
      {options.map(([v, l]) => <option key={v} value={v}>{l}</option>)}
    </select>
  </div>
);

const EmptyOrLockedRecordings = ({ enrollment, go, compact }) => (
  <div style={{ padding: compact ? 40 : 80, textAlign: "center", color: "var(--ink-3)" }}>
    <Icon name="lock" size={28}/>
    <div style={{ marginTop: 12, fontWeight: 600, color: "var(--ink)" }}>Recordings locked</div>
    <p style={{ marginTop: 6, fontSize: 14 }}>Enrol to unlock all recordings for ISC-XII Aug 2025.</p>
    <button className="btn btn-primary btn-sm" style={{ marginTop: 16 }} onClick={() => go("/signup")}>Enrol now</button>
  </div>
);

const { useMemo } = React;

Object.assign(window, {
  DashboardHome, DashboardLive, DashboardRecordings,
  DashboardRecordingPlayer, DashboardProfile,
});
