/* Ritesh Shukla — portfolio site sections.
   Composes the design-system components. Exports to window for index.html. */

const { Button, Badge, Card, PackageCard, Terminal, Equalizer, Wordmark } =
  window.RiteshShuklaDesignSystem_fd8f35;

/* ---- Lucide icon helper ---- */
function Icon({ name, size = 18, color, style }) {
  return <i data-lucide={name} style={{ width: size, height: size, display: "inline-flex", color, ...style }} />;
}
function useLucide(dep) {
  React.useEffect(() => { if (window.lucide) window.lucide.createIcons(); }, [dep]);
}

/* ---- Scroll reveal (IntersectionObserver driven from index.html) ---- */
function Reveal({ children, as = "div", style = {}, className = "", delay = 0 }) {
  const Comp = as;
  return (
    <Comp className={("reveal " + className).trim()} style={{ "--reveal-delay": delay + "ms", ...style }}>
      {children}
    </Comp>
  );
}

/* ===================== DATA ===================== */
const PACKAGES = [
  { name: "react-native-nitro-fetch", description: "A drop-in fetch replacement. ~28% faster API calls via Cronet and native networking — change one import.", version: "0.16.0", href: "https://github.com/riteshshukla04/react-native-nitro-fetch" },
  { name: "react-native-nitro-player", description: "A powerful audio player. Zero bridge overhead, native Kotlin & Swift, CarPlay & Android Auto, offline queue.", version: "0.8.0", href: "https://github.com/riteshshukla04/react-native-nitro-player" },
  { name: "react-native-superconfig", description: "Super-fast remote config backed by native storage. Read flags synchronously on the JS thread.", version: "1.0.1", href: "https://github.com/riteshshukla04/react-native-superconfig" },
  { name: "react-native-nitro-ota", description: "Over-the-air updates built on Nitro Modules. Ship JS without waiting on the store.", version: "0.4.2", href: "https://github.com/riteshshukla04/react-native-nitro-ota" },
];

const EXPERIENCE = [
  { role: "Founder", org: "Dark", period: "Now", note: "Building Dark." },
  { role: "React Native Engineer", org: "Margelo", period: "Now", note: "High-performance React Native for media-heavy apps. JSI, C++, and native modules done right." },
  { role: "React Native Engineer", org: "Bajaj Finserv Health", period: "Ex", note: "I Made all BFHL apps faster." },
  { role: "Open Source", org: "React Native Core", period: "Ongoing", note: "Contributor to React Native Core and a stack of ecosystem libraries." },
];

const SOCIALS = [
  ["github", "https://github.com/riteshshukla04"],
  ["twitter", "https://x.com/RiteshRk14"],
  ["linkedin", "https://www.linkedin.com/in/ritesh-shukla-19a154208/"],
  ["mail", "mailto:riteshshukla2381@gmail.com"],
];

/* ===================== NAV ===================== */
function TopNav({ theme, onToggleTheme }) {
  useLucide(theme);
  const links = [["Open Source", "#work"], ["Experience", "#experience"], ["About", "#about"]];
  return (
    <header style={{ position: "sticky", top: 0, zIndex: 50, backdropFilter: "blur(var(--blur-md))", background: "color-mix(in srgb, var(--bg) 72%, transparent)", borderBottom: "1px solid var(--border-subtle)" }}>
      <div className="container" style={{ display: "flex", alignItems: "center", justifyContent: "space-between", height: 68 }}>
        <a href="#top" style={{ textDecoration: "none" }}><Wordmark variant="dev" size={19} /></a>
        <nav style={{ display: "flex", alignItems: "center", gap: 28 }} className="nav-links">
          {links.map(([label, href]) => (
            <a key={href} href={href} style={{ font: "var(--text-ui)", color: "var(--text-secondary)" }}
               onMouseEnter={e => e.currentTarget.style.color = "var(--text-primary)"}
               onMouseLeave={e => e.currentTarget.style.color = "var(--text-secondary)"}>{label}</a>
          ))}
        </nav>
        <div style={{ display: "flex", alignItems: "center", gap: 12 }}>
          <button onClick={onToggleTheme} aria-label="Toggle theme" style={{ width: 38, height: 38, display: "grid", placeItems: "center", borderRadius: "var(--radius-md)", background: "transparent", border: "1px solid var(--border)", color: "var(--text-secondary)", cursor: "pointer" }}>
            <Icon name={theme === "dark" ? "sun" : "moon"} size={17} />
          </button>
          <Button variant="ghost" size="sm" as="a" href="https://github.com/riteshshukla04" iconRight={<Icon name="arrow-up-right" size={15} />}>GitHub</Button>
        </div>
      </div>
    </header>
  );
}

