import { useHealth } from "../lib/health";
import type { ViewKey } from "../shell/LeftNav";

function Stat({ label, value, sub, accent }: { label: string; value: string; sub: string; accent: string }) {
  return (
    <div className="rounded-xl border border-line bg-ink-850 p-4">
      <div className="text-xs text-slate-500">{label}</div>
      <div className={`mt-1 text-2xl font-semibold ${accent}`}>{value}</div>
      <div className="mt-0.5 text-[11px] text-slate-500">{sub}</div>
    </div>
  );
}

const CAPS = [
  { icon: "◆", title: "Agent 编排", desc: "React Flow 画布 → Eino 动态图 → 流式执行", to: "studio" as ViewKey, color: "text-violet-400" },
  { icon: "▣", title: "RAG 知识库", desc: "多文件入库 + 向量/全文/图谱 三路混合检索", to: "kb" as ViewKey, color: "text-cyan-400" },
  { icon: "◇", title: "偏好记忆", desc: "画像 + 多轮历史，让模型“知道是你”", to: "memory" as ViewKey, color: "text-emerald-400" },
];

// 工作台：平台概览 + 快捷入口（深色 AI 控制台首页）。
export function Home({ onSelect }: { onSelect: (v: ViewKey) => void }) {
  const h = useHealth();
  return (
    <div className="h-full overflow-auto p-8">
      <div className="mx-auto max-w-5xl">
        <div className="flex items-center gap-3">
          <div className="flex h-11 w-11 items-center justify-center rounded-xl bg-gradient-to-br from-violet-500 to-cyan-500 text-lg font-bold text-white shadow-glow">
            S
          </div>
          <div>
            <h1 className="brand-gradient text-2xl font-bold leading-tight">sundynix-agentix</h1>
            <p className="text-sm text-slate-500">分层式 AI Agent 平台 · 编排 / 知识库 / 记忆</p>
          </div>
        </div>

        <div className="mt-6 grid grid-cols-2 gap-3 md:grid-cols-4">
          <Stat label="对话模型" value="DeepSeek" sub="chat · 控制面" accent="text-violet-300" />
          <Stat label="向量模型" value="百炼 v3" sub="embedding · 1024维" accent="text-cyan-300" />
          <Stat label="混合检索" value="3 路" sub="向量+全文+图谱" accent="text-emerald-300" />
          <Stat label="网关" value={h.gateway ? "在线" : "离线"} sub={h.persisted ? "持久化就绪" : "降级"} accent={h.gateway ? "text-emerald-300" : "text-rose-300"} />
        </div>

        <div className="mt-8 grid grid-cols-1 gap-4 md:grid-cols-3">
          {CAPS.map((c) => (
            <button
              key={c.to}
              onClick={() => onSelect(c.to)}
              className="group rounded-xl border border-line bg-ink-850 p-5 text-left transition hover:border-violet-500/50 hover:bg-ink-800"
            >
              <div className={`text-2xl ${c.color}`}>{c.icon}</div>
              <div className="mt-3 font-medium text-slate-100">{c.title}</div>
              <div className="mt-1 text-xs leading-relaxed text-slate-500">{c.desc}</div>
              <div className="mt-3 text-xs text-violet-400 opacity-0 transition group-hover:opacity-100">进入 →</div>
            </button>
          ))}
        </div>

        <div className="mt-8 rounded-xl border border-line bg-ink-900 p-5">
          <div className="mb-3 text-sm font-medium text-slate-300">快速开始</div>
          <div className="flex flex-wrap gap-2">
            <button onClick={() => onSelect("studio")} className="rounded-lg bg-violet-600 px-3 py-1.5 text-sm font-medium text-white hover:bg-violet-500">
              ＋ 新建 Agent 编排
            </button>
            <button onClick={() => onSelect("kb")} className="rounded-lg border border-line px-3 py-1.5 text-sm text-slate-300 hover:bg-ink-800">
              ⬆ 入库知识
            </button>
            <button onClick={() => onSelect("memory")} className="rounded-lg border border-line px-3 py-1.5 text-sm text-slate-300 hover:bg-ink-800">
              ◇ 管理记忆
            </button>
          </div>
        </div>
      </div>
    </div>
  );
}
