// Dashboard.jsx — Overview page composition: metrics, sparkline, theme switcher, activity.
const THEMES = [
  ["neutral",  "0 0% 32%"],
  ["trust",    "210 18% 38%"],
  ["energy",   "28 20% 36%"],
  ["harmony",  "170 16% 36%"],
  ["spirit",   "265 16% 40%"],
  ["urgency",  "8 18% 38%"],
];

function Sparkline({ points = [], color = "var(--primary)" }) {
  const w = 320, h = 60;
  if (!points.length) return null;
  const min = Math.min(...points), max = Math.max(...points);
  const norm = (v) => h - 6 - ((v - min) / (max - min || 1)) * (h - 12);
  const step = w / (points.length - 1);
  const d = points.map((p, i) => `${i === 0 ? "M" : "L"}${(i*step).toFixed(1)},${norm(p).toFixed(1)}`).join(" ");
  const dArea = d + ` L${w},${h} L0,${h} Z`;
  return (
    <svg className="spark" viewBox={`0 0 ${w} ${h}`} preserveAspectRatio="none">
      <defs>
        <linearGradient id="spark-grad" x1="0" x2="0" y1="0" y2="1">
          <stop offset="0%"  stopColor={color} stopOpacity="0.25"/>
          <stop offset="100%" stopColor={color} stopOpacity="0"/>
        </linearGradient>
      </defs>
      <path d={dArea} fill="url(#spark-grad)"/>
      <path d={d} fill="none" stroke={color} strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round"/>
    </svg>
  );
}

function Metric({ label, value, delta, down, points }) {
  return (
    <div className="card">
      <div className="card__head">
        <div className="card__title">{label}</div>
        <Icon name="more-horizontal" size={14} style={{ color: "var(--muted-foreground)" }}/>
      </div>
      <div className="metric__value">{value}</div>
      <div className="row" style={{ justifyContent: "space-between" }}>
        <span className={"metric__delta" + (down ? " metric__delta--down" : "")}>
          <Icon name={down ? "arrow-right" : "trending-up"} size={11} style={{ marginRight: 4, transform: down ? "rotate(45deg)" : "none" }}/>
          {delta}
        </span>
        <span className="muted" style={{ font: "400 11px var(--font-mono)" }}>30d</span>
      </div>
      <div style={{ marginTop: 12 }}>
        <Sparkline points={points} color={down ? "var(--destructive)" : "var(--primary)"}/>
      </div>
    </div>
  );
}

function ThemeSwitcher({ theme, setTheme }) {
  return (
    <div className="card">
      <div className="card__head">
        <div className="card__title">Active theme</div>
        <span className="muted" style={{ font: "400 11px var(--font-mono)" }}>20 available</span>
      </div>
      <div className="theme-row">
        {THEMES.map(([name, hsl]) => (
          <button
            key={name}
            className={"theme-chip" + (theme === name ? " theme-chip--active" : "")}
            onClick={() => setTheme(name)}
          >
            <span className="theme-chip__dot" style={{ background: `hsl(${hsl})` }}/>
            {name}
          </button>
        ))}
      </div>
      <div className="divider"/>
      <div className="muted" style={{ fontSize: 12, lineHeight: 1.5 }}>
        Theme tokens compose by hue family — never by hex. <code>{`<html data-theme="${theme}">`}</code>
      </div>
    </div>
  );
}

function ActivityFeed() {
  const items = [
    ["EB", "Edwin B.", "regenerated palette", "harmony · 11 shades", "12m"],
    ["AB", "Annie B.",  "exported tokens",     "tailwind.config.ts",  "47m"],
    ["CL", "C. Lead",   "approved review",     "Card · radius-lg",    "2h"],
    ["MK", "M. Kahn",   "added theme",         "spirit",              "5h"],
    ["JM", "J. Mendez", "fixed contrast",      "muted-foreground · light", "yesterday"],
  ];
  return (
    <div className="card">
      <div className="card__head">
        <div className="card__title">Activity</div>
        <button className="btn btn--ghost" style={{ padding: "4px 8px" }}>View all <Icon name="chevron-right" size={12}/></button>
      </div>
      <div className="list">
        {items.map(([initials, name, action, target, when]) => (
          <div className="list-item" key={name + when}>
            <span className="list-item__avatar">{initials}</span>
            <div>
              <div className="list-item__title">
                <span style={{ fontWeight: 500 }}>{name}</span>{" "}
                <span className="muted">{action}</span>{" "}
                <code style={{ fontSize: 11 }}>{target}</code>
              </div>
              <div className="list-item__meta">{when} ago</div>
            </div>
            <div className="list-item__right">
              <span className="pill pill--success">SYNCED</span>
            </div>
          </div>
        ))}
      </div>
    </div>
  );
}

