import { RealtimeRefresh } from "@/components/app-runtime";
import {
  AppShell,
  HeaderTitle,
  MenuRow,
  OtaCenterCard,
  ProfileHero,
  StatusBar,
} from "@/components/app-ui";
import { requirePageSession } from "@/lib/boss-auth";
import { getOtaStatus, readState } from "@/lib/boss-data";

export const dynamic = "force-dynamic";

export default async function MePage() {
  await requirePageSession();
  const [state, otaStatus] = await Promise.all([readState(), getOtaStatus()]);

  return (
    <AppShell>
      <RealtimeRefresh events={["ota.updated"]} />
      <StatusBar />
      <HeaderTitle title="我的" />
      <div className="flex flex-col gap-3 px-[18px] pb-5">
        <ProfileHero user={state.user} />
        <MenuRow
          href="/me/master-agent"
          title="主 Agent 提示词 / 记忆"
          description="配置全局主提示词、当前主提示词和用户记忆"
        />
        <MenuRow
          href="/me/master-agent/evolution"
          title="主 Agent 自动进化"
          description="查看进化信号、提案、规则，并切换受控 / 全自动模式"
        />
        <MenuRow
          href="/me/storage"
          title="附件与存储"
          description="当前附件存储模式、服务器文件存储与阿里 OSS"
        />
        <MenuRow
          href="/me/security"
          title="账号与安全"
          description="修改登录密码、设备安全与身份校验"
        />
        <MenuRow href="/me/settings" title="设置" description="实时刷新、风险徽标和默认首页" />
        <MenuRow
          href="/me/ops"
          title="运维与修复"
          description="查看运维对话、审计对话、修复回放与 standby 切换"
        />
        <MenuRow
          href="/me/ai-accounts"
          title="AI 账号"
          description="管理主 GPT、备用 GPT、ChatGPT Plus / Codex 节点与 API 容灾"
        />
        <MenuRow
          href="/me/skills"
          title="技能"
          description="按绑定设备查看已同步 Skill，并一键复制调用语句"
        />
        <MenuRow
          href="/me/about"
          title="关于"
          description={`当前版本 ${state.user.version}`}
          badge={state.user.hasOta ? <span className="text-[12px] text-[#FF3B30]">OTA</span> : null}
        />
        <OtaCenterCard
          currentVersion={state.user.version}
          availableRelease={otaStatus.availableRelease}
          logs={otaStatus.logs}
          boundCodexNodeLabel={state.user.boundCodexNodeLabel}
          roleLabel={state.user.roleLabel}
          canApply={state.user.role === "highest_admin"}
          compact
        />
      </div>
    </AppShell>
  );
}
