import { unlink } from 'node:fs/promises'
import { join } from 'node:path'

export default defineEventHandler(async (event) => {
  const slug = getRouterParam(event, 'slug')

  if (!slug || !/^[a-z0-9-]+$/.test(slug)) {
    throw createError({ statusCode: 400, statusMessage: 'Invalid slug' })
  }

  const filePath = join(process.cwd(), 'content', 'book', `${slug}.md`)

  await unlink(filePath)
  gitSyncContent(`Suppression chapitre ${slug}`, [`content/book/${slug}.md`])

  return { ok: true }
})