function PaletteCard({ name, hueLabel, hsl, ramps }) {
  return (
    <div className="card" style={{ padding: 16 }}>
      <div className="row" style={{ justifyContent: "space-between", marginBottom: 10 }}>
        <div className="row" style={{ gap: 8 }}>
          <span style={{ width: 18, height: 18, borderRadius: 4, background: `hsl(${hsl})`, border: "1px solid var(--border)" }}/>
          <span style={{ fontWeight: 500, fontSize: 13 }}>{name}</span>
        </div>
        <span className="muted" style={{ font: "400 11px var(--font-mono)" }}>{hueLabel}</span>
      </div>
      <div style={{ display: "grid", gridTemplateColumns: "repeat(11, 1fr)", gap: 2, height: 14, borderRadius: 4, overflow: "hidden", border: "1px solid var(--border)" }}>
        {ramps.map((c, i) => <div key={i} style={{ background: c }}/>)}
      </div>
    </div>
  );
}

function Dashboard({ theme, setTheme }) {
  const points1 = [12,14,13,16,18,17,19,22,21,24,26,25,28,27,30,32,31,34,36,35,38];
  const points2 = [40,42,41,44,40,38,42,46,48,50,49,52,55,54,57,60,58,62,65,63,66];
  const points3 = [80,78,82,80,76,72,70,73,76,74,70,68,71,69,66,64,67,65,68,66,64];
  const points4 = [5,6,5,7,8,7,9,10,9,11,12,11,13,14,13,15,16,15,17,18,17];

  const palettes = [
    ["redGray", "8°",   "hsl(8 18% 46%)",   ["hsl(8 14% 97%)","hsl(8 14% 93%)","hsl(8 14% 85%)","hsl(8 16% 73%)","hsl(8 18% 60%)","hsl(8 18% 46%)","hsl(8 20% 38%)","hsl(8 20% 30%)","hsl(8 22% 22%)","hsl(8 24% 14%)","hsl(8 28% 8%)"]],
    ["greenGray","120°","hsl(120 14% 45%)", ["hsl(120 8% 97%)","hsl(120 8% 93%)","hsl(120 10% 86%)","hsl(120 12% 74%)","hsl(120 14% 60%)","hsl(120 14% 45%)","hsl(120 16% 37%)","hsl(120 16% 30%)","hsl(120 18% 22%)","hsl(120 20% 14%)","hsl(120 22% 8%)"]],
    ["blueGray", "210°","hsl(210 18% 46%)", ["hsl(210 14% 97%)","hsl(210 14% 93%)","hsl(210 14% 86%)","hsl(210 16% 74%)","hsl(210 16% 60%)","hsl(210 18% 46%)","hsl(210 18% 38%)","hsl(210 20% 30%)","hsl(210 22% 22%)","hsl(210 24% 14%)","hsl(210 26% 8%)"]],
    ["violetGray","265°","hsl(265 16% 48%)",["hsl(265 10% 97%)","hsl(265 12% 93%)","hsl(265 12% 86%)","hsl(265 14% 74%)","hsl(265 14% 60%)","hsl(265 16% 48%)","hsl(265 16% 40%)","hsl(265 18% 32%)","hsl(265 18% 24%)","hsl(265 22% 14%)","hsl(265 24% 8%)"]],
  ];

  return (
    <div>
      <div className="page-head">
        <div>
          <div className="eyebrow-row">
            <span className="pill">DASHBOARD</span>
            <span className="muted" style={{ font: "400 11px var(--font-mono)" }}>Calibrated · 8–24% saturation</span>
          </div>
          <div className="page-title">Overview</div>
          <div className="page-sub">Twenty intention-mapped themes derived from Edwin Babbitt's 1878 atomic chromatology — calibrated for screens.</div>
        </div>
        <div className="row" style={{ gap: 8 }}>
          <button className="btn btn--secondary"><Icon name="filter" size={14}/>Filter</button>
          <button className="btn btn--primary"><Icon name="plus" size={14}/>New palette</button>
        </div>
      </div>

      <div className="grid grid--metrics" style={{ marginBottom: 16 }}>
        <Metric label="Active themes"       value="20"     delta="+2 this week" points={points1}/>
        <Metric label="Components"          value="84"     delta="+11"          points={points2}/>
        <Metric label="Avg. saturation"     value="14.2%"  delta="-0.8%" down   points={points3}/>
        <Metric label="WCAG AA pass rate"   value="98.6%"  delta="+0.4%"        points={points4}/>
      </div>

      <div className="grid grid--two" style={{ marginBottom: 16 }}>
        <ActivityFeed/>
        <ThemeSwitcher theme={theme} setTheme={setTheme}/>
      </div>

      <div className="card__title" style={{ margin: "8px 4px 12px" }}>Palette library</div>
      <div className="grid" style={{ gridTemplateColumns: "repeat(4, 1fr)" }}>
        {palettes.map((p) => <PaletteCard key={p[0]} name={p[0]} hueLabel={p[1]} hsl={p[2].replace("hsl(","").replace(")","")} ramps={p[3]}/>)}
      </div>
    </div>
  );
}
window.Dashboard = Dashboard;