/* ===================== HERO ===================== */
function Hero() {
  useLucide();
  return (
    <section id="top" style={{ position: "relative", overflow: "hidden" }}>
      <div className="hero-glow" aria-hidden style={{ position: "absolute", inset: 0, pointerEvents: "none" }} />
      <div className="hero-grid" aria-hidden />
      <div className="container" style={{ position: "relative", display: "grid", gridTemplateColumns: "1.15fr 0.85fr", gap: 56, alignItems: "center", paddingTop: 100, paddingBottom: 110 }}>
        <div>
          <div className="reveal" style={{ "--reveal-delay": "80ms", display: "flex", alignItems: "center", gap: 14, marginBottom: 22 }}>
            <Badge tone="signal" mono>react-native engineer</Badge>
            <span style={{ font: "var(--text-label)", color: "var(--text-muted)" }}>Pune, IN · UTC+5:30</span>
          </div>
          <h1 className="reveal" style={{ "--reveal-delay": "180ms", font: "var(--weight-bold) var(--text-5xl)/0.98 var(--font-display)", letterSpacing: "var(--tracking-tighter)", color: "var(--text-primary)" }}>
            I make React Native<br /><span className="accent-sweep">stupid fast.</span><span className="caret" aria-hidden />
          </h1>
          <p className="reveal" style={{ "--reveal-delay": "300ms", font: "var(--text-lead)", color: "var(--text-secondary)", maxWidth: "46ch", marginTop: 22 }}>
            I code in React Native. I founded Dark and I work at Margelo.
          </p>
          <div className="reveal" style={{ "--reveal-delay": "400ms", display: "flex", gap: 14, marginTop: 30, flexWrap: "wrap" }}>
            <Button variant="signal" size="lg" as="a" href="#work" iconRight={<Icon name="arrow-right" size={18} />}>See what I build</Button>
            <Button variant="ghost" size="lg" as="a" href="#about">More about me</Button>
          </div>
          <div className="reveal" style={{ "--reveal-delay": "500ms", display: "flex", alignItems: "center", gap: 18, marginTop: 36 }}>
            <span style={{ font: "var(--text-label)", color: "var(--text-muted)" }}>Currently</span>
            <Equalizer bars={5} height={22} barWidth={4} />
            <span style={{ font: "var(--text-sm)", color: "var(--text-secondary)" }}>Margelo · founder of Dark</span>
          </div>
        </div>
        <div id="hero-terminal" className="reveal" style={{ "--reveal-delay": "360ms" }}>
          <Terminal title="~/ritesh — zsh" lines={[
            { type: "prompt",  text: "whoami" },
            { type: "success", text: "ritesh shukla · react-native engineer" },
            { type: "comment", text: "# guy with earphones 🎧" },
          ]} />
        </div>
      </div>
    </section>
  );
}

/* ===================== OPEN SOURCE ===================== */
function Work() {
  // Auto-fetch the latest published version for each package from the npm
  // registry (CORS-enabled). Falls back to the bundled version on failure.
  const [versions, setVersions] = React.useState({});
  React.useEffect(() => {
    let alive = true;
    PACKAGES.forEach((p) => {
      fetch("https://registry.npmjs.org/" + encodeURIComponent(p.name) + "/latest")
        .then((r) => (r.ok ? r.json() : null))
        .then((d) => {
          if (alive && d && d.version) setVersions((v) => ({ ...v, [p.name]: d.version }));
        })
        .catch(() => {});
    });
    return () => { alive = false; };
  }, []);
  return (
    <section id="work" className="container" style={{ paddingTop: "var(--section-y)", paddingBottom: "var(--section-y)" }}>
      <Reveal><SectionHead eyebrow="// Open source" title="What I maintain" sub="The packages I'm proudest of — the ones that move the needle on performance and audio." /></Reveal>
      <div style={{ display: "grid", gridTemplateColumns: "repeat(auto-fill, minmax(330px, 1fr))", gap: 16 }}>
        {PACKAGES.map((p, i) => <Reveal key={p.name} delay={i * 80}><PackageCard {...p} version={versions[p.name] || p.version} /></Reveal>)}
      </div>
    </section>
  );
}

/* ===================== EXPERIENCE ===================== */
function Experience() {
  return (
    <section id="experience" style={{ background: "var(--bg-deep)", borderTop: "1px solid var(--border-subtle)", borderBottom: "1px solid var(--border-subtle)" }}>
      <div className="container" style={{ paddingTop: "var(--section-y)", paddingBottom: "var(--section-y)" }}>
        <Reveal><SectionHead eyebrow="// Experience" title="Where I've shipped" /></Reveal>
        <div style={{ display: "flex", flexDirection: "column" }}>
          {EXPERIENCE.map((e, i) => (
            <Reveal key={i} delay={i * 70} style={{ display: "grid", gridTemplateColumns: "120px 1fr", gap: 28, padding: "26px 0", borderTop: "1px solid var(--border-subtle)" }}>
              <div style={{ font: "var(--text-label)", color: "var(--accent)", textTransform: "uppercase", letterSpacing: "var(--tracking-caps)", paddingTop: 5 }}>{e.period}</div>
              <div>
                <div style={{ display: "flex", alignItems: "baseline", gap: 12, flexWrap: "wrap" }}>
                  <h3 style={{ font: "var(--text-title)", color: "var(--text-primary)" }}>{e.role}</h3>
                  <span style={{ font: "var(--text-sm)", color: "var(--text-secondary)" }}>· {e.org}</span>
                </div>
                <p style={{ font: "var(--text-body)", color: "var(--text-secondary)", marginTop: 8, maxWidth: "62ch" }}>{e.note}</p>
              </div>
            </Reveal>
          ))}
        </div>
      </div>
    </section>
  );
}

