import { readdir, readFile } from 'node:fs/promises'
import { join } from 'node:path'

export default defineEventHandler(async () => {
  const bookDir = join(process.cwd(), 'content', 'book')
  const files = await readdir(bookDir)
  const mdFiles = files.filter(f => f.endsWith('.md')).sort()

  const chapters = await Promise.all(
    mdFiles.map(async (file) => {
      const raw = await readFile(join(bookDir, file), 'utf-8')
      const slug = file.replace(/\.md$/, '')
      const frontmatter = parseFrontmatter(raw)
      return { slug, ...frontmatter }
    }),
  )

  return chapters.sort((a, b) => (a.order ?? 0) - (b.order ?? 0))
})

function parseFrontmatter(content: string): Record<string, unknown> {
  const match = content.match(/^---\n([\s\S]*?)\n---/)
  if (!match) return {}

  const lines = match[1].split('\n')
  const result: Record<string, unknown> = {}

  for (const line of lines) {
    const colonIdx = line.indexOf(':')
    if (colonIdx === -1) continue
    const key = line.slice(0, colonIdx).trim()
    let value: string | number = line.slice(colonIdx + 1).trim()
    // Remove quotes
    if ((value.startsWith('"') && value.endsWith('"')) || (value.startsWith("'") && value.endsWith("'"))) {
      value = value.slice(1, -1)
    }
    // Parse numbers
    if (/^\d+$/.test(value)) {
      result[key] = parseInt(value, 10)
    }
    else {
      result[key] = value
    }
  }

  return result
}
