import { DEFAULT_H5_THEME, type H5Theme } from '@/lib/h5/theme'

/**
 * H5 主题预设库（P2.5 预设化）。
 *
 * 设计目标：把"调 N 个颜色"降维成"选一个预设名"。
 * 每份 design.md（如 Cursor、Claude、Linear）提炼成一个预设条目，
 * owner 只需选名称，系统自动应用整套搭配好的主题。
 *
 * 扩展方式：往 H5_THEME_PRESETS 加一个条目即可。
 *   1. 从 design.md 提取 colors（canvas/ink/primary/hairline...）
 *   2. 映射到 H5Theme 字段（bg/surface/text/accent/border...）
 *   3. 浅色 design.md 建议同时做一个深色变体（mobile 夜间友好）
 *
 * 存储层只存预设 ID 字符串（app_secrets[appId].themePreset）。
 * 入口路由在签发 cookie 时把 ID 解析成完整 H5Theme，
 * 前端消费层无感知"预设"概念，拿到的仍是 H5Theme。
 */

export type H5VisualStyle = 'bubble' | 'editorial'
export type H5WelcomeLayout = 'standard' | 'profile-card' | 'product-card'
export type H5WelcomeHighlightIcon = 'heart' | 'sparkles' | 'report'

export interface H5WelcomeHighlight {
  icon: H5WelcomeHighlightIcon
  title: string
  description: string
}

export interface H5ThemePreset {
  /** 预设 ID（存储/引用用，小写 kebab-case） */
  id: string
  /** 显示名（owner 配置 UI 展示） */
  name: string
  /** 浅色/深色（归类展示用） */
  mode: 'dark' | 'light'
  /** 一句话风格描述 */
  description: string
  /** 组件结构变体。内置和未配置场景默认 editorial；bubble 仅用于显式兼容。 */
  visualStyle?: H5VisualStyle
  /** 首次进入时的欢迎区域布局；与正常对话结构相互独立。 */
  welcomeLayout?: H5WelcomeLayout
  /** profile-card 欢迎布局底部的能力摘要。暂存于预设中，避免引入数据库字段。 */
  welcomeHighlights?: readonly H5WelcomeHighlight[]
  /** 欢迎卡上展示 Beta 徽章。预设级开关，不需要 DB 字段。 */
  showBetaBadge?: boolean
  /** 完整主题（应用到 H5 渲染层） */
  theme: H5Theme
}

/**
 * 预设库。顺序即 UI 展示顺序。
 * 新增预设：在此数组追加条目，无需改其他文件。
 */
