export type FeaturedPostMode = 'latest' | 'fixed'

export interface HomepageConfig {
  hero: {
    eyebrow: string
    titleLines: [string, string, string]
    bioBefore: string
    bioName: string
    bioMid: string
    bioEmphasize: string
    ctaLabel: string
    ctaLink: string
    ctaClickable: boolean
  }
  latestWorkCard: {
    visible: boolean
    clickable: boolean
    label: string
  }
  bento: {
    sectionTitle: string
    sectionHint: string
    featuredPost: {
      visible: boolean
      clickable: boolean
      mode: FeaturedPostMode
      postId: number | null
      backgroundImage: string
    }
    stats: {
      visible: boolean
      clickable: boolean
      link: string
      value: string
      label: string
      sublabel: string
    }
    snippets: {
      visible: boolean
      clickable: boolean
      link: string
      comment: string
      codeLines: {
        keyword: string
        func: string
        string: string
      }
      ctaLabel: string
    }
    services: {
      visible: boolean
      clickable: boolean
      link: string
      title: string
      description: string
    }
  }
}

export const DEFAULT_HOMEPAGE_CONFIG: HomepageConfig = {
  hero: {
    eyebrow: '前端架构师 & 创意开发者',
    titleLines: ['设计', '铸造数字', '灵魂'],
    bioBefore: '在代码的逻辑与设计的感性之间寻找平衡。我是',
    bioName: '年糕崽崽',
    bioMid: '，不仅仅构建页面，更在构建',
    bioEmphasize: '沉浸式体验',
    ctaLabel: '浏览作品集',
    ctaLink: '/works',
    ctaClickable: true,
  },
  latestWorkCard: {
    visible: true,
    clickable: true,
    label: '最新作品',
  },
  bento: {
    sectionTitle: '精选内容',
    sectionHint: '下滑探索更多',
    featuredPost: {
      visible: true,
      clickable: true,
      mode: 'latest',
      postId: null,
      backgroundImage: '/bg/img.png',
    },
    stats: {
      visible: true,
      clickable: false,
      link: '',
      value: '120+',
      label: '开源提交 (Commits)',
      sublabel: 'Vue, React, Three.js',
    },
    snippets: {
      visible: true,
      clickable: true,
      link: '/snippets',
      comment: '// React Hook: useArt',
      codeLines: {
        keyword: 'const',
        func: 'useCreative',
        string: 'Innovation',
      },
      ctaLabel: '访问代码实验室',
    },
    services: {
      visible: true,
      clickable: true,
      link: '/services',
      title: '需要独特的前端架构？',
      description: '不论是 WebGL 3D 交互网站，还是高性能的 SaaS 管理系统，我都能提供专业的解决方案。',
    },
  },
}

export const DEFAULT_HOMEPAGE_CONFIG_JSON = JSON.stringify(DEFAULT_HOMEPAGE_CONFIG)

function isObject(v: unknown): v is Record<string, unknown> {
  return typeof v === 'object' && v !== null && !Array.isArray(v)
}

/** Deep-merge parsed config onto defaults so partial/corrupt JSON still works. */
export function parseHomepageConfig(raw?: string | null): HomepageConfig {
  const base = structuredClone(DEFAULT_HOMEPAGE_CONFIG)
  if (!raw) return base
  try {
    const parsed = JSON.parse(raw) as unknown
    if (!isObject(parsed)) return base

    if (isObject(parsed.hero)) {
      const h = parsed.hero
      Object.assign(base.hero, {
        eyebrow: typeof h.eyebrow === 'string' ? h.eyebrow : base.hero.eyebrow,
        bioBefore: typeof h.bioBefore === 'string' ? h.bioBefore : base.hero.bioBefore,
        bioName: typeof h.bioName === 'string' ? h.bioName : base.hero.bioName,
        bioMid: typeof h.bioMid === 'string' ? h.bioMid : base.hero.bioMid,
        bioEmphasize: typeof h.bioEmphasize === 'string' ? h.bioEmphasize : base.hero.bioEmphasize,
        ctaLabel: typeof h.ctaLabel === 'string' ? h.ctaLabel : base.hero.ctaLabel,
        ctaLink: typeof h.ctaLink === 'string' ? h.ctaLink : base.hero.ctaLink,
        ctaClickable: typeof h.ctaClickable === 'boolean' ? h.ctaClickable : base.hero.ctaClickable,
      })
      if (Array.isArray(h.titleLines) && h.titleLines.length >= 3) {
        base.hero.titleLines = [
          String(h.titleLines[0] ?? base.hero.titleLines[0]),
          String(h.titleLines[1] ?? base.hero.titleLines[1]),
          String(h.titleLines[2] ?? base.hero.titleLines[2]),
        ]
      }
    }

    if (isObject(parsed.latestWorkCard)) {
      Object.assign(base.latestWorkCard, parsed.latestWorkCard)
    }

    if (isObject(parsed.bento)) {
      const b = parsed.bento
      if (typeof b.sectionTitle === 'string') base.bento.sectionTitle = b.sectionTitle
      if (typeof b.sectionHint === 'string') base.bento.sectionHint = b.sectionHint

      if (isObject(b.featuredPost)) {
        const fp = b.featuredPost
        Object.assign(base.bento.featuredPost, {
          visible: typeof fp.visible === 'boolean' ? fp.visible : base.bento.featuredPost.visible,
          clickable: typeof fp.clickable === 'boolean' ? fp.clickable : base.bento.featuredPost.clickable,
          mode: fp.mode === 'fixed' ? 'fixed' : 'latest',
          postId: typeof fp.postId === 'number' ? fp.postId : fp.postId === null ? null : base.bento.featuredPost.postId,
          backgroundImage: typeof fp.backgroundImage === 'string' ? fp.backgroundImage : base.bento.featuredPost.backgroundImage,
        })
      }
      if (isObject(b.stats)) Object.assign(base.bento.stats, b.stats)
      if (isObject(b.snippets)) {
        Object.assign(base.bento.snippets, b.snippets)
        if (isObject((b.snippets as Record<string, unknown>).codeLines)) {
          Object.assign(base.bento.snippets.codeLines, (b.snippets as Record<string, unknown>).codeLines)
        }
        // backward compat with plan name codeHtmlOrLines
        const legacy = (b.snippets as Record<string, unknown>).codeHtmlOrLines
        if (isObject(legacy)) Object.assign(base.bento.snippets.codeLines, legacy)
      }
      if (isObject(b.services)) Object.assign(base.bento.services, b.services)
    }
  } catch {
    return structuredClone(DEFAULT_HOMEPAGE_CONFIG)
  }
  return base
}
