import { bookData, specialSections } from "@/lib/book-data"

export type DocumentationPage = {
  path: string
  title: string
  subtitle?: string
  caption?: string
  group: string
  waitForSelector?: string
  order?: number
}

function pickRepresentativeReadIds(): { id: string; title: string; group: string }[] {
  const picks: { id: string; title: string; group: string }[] = []

  picks.push({ id: specialSections.preface.id, title: specialSections.preface.title, group: "阅读页面" })

  for (const part of bookData) {
    const firstChapter = part.chapters[0]
    const firstSection = firstChapter?.sections?.[0]
    if (firstSection) {
      picks.push({
        id: firstSection.id,
        title: `${part.number} ${part.title}｜${firstSection.title}`,
        group: "阅读页面",
      })
    }
  }

  const extraReadIds = ["9.11", "9.10", "9.9"]
  for (const targetId of extraReadIds) {
    const found = bookData
      .flatMap((p) => p.chapters)
      .flatMap((c) => c.sections)
      .find((s) => s.id === targetId)
    if (found) {
      picks.push({ id: found.id, title: found.title, group: "阅读页面" })
    }
  }

  const seen = new Set<string>()
  return picks.filter((p) => {
    if (seen.has(p.id)) return false
    seen.add(p.id)
    return true
  })
}

export function getDocumentationCatalog(): DocumentationPage[] {
  const pages: DocumentationPage[] = [
    {
      path: "/",
      title: "首页",
      subtitle: "应用主入口",
      caption:
        "首页是用户进入应用的第一个页面，展示书籍封面、简介、目录预览和购买入口。用户可以快速了解内容概要并进行购买决策。",
      group: "核心页面",
      order: 1,
    },
    {
      path: "/chapters",
      title: "目录页",
      subtitle: "章节浏览与导航",
      caption:
        "目录页展示全书的完整章节结构，用户可以浏览各篇、各章内容，查看已解锁和待解锁章节，并快速跳转到阅读页面。",
      group: "核心页面",
      order: 2,
    },
    {
      path: "/about",
      title: "关于页面",
      subtitle: "作者与产品介绍",
      caption: "关于页面展示作者信息、产品理念、运营数据等，帮助用户建立对内容的信任和理解。",
      group: "核心页面",
      order: 3,
    },
    {
      path: "/my",
      title: "个人中心",
      subtitle: "用户账户入口",
      caption: "个人中心聚合用户的账户信息、购买记录、分销收益等功能入口，是用户管理个人信息的核心页面。",
      group: "用户中心",
      order: 4,
    },
    {
      path: "/my/purchases",
      title: "我的购买",
      subtitle: "已购内容管理",
      caption: "展示用户已购买的所有章节，包括购买时间、解锁进度，用户可快速跳转到已购内容继续阅读。",
      group: "用户中心",
      order: 5,
    },
    {
      path: "/my/settings",
      title: "账户设置",
      subtitle: "个人信息配置",
      caption: "用户可在此页面管理个人基础信息、通知偏好、隐私设置等账户相关配置。",
      group: "用户中心",
      order: 6,
    },
    {
      path: "/my/referral",
      title: "分销中心",
      subtitle: "邀请与收益管理",
      caption: "分销中心展示用户的专属邀请链接、邀请人数统计、收益明细，支持一键分享到朋友圈或Soul派对。",
      group: "用户中心",
      order: 7,
    },
    {
      path: "/admin/login",
      title: "后台登录",
      subtitle: "管理员入口",
      caption: "管理后台的登录页面，管理员通过账号密码验证后进入管理系统。",
      group: "管理后台",
      order: 8,
    },
    {
      path: "/admin",
      title: "后台管理",
      subtitle: "系统配置中心",
      caption: "管理后台的核心页面，包含数据概览、内容管理、用户管理、支付配置、二维码管理、系统设置等功能模块。",
      group: "管理后台",
      order: 9,
    },
    {
      path: "/docs",
      title: "开发文档",
      subtitle: "技术与配置说明",
      caption: "面向开发者和运营人员的技术文档，包含支付接口配置说明、分销规则详解、提现流程等内容。",
      group: "运营支持",
      order: 10,
    },
  ]

  const readPicks = pickRepresentativeReadIds()
  for (let i = 0; i < readPicks.length; i++) {
    const pick = readPicks[i]
    pages.push({
      path: `/read/${encodeURIComponent(pick.id)}`,
      title: pick.title,
      subtitle: "章节阅读",
      caption: "阅读页面展示章节的完整内容，未购买用户可预览部分内容，付费墙引导购买解锁全文。",
      group: pick.group,
      waitForSelector: "main",
      order: 100 + i,
    })
  }

  // Sort by order
  return pages.sort((a, b) => (a.order || 999) - (b.order || 999))
}