export const H5_THEME_PRESETS: readonly H5ThemePreset[] = [
  {
    id: 'default-dark',
    name: 'Forge 默认',
    mode: 'dark',
    description: '系统内置深色主题，靛蓝主色',
    visualStyle: 'editorial',
    theme: {
      ...DEFAULT_H5_THEME,
      userMessageBg: '#252525',
      userMessageText: '#ffffff',
      composerBg: '#1a1a1a',
      controlBg: '#1f1f1f',
      conversationText: '#ffffff',
      fontConversation: DEFAULT_H5_THEME.fontBody,
    },
  },
  {
    id: 'companion-light',
    name: '陪伴 · 柔雾蓝',
    mode: 'light',
    description: '冷白画布 + 明亮钴蓝，点缀柔和珊瑚红的陪伴型对话主题',
    visualStyle: 'editorial',
    welcomeLayout: 'profile-card',
    showBetaBadge: true,
    welcomeHighlights: [
      {
        icon: 'heart',
        title: '充分理解你',
        description: '像朋友一样倾听，理解你的经历与思考',
      },
      {
        icon: 'sparkles',
        title: '激发更多可能',
        description: '通过提问与引导，发现更多隐性价值',
      },
      {
        icon: 'report',
        title: '生成专业报告',
        description: '将经验沉淀为清晰、可复用的成果',
      },
    ],
    // 来源：用户提供的「AI 经验萃取师」参考图。
    // 对话结构继续沿用与 Cursor / Claude 一致的 editorial 骨架，
    // 仅首次欢迎区域使用独立的 profile-card 布局。
    // 主画布约 #f7f8fb · 用户气泡约 #2f53ef · 主文字约 #231f2a
    // 参考图中的粉红仅承担价值提示/错误语义，不与钴蓝争夺主强调色。
    theme: {
      mode: 'light',
      bg: '#f7f8fb',
      surface: '#ffffff',
      surfaceHover: '#f1f3fb',
      surfaceActive: '#e9edff',
      border: '#dfe3f2',
      borderSubtle: '#eceef6',
      text: '#231f2a',
      textSecondary: '#575a68',
      textMuted: '#6f7282',
      accent: '#2f53ef',
      accentHover: '#2445d4',
      onAccent: '#ffffff',
      success: '#3f8f70',
      warning: '#d69b22',
      error: '#ef5b67',
      fontBody: "'Inter', -apple-system, BlinkMacSystemFont, 'PingFang SC', 'Microsoft YaHei', sans-serif",
      fontMono: "'JetBrains Mono', ui-monospace, SFMono-Regular, monospace",
      fontHeading: "'Inter', -apple-system, BlinkMacSystemFont, 'PingFang SC', 'Microsoft YaHei', sans-serif",
      fontConversation: "'Inter', -apple-system, BlinkMacSystemFont, 'PingFang SC', 'Microsoft YaHei', sans-serif",
      fontSizeConversation: '16px',
      lineHeightConversation: '1.65',
      readWidth: '680px',
      userMessageBg: '#2f53ef',
      userMessageText: '#ffffff',
      composerBg: '#ffffff',
      controlBg: '#eef1fb',
      conversationText: '#231f2a',
      radiusBubble: '16px',
      radiusInput: '18px',
    },
  },
  {
    id: 'cursor-light',
    name: 'Cursor · 暖奶油',
    mode: 'light',
    description: '暖色编辑画布 + Cursor 橙强调色（忠于 Cursor design.md）',
    visualStyle: 'editorial',
    // 来源：Cursor design.md
    //   canvas #f7f7f4 · ink #26251e · primary #f54e00 · hairline #e6e5e0
    //   body #5a5852 · muted #807d72 · surface-card #ffffff
    //   semantic-success #1f8a65 · semantic-error #cf2d56
    theme: {
      mode: 'light',
      bg: '#f7f7f4',
      surface: '#ffffff',
      surfaceHover: '#fafaf7',
      surfaceActive: '#e6e5e0',
      border: '#cfcdc4',
      borderSubtle: '#e6e5e0',
      text: '#26251e',
      textSecondary: '#5a5852',
      textMuted: '#807d72',
      accent: '#f54e00',
      accentHover: '#d04200',
      onAccent: '#ffffff',
      success: '#1f8a65',
      warning: '#c08532',
      error: '#cf2d56',
      // 字体：Inter（CursorGothic 开源替代）+ JetBrains Mono（design.md 指定）
      fontBody: "'Inter', -apple-system, BlinkMacSystemFont, system-ui, sans-serif",
      fontMono: "'JetBrains Mono', ui-monospace, SFMono-Regular, monospace",
      fontConversation: "'Inter', -apple-system, BlinkMacSystemFont, system-ui, sans-serif",
      // 排版密度：sans 紧凑型（区别于 Pi/Claude 的 serif 阅读型默认值）
      fontSizeConversation: '16px',
      lineHeightConversation: '1.65',
      readWidth: '640px',
      userMessageBg: '#e6e5e0',
      userMessageText: '#26251e',
      composerBg: '#ffffff',
      controlBg: '#e6e5e0',
      conversationText: '#26251e',
      // 圆角：Cursor 紧凑风格（CTA 8px / 卡片 12px）
      radiusBubble: '12px',
      radiusInput: '8px',
    },
  },
  {
    id: 'cursor-dark',
    name: 'Cursor · 暖夜',
    mode: 'dark',
    description: 'Cursor 配色的深色反转，保留橙色强调（mobile 夜间友好）',
    visualStyle: 'editorial',
    // 由 cursor-light 反派：ink(#26251e) → 表面，canvas(#f7f7f4) → 文字
    theme: {
      mode: 'dark',
      bg: '#1a1815',
      surface: '#26251e',
      surfaceHover: '#2f2d24',
      surfaceActive: '#3a382f',
      border: '#4a483f',
      borderSubtle: '#3a382f',
      text: '#f7f7f4',
      textSecondary: '#c9c5bb',
      textMuted: '#a09c92',
      accent: '#f54e00',
      accentHover: '#d04200',
      onAccent: '#ffffff',
      success: '#1f8a65',
      warning: '#d99a3a',
      error: '#e8456e',
      // 字体：同 cursor-light（Inter + JetBrains Mono）
      fontBody: "'Inter', -apple-system, BlinkMacSystemFont, system-ui, sans-serif",
      fontMono: "'JetBrains Mono', ui-monospace, SFMono-Regular, monospace",
      fontConversation: "'Inter', -apple-system, BlinkMacSystemFont, system-ui, sans-serif",
      // 排版密度：sans 紧凑型（区别于 Pi/Claude 的 serif 阅读型默认值）
      fontSizeConversation: '16px',
      lineHeightConversation: '1.65',
      readWidth: '640px',
      userMessageBg: '#3a382f',
      userMessageText: '#f7f7f4',
      composerBg: '#26251e',
      controlBg: '#3a382f',
      conversationText: '#f7f7f4',
      // 圆角：同 cursor-light（紧凑开发者风格）
      radiusBubble: '12px',
      radiusInput: '8px',
    },
  },
  {
    id: 'claude-light',
    name: 'Claude · 暖奶油',
    mode: 'light',
    description: 'Anthropic 奶油画布 + 珊瑚强调色（忠于 Claude design.md）',
    visualStyle: 'editorial',
    // 来源：Claude design.md
    //   canvas #faf9f5 · surface-soft #f5f0e8 · surface-card #efe9de
    //   ink #141413 · body #3d3d3a · muted #6c6a64
    //   primary #cc785c · primary-active #a9583e · on-primary #ffffff
    //   hairline #e6dfd8 · hairline-soft #ebe6df
    //   success #5db872 · warning #d4a017 · error #c64545
    theme: {
      mode: 'light',
      bg: '#faf9f5',
      surface: '#f5f0e8',
      surfaceHover: '#efe9de',
      surfaceActive: '#e8e0d2',
      border: '#e6dfd8',
      borderSubtle: '#ebe6df',
      text: '#141413',
      textSecondary: '#3d3d3a',
      textMuted: '#6c6a64',
      accent: '#cc785c',
      accentHover: '#a9583e',
      onAccent: '#ffffff',
      success: '#5db872',
      warning: '#d4a017',
      error: '#c64545',
      // 字体：Inter（StyreneB 开源替代）+ JetBrains Mono（design.md 指定）
      // 标题字体：Copernicus 不可商用，用 Tiempos Headline → Cormorant Garamond 兜底
      fontBody: "'Inter', -apple-system, BlinkMacSystemFont, system-ui, sans-serif",
      fontMono: "'JetBrains Mono', ui-monospace, SFMono-Regular, monospace",
      fontHeading: "'Tiempos Headline', 'Cormorant Garamond', Georgia, serif",
      fontConversation: "'Tiempos Headline', 'Cormorant Garamond', 'Songti SC', STSong, Georgia, serif",
      userMessageBg: '#efe9de',
      userMessageText: '#141413',
      composerBg: '#f5f0e8',
      controlBg: '#efe9de',
      conversationText: '#141413',
      // 圆角：Claude 编辑风格（按钮/输入 8px，卡片/气泡 12px）
      radiusBubble: '12px',
      radiusInput: '8px',
    },
  },
  {
    id: 'claude-dark',
    name: 'Claude · 暖夜',
    mode: 'dark',
    description: 'Claude 珊瑚配色的深色反转，保留奶油文字（mobile 夜间友好）',
    visualStyle: 'editorial',
    // 由 claude-light 反派：dark surfaces 来自 surface-dark/surface-dark-soft/surface-dark-elevated
    theme: {
      mode: 'dark',
      bg: '#181715',
      surface: '#1f1e1b',
      surfaceHover: '#252320',
      surfaceActive: '#2f2c27',
      border: '#3a3832',
      borderSubtle: '#252320',
      text: '#faf9f5',
      textSecondary: '#a09d96',
      textMuted: '#6c6a64',
      accent: '#cc785c',
      accentHover: '#a9583e',
      onAccent: '#ffffff',
      success: '#5db872',
      warning: '#e8b54a',
      error: '#d45a5a',
      // 字体：同 claude-light（Inter + JetBrains Mono + 标题 serif）
      fontBody: "'Inter', -apple-system, BlinkMacSystemFont, system-ui, sans-serif",
      fontMono: "'JetBrains Mono', ui-monospace, SFMono-Regular, monospace",
      fontHeading: "'Tiempos Headline', 'Cormorant Garamond', Georgia, serif",
      fontConversation: "'Tiempos Headline', 'Cormorant Garamond', 'Songti SC', STSong, Georgia, serif",
      userMessageBg: '#2f2c27',
      userMessageText: '#faf9f5',
      composerBg: '#1f1e1b',
      controlBg: '#2f2c27',
      conversationText: '#faf9f5',
      // 圆角：同 claude-light
      radiusBubble: '12px',
      radiusInput: '8px',
    },
  },
  {
    id: 'pi-editorial-light',
    name: 'Pi 暖色对话',
    mode: 'light',
    description: '暖米色阅读画布、森林绿正文和无气泡 AI 回复',
    visualStyle: 'editorial',
    // 来源：Talking with Pi design.md + Mobbin 七屏流程。
    // 结构重点：assistant 无气泡、用户浅沙气泡、统一输入胶囊、阅读型 serif 正文。
    theme: {
      mode: 'light',
      bg: '#f9f4ec',
      surface: '#fcfaf6',
      surfaceHover: '#f3eadd',
      surfaceActive: '#ebe2d3',
      border: '#ded6c8',
      borderSubtle: '#e9e1d5',
      text: '#1b3c28',
      textSecondary: '#405846',
      textMuted: '#aaa298',
      accent: '#37804c',
      accentHover: '#2f7042',
      onAccent: '#ffffff',
      success: '#37804c',
      warning: '#b7791f',
      error: '#b3413b',
      fontBody: "'Inter', -apple-system, BlinkMacSystemFont, 'PingFang SC', 'Microsoft YaHei', sans-serif",
      fontMono: "'JetBrains Mono', ui-monospace, SFMono-Regular, monospace",
      fontHeading: "'Cormorant Garamond', 'Songti SC', STSong, Georgia, serif",
      fontConversation: "'Cormorant Garamond', 'Songti SC', STSong, Georgia, serif",
      userMessageBg: '#f3eadd',
      userMessageText: '#1b3c28',
      composerBg: '#fcfaf6',
      controlBg: '#ebe2d3',
      conversationText: '#1b3c28',
      radiusBubble: '10px',
      radiusInput: '28px',
    },
  },
  {
    id: 'linear-dark',
    name: 'Linear · 极夜',
    mode: 'dark',
    description: '极黑画布 + 薰衣草蓝强调色（忠于 Linear design.md，无浅色变体）',
    visualStyle: 'editorial',
    // 来源：Linear design.md
    //   canvas #010102 · surface-1 #0f1011 · surface-2 #141516 · surface-3 #18191a
    //   ink #f7f8f8 · ink-muted #d0d6e0 · ink-subtle #8a8f98
    //   primary #5e6ad2 · primary-hover #828fff · on-primary #ffffff
    //   hairline #23252a · hairline-strong #34343a
    //   semantic-success #27a644
    theme: {
      mode: 'dark',
      bg: '#010102',
      surface: '#0f1011',
      surfaceHover: '#141516',
      surfaceActive: '#18191a',
      border: '#23252a',
      borderSubtle: '#18191a',
      text: '#f7f8f8',
      textSecondary: '#d0d6e0',
      textMuted: '#8a8f98',
      accent: '#5e6ad2',
      accentHover: '#828fff',
      onAccent: '#ffffff',
      success: '#27a644',
      warning: '#d4a017',
      error: '#c64545',
      // 字体：Linear Display/Text 用 Inter 替代，Linear Mono 用 JetBrains Mono 替代
      fontBody: "'Inter', -apple-system, BlinkMacSystemFont, system-ui, sans-serif",
      fontMono: "'JetBrains Mono', ui-monospace, SFMono-Regular, monospace",
      fontHeading: "'Inter', -apple-system, BlinkMacSystemFont, system-ui, sans-serif",
      fontConversation: "'Inter', -apple-system, BlinkMacSystemFont, system-ui, sans-serif",
      // 排版密度：sans 紧凑型（区别于 Pi/Claude 的 serif 阅读型默认值）
      fontSizeConversation: '16px',
      lineHeightConversation: '1.65',
      readWidth: '640px',
      userMessageBg: '#18191a',
      userMessageText: '#f7f8f8',
      composerBg: '#0f1011',
      controlBg: '#18191a',
      conversationText: '#f7f8f8',
      // 圆角：Linear 紧凑风格（按钮/输入 8px，卡片/气泡 12px）
      radiusBubble: '12px',
      radiusInput: '8px',
    },
  },
  {
    id: 'opencode-light',
    name: 'OpenCode · 奶油终端',
    mode: 'light',
    description: 'Berkeley Mono 等宽排版 + 奶油画布 + 4px 直角（忠于 OpenCode design.md）',
    visualStyle: 'editorial',
    theme: {
      mode: 'light',
      bg: '#fdfcfc',
      surface: '#f1eeee',
      surfaceHover: '#f8f7f7',
      surfaceActive: '#e8e4e4',
      border: 'rgba(15,0,0,0.12)',
      borderSubtle: 'rgba(15,0,0,0.08)',
      text: '#201d1d',
      textSecondary: '#424245',
      textMuted: '#646262',
      accent: '#007aff',
      accentHover: '#0056b3',
      onAccent: '#fdfcfc',
      success: '#30d158',
      warning: '#ff9f0a',
      error: '#ff3b30',
      // Berkeley Mono 为商用字体，用 IBM Plex Mono / JetBrains Mono 做开源回退
      fontBody: "'Berkeley Mono', 'IBM Plex Mono', 'JetBrains Mono', ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, 'Liberation Mono', 'Courier New', monospace",
      fontMono: "'Berkeley Mono', 'IBM Plex Mono', 'JetBrains Mono', ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, 'Liberation Mono', 'Courier New', monospace",
      fontHeading: "'Berkeley Mono', 'IBM Plex Mono', 'JetBrains Mono', ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, 'Liberation Mono', 'Courier New', monospace",
      fontConversation: "'Berkeley Mono', 'IBM Plex Mono', 'JetBrains Mono', ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, 'Liberation Mono', 'Courier New', monospace",
      // 排版密度：等宽终端风最紧凑
      fontSizeConversation: '15px',
      lineHeightConversation: '1.6',
      readWidth: '600px',
      userMessageBg: '#e8e4e4',
      userMessageText: '#201d1d',
      composerBg: '#f1eeee',
      controlBg: '#e8e4e4',
      conversationText: '#201d1d',
      // OpenCode 营销页：交互元素 4px，容器 0px；H5 气泡/输入框统一 4px
      radiusBubble: '4px',
      radiusInput: '4px',
    },
  },
  {
    id: 'opencode-dark',
    name: 'OpenCode · 夜间终端',
    mode: 'dark',
    description: 'OpenCode 奶油配色的深色反转，保留蓝色强调与等宽字体',
    visualStyle: 'editorial',
    theme: {
      mode: 'dark',
      bg: '#0f0e0e',
      surface: '#201d1d',
      surfaceHover: '#302c2c',
      surfaceActive: '#3a3535',
      border: 'rgba(253,252,252,0.12)',
      borderSubtle: 'rgba(253,252,252,0.08)',
      text: '#fdfcfc',
      textSecondary: '#c9c5c5',
      textMuted: '#9a9898',
      accent: '#007aff',
      accentHover: '#4a9eff',
      onAccent: '#fdfcfc',
      success: '#30d158',
      warning: '#ff9f0a',
      error: '#ff3b30',
      fontBody: "'Berkeley Mono', 'IBM Plex Mono', 'JetBrains Mono', ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, 'Liberation Mono', 'Courier New', monospace",
      fontMono: "'Berkeley Mono', 'IBM Plex Mono', 'JetBrains Mono', ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, 'Liberation Mono', 'Courier New', monospace",
      fontHeading: "'Berkeley Mono', 'IBM Plex Mono', 'JetBrains Mono', ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, 'Liberation Mono', 'Courier New', monospace",
      fontConversation: "'Berkeley Mono', 'IBM Plex Mono', 'JetBrains Mono', ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, 'Liberation Mono', 'Courier New', monospace",
      // 排版密度：等宽终端风最紧凑
      fontSizeConversation: '15px',
      lineHeightConversation: '1.6',
      readWidth: '600px',
      userMessageBg: '#3a3535',
      userMessageText: '#fdfcfc',
      composerBg: '#201d1d',
      controlBg: '#3a3535',
      conversationText: '#fdfcfc',
      radiusBubble: '4px',
      radiusInput: '4px',
    },
  },
] as const