/* ===================== ABOUT ===================== */
function About() {
  useLucide();
  const facts = [
    ["terminal", "If it has a CLI, I'll probably love it"],
    ["headphones", "Guy with earphones — audio is my thing"],
    ["smile", "I love memes (probably too much)"],
    ["zap", "Perf over ceremony. One-line wins beat refactors"],
    ["sparkles", "Starter Pokémon: Bulbasaur"],
  ];
  return (
    <section id="about" style={{ background: "var(--bg-deep)", borderTop: "1px solid var(--border-subtle)" }}>
      <div className="container" style={{ paddingTop: "var(--section-y)", paddingBottom: "var(--section-y)", display: "grid", gridTemplateColumns: "1fr 1fr", gap: 56, alignItems: "center" }}>
        <Reveal>
          <SectionHead eyebrow="// About" title="Engineer, not influencer" />
          <p style={{ font: "var(--text-lead)", color: "var(--text-secondary)", marginBottom: 16 }}>
            You don't need re-render hacks and useEffect cleanups to make a React Native app fast. You need to know where the bridge actually hurts.
          </p>
          <p style={{ font: "var(--text-body)", color: "var(--text-secondary)", maxWidth: "58ch" }}>
            I work in the native layer — Kotlin, Swift, C++, JSI — and wrap it in clean JS APIs so product teams move fast without paying for it later.
          </p>
        </Reveal>
        <div style={{ display: "grid", gridTemplateColumns: "1fr 1fr", gap: 14 }}>
          {facts.map(([icon, text], i) => (
            <Reveal key={i} delay={i * 70} style={{ gridColumn: i === facts.length - 1 && facts.length % 2 === 1 ? "1 / -1" : undefined }}>
              <Card padding="var(--space-5)" style={{ display: "flex", flexDirection: "column", gap: 14, height: "100%" }}>
                <span style={{ width: 40, height: 40, display: "grid", placeItems: "center", borderRadius: "var(--radius-md)", background: "var(--accent-soft)", color: "var(--accent)" }}>
                  <Icon name={icon} size={20} />
                </span>
                <span style={{ font: "var(--text-ui)", color: "var(--text-primary)", lineHeight: 1.4 }}>{text}</span>
              </Card>
            </Reveal>
          ))}
        </div>
      </div>
    </section>
  );
}

/* ===================== FOOTER ===================== */
function Footer() {
  useLucide();
  return (
    <footer style={{ borderTop: "1px solid var(--border-subtle)", background: "var(--bg-deep)" }}>
      <div className="container" style={{ paddingTop: 44, paddingBottom: 44, display: "flex", alignItems: "center", justifyContent: "space-between", flexWrap: "wrap", gap: 20 }}>
        <div style={{ display: "flex", flexDirection: "column", gap: 10 }}>
          <Wordmark variant="full" size={18} />
          <span style={{ font: "var(--text-label)", color: "var(--text-muted)" }}>Vibe coded this site — ignore the cringeness · Pune, IN</span>
        </div>
        <div style={{ display: "flex", alignItems: "center", gap: 18 }}>
          {SOCIALS.map(([i, h]) => (
            <a key={i} href={h} style={{ color: "var(--text-muted)", display: "inline-flex" }}
               onMouseEnter={e => e.currentTarget.style.color = "var(--accent)"}
               onMouseLeave={e => e.currentTarget.style.color = "var(--text-muted)"}><Icon name={i} size={19} /></a>
          ))}
        </div>
      </div>
    </footer>
  );
}

/* ---- shared section header ---- */
function SectionHead({ eyebrow, title, sub, noMargin }) {
  return (
    <div style={{ marginBottom: noMargin ? 0 : 30 }}>
      <div style={{ font: "var(--text-label)", color: "var(--accent)", textTransform: "uppercase", letterSpacing: "var(--tracking-caps)", marginBottom: 14 }}>{eyebrow}</div>
      <h2 style={{ font: "var(--weight-bold) var(--text-3xl)/1.05 var(--font-display)", letterSpacing: "var(--tracking-tight)", color: "var(--text-primary)" }}>{title}</h2>
      {sub && <p style={{ font: "var(--text-lead)", color: "var(--text-secondary)", marginTop: 12, maxWidth: "52ch" }}>{sub}</p>}
    </div>
  );
}

Object.assign(window, { TopNav, Hero, Work, Experience, About, Footer });
