import { existsSync } from 'node:fs'
import { readFile, writeFile, mkdir } from 'node:fs/promises'
import { join } from 'node:path'

// Seeds data/messages.yml from site/messages.yml on first boot (or after data loss).
// Only runs if data/messages.yml is absent — never overwrites existing runtime data.
export default defineNitroPlugin(async () => {
  const dataFile = join(process.cwd(), 'data', 'messages.yml')
  if (existsSync(dataFile)) return

  const seedFile = join(process.cwd(), 'site', 'messages.yml')
  if (!existsSync(seedFile)) return

  await mkdir(join(process.cwd(), 'data'), { recursive: true })
  const seed = await readFile(seedFile, 'utf-8')
  await writeFile(dataFile, seed, 'utf-8')
  console.log('[seed-messages] data/messages.yml initialisé depuis site/messages.yml')
})
