// Shared shell: Navbar (top) + Sidebar (left, collapsible, past reports)
const { useState } = React;

function StatusChip({ status }) {
  const styles = {
    generating: "bg-teal-500/15 text-teal-300 ring-1 ring-inset ring-teal-500/30",
    complete:   "bg-emerald-500/10 text-emerald-300 ring-1 ring-inset ring-emerald-500/25",
    draft:      "bg-white/5 text-ink-300 ring-1 ring-inset ring-white/10",
  };
  const labels = { generating: "Generating", complete: "Complete", draft: "Draft" };
  return (
    <span className={`mono inline-flex items-center gap-1 px-1.5 py-0.5 rounded text-[10px] tracking-tight ${styles[status]}`}>
      {status === "generating" && <span className="w-1 h-1 rounded-full bg-teal-300 animate-pulse"/>}
      {status === "complete" && <span className="w-1 h-1 rounded-full bg-emerald-300"/>}
      {status === "draft" && <span className="w-1 h-1 rounded-full bg-ink-300"/>}
      {labels[status]}
    </span>
  );
}

function Navbar({ onNewReport, currentTopic, step }) {
  const stepLabels = ["Topic", "Title", "Index", "Generation"];
  return (
    <header className="h-14 shrink-0 bg-white border-b border-ink-100 px-5 flex items-center sticky top-0 z-30">
      <div className="flex items-center gap-2.5">
        <Icons.Logo size={22}/>
        <div className="leading-none">
          <div className="text-[14px] font-semibold tracking-tight text-ink-900">IntegerMarket</div>
          <div className="mono text-[10px] text-ink-400 tracking-wide uppercase mt-0.5">Generation Console</div>
        </div>
      </div>

      {/* Breadcrumb / Step indicator */}
      <div className="ml-7 hidden md:flex items-center gap-1.5 text-[12px] text-ink-500">
        <span className="text-ink-400">Reports</span>
        <Icons.ChevronRight size={12} className="text-ink-300"/>
        <span className="text-ink-700 font-medium truncate max-w-[280px]">
          {currentTopic || "New Report"}
        </span>
        {step >= 0 && (
          <>
            <Icons.ChevronRight size={12} className="text-ink-300"/>
            <span className="mono text-[11px] text-ink-500">Step {step + 1} · {stepLabels[step]}</span>
          </>
        )}
      </div>

      <div className="ml-auto flex items-center gap-2">
        <button className="hidden md:flex items-center gap-2 h-8 px-2.5 rounded-md text-[12px] text-ink-500 hover:bg-ink-50">
          <Icons.Search size={14}/>
          <span>Search</span>
          <span className="mono text-[10px] px-1 py-0.5 rounded bg-ink-100 text-ink-500">⌘K</span>
        </button>
        <button className="h-8 w-8 grid place-items-center text-ink-500 hover:bg-ink-50 rounded-md" aria-label="Notifications">
          <Icons.Bell size={15}/>
        </button>
        <button
          onClick={onNewReport}
          className="h-8 inline-flex items-center gap-1.5 px-3 rounded-md bg-ink-900 text-white text-[12px] font-medium hover:bg-ink-800 transition-colors">
          <Icons.Plus size={14}/> New Report
        </button>
        <div className="ml-1 h-8 w-8 rounded-full bg-gradient-to-br from-teal-400 to-teal-600 grid place-items-center text-white text-[11px] font-semibold ring-2 ring-white">
          NS
        </div>
      </div>
    </header>
  );
}