/** 预设 ID → 预设对象。不存在返回 null。 */
export function getPresetById(id: string | null | undefined): H5ThemePreset | null {
  if (!id) return null
  return H5_THEME_PRESETS.find((p) => p.id === id) ?? null
}

/** 默认预设 ID（owner UI 的"未配置"占位） */
export const DEFAULT_PRESET_ID = 'default-dark'

/**
 * 把预设 ID 解析成 H5Theme。
 * - id 为空/null → null（调用方用 DEFAULT_H5_THEME）
 * - id 无效 → null
 * - id 有效 → 对应预设的 theme
 */
export function resolvePresetTheme(id: string | null | undefined): H5Theme | null {
  return getPresetById(id)?.theme ?? null
}

/** 解析预设对应的组件结构。未配置或未知预设同样使用统一的 editorial 骨架。 */
export function resolvePresetVisualStyle(id: string | null | undefined): H5VisualStyle {
  return getPresetById(id)?.visualStyle ?? 'editorial'
}

/** 解析欢迎区域布局。它只影响空会话欢迎卡，不改变消息流和输入区结构。 */
export function resolvePresetWelcomeLayout(id: string | null | undefined): H5WelcomeLayout {
  return getPresetById(id)?.welcomeLayout ?? 'standard'
}

/** 解析欢迎卡能力摘要。返回预设中的只读配置，不进入主题 CSS 变量。 */
export function resolvePresetWelcomeHighlights(id: string | null | undefined): readonly H5WelcomeHighlight[] {
  return getPresetById(id)?.welcomeHighlights ?? []
}

/** 解析欢迎卡是否展示 Beta 徽章。 */
export function resolvePresetShowBetaBadge(id: string | null | undefined): boolean {
  return getPresetById(id)?.showBetaBadge ?? false
}