function Sidebar({ reports, onPick, onNew, activeId, view, onChangeView }) {
  const [filter, setFilter] = useState("all");
  const [query, setQuery] = useState("");
  const counts = {
    all: reports.length,
    generating: reports.filter(r => r.status === "generating").length,
    complete: reports.filter(r => r.status === "complete").length,
    draft: reports.filter(r => r.status === "draft").length,
  };
  const filtered = reports
    .filter(r => filter === "all" || r.status === filter)
    .filter(r => r.title.toLowerCase().includes(query.toLowerCase()));

  return (
    <aside className="w-64 shrink-0 bg-ink-900 text-ink-100 border-r border-black/30 flex flex-col">
      <div className="p-3 pb-2">
        <button
          onClick={onNew}
          className="w-full h-9 inline-flex items-center justify-between px-3 rounded-md bg-teal-500 hover:bg-teal-400 text-white text-[12.5px] font-medium transition-colors shadow-sm">
          <span className="inline-flex items-center gap-2"><Icons.Plus size={14}/> New Report</span>
          <span className="mono text-[10px] opacity-80">⌘N</span>
        </button>
      </div>

      {/* Primary nav */}
      <nav className="px-2 pb-1">
        {[
          { id: "reports",  label: "Reports",         icon: <Icons.FileText size={14}/> },
          { id: "diagrams", label: "Diagram Manager", icon: <Icons.Image size={14}/> },
        ].map((item) => (
          <button
            key={item.id}
            onClick={() => onChangeView && onChangeView(item.id)}
            className={`w-full h-8 flex items-center gap-2.5 px-2.5 rounded-md text-[12.5px] transition-colors
              ${view === item.id ? "bg-white/10 text-white" : "text-ink-300 hover:text-white hover:bg-white/5"}`}>
            <span className={view === item.id ? "text-teal-300" : "text-ink-400"}>{item.icon}</span>
            <span>{item.label}</span>
          </button>
        ))}
      </nav>

      <div className="mx-3 my-2 h-px bg-white/5"/>

      <div className="px-3">
        <div className="relative">
          <Icons.Search size={13} className="absolute left-2.5 top-1/2 -translate-y-1/2 text-ink-400"/>
          <input
            value={query}
            onChange={(e) => setQuery(e.target.value)}
            placeholder="Search reports…"
            className="w-full h-8 bg-black/30 border border-white/5 rounded-md pl-7 pr-2 text-[12px] placeholder:text-ink-400 focus:outline-none focus:ring-1 focus:ring-teal-500/60"/>
        </div>
      </div>

      <div className="px-3 pt-3 pb-2 flex items-center gap-1">
        {[
          ["all", "All"],
          ["generating", "Active"],
          ["complete", "Done"],
          ["draft", "Drafts"],
        ].map(([k, label]) => (
          <button key={k} onClick={() => setFilter(k)}
            className={`h-6 px-2 rounded text-[11px] font-medium transition-colors ${filter === k ? "bg-white/10 text-white" : "text-ink-300 hover:text-white hover:bg-white/5"}`}>
            {label} <span className="mono opacity-60 ml-0.5">{counts[k]}</span>
          </button>
        ))}
      </div>

      <div className="mono text-[10px] uppercase tracking-wider text-ink-400 px-4 pt-2 pb-1">Recent</div>
      <div className="flex-1 overflow-y-auto dark-scroll px-2 pb-2">
        {filtered.map((r) => (
          <button
            key={r.id}
            onClick={() => onPick(r)}
            className={`group w-full text-left rounded-md px-2 py-2 mb-0.5 transition-colors ${activeId === r.id ? "bg-white/10" : "hover:bg-white/5"}`}>
            <div className="flex items-start gap-2">
              <Icons.FileText size={14} className="text-ink-400 mt-0.5 shrink-0 group-hover:text-teal-300"/>
              <div className="min-w-0 flex-1">
                <div className="text-[12.5px] text-ink-100 truncate leading-tight">{r.title}</div>
                <div className="mt-1 flex items-center justify-between gap-2">
                  <span className="mono text-[10px] text-ink-400">{r.date}</span>
                  <StatusChip status={r.status}/>
                </div>
                {r.status === "generating" && (
                  <div className="mt-1.5 h-1 bg-black/30 rounded-full overflow-hidden">
                    <div className="h-full bg-teal-400" style={{ width: `${(r.progress || 0) * 100}%` }}/>
                  </div>
                )}
              </div>
            </div>
          </button>
        ))}
      </div>

      <div className="p-3 border-t border-white/5 flex items-center gap-2">
        <div className="h-7 w-7 rounded-full bg-gradient-to-br from-teal-400 to-teal-600 grid place-items-center text-white text-[10px] font-semibold">NS</div>
        <div className="min-w-0 flex-1">
          <div className="text-[12px] text-ink-100 truncate">Nadia Shah</div>
          <div className="mono text-[10px] text-ink-400 truncate">Acme Strategy · Pro</div>
        </div>
        <button className="text-ink-400 hover:text-white"><Icons.Settings size={14}/></button>
      </div>
    </aside>
  );
}

// Stepper across the top of the canvas
function Stepper({ step }) {
  const steps = [
    { label: "Enter Topic", sub: "Describe market" },
    { label: "Select Title", sub: "Pick angle" },
    { label: "Review Index", sub: "Confirm scope" },
    { label: "Generation",  sub: "Live progress" },
  ];
  return (
    <div className="flex items-center gap-0 px-8 pt-6 pb-4">
      {steps.map((s, i) => {
        const state = i < step ? "done" : i === step ? "active" : "todo";
        return (
          <React.Fragment key={i}>
            <div className="flex items-center gap-2.5">
              <div className={`step-dot h-7 w-7 rounded-full grid place-items-center text-[11px] mono font-semibold
                ${state === "done"   ? "bg-teal-500 text-white" : ""}
                ${state === "active" ? "bg-teal-500 text-white pulse-ring" : ""}
                ${state === "todo"   ? "bg-white text-ink-400 ring-1 ring-ink-200" : ""}`}>
                {state === "done" ? <Icons.Check size={13}/> : (i + 1)}
              </div>
              <div className="leading-tight">
                <div className={`text-[12.5px] font-medium ${state === "todo" ? "text-ink-400" : "text-ink-800"}`}>{s.label}</div>
                <div className="mono text-[10px] text-ink-400 uppercase tracking-wider">{s.sub}</div>
              </div>
            </div>
            {i < steps.length - 1 && (
              <div className="flex-1 mx-4 h-px relative">
                <div className="absolute inset-0 bg-ink-200"/>
                <div className={`absolute inset-y-0 left-0 bg-teal-500 transition-all`}
                  style={{ width: i < step ? "100%" : "0%" }}/>
              </div>
            )}
          </React.Fragment>
        );
      })}
    </div>
  );
}

window.Shell = { Navbar, Sidebar, Stepper, StatusChip };
