/**
 * Print what the site is saying now, in both languages, so a rewrite starts from the real text rather than
 * from the seed files (which only ever run on an empty database and have not been the source since).
 *
 *   docker compose --profile tools run --rm -e NODE_ENV=production tools npx tsx src/scripts/dump-content.ts
 *
 * Reads only. It writes one file, src/scripts/content-now.json, which is also the backup a rewrite restores
 * from if it has to be undone.
 */
import fs from 'node:fs'
import path from 'node:path'
import { getPayload } from 'payload'
import config from '@payload-config'

type L = 'ar' | 'en'
const GLOBALS = ['settings', 'home', 'catalogue', 'pricing', 'contactpage', 'about']
const payload = await getPayload({ config })

const out: Record<string, unknown> = {}
for (const slug of GLOBALS) {
  for (const locale of ['ar', 'en'] as L[]) {
    out[`${slug}.${locale}`] = await payload.findGlobal({ slug: slug as never, locale, depth: 0 })
  }
}
const courses = await payload.find({ collection: 'courses', limit: 100, depth: 0, locale: 'ar' })
out['courses.ar'] = courses.docs
out['courses.en'] = (await payload.find({ collection: 'courses', limit: 100, depth: 0, locale: 'en' })).docs

const file = path.join(process.cwd(), 'src/scripts/content-now.json')
fs.writeFileSync(file, JSON.stringify(out, null, 2), 'utf8')
console.log(`wrote ${file}`)
for (const k of Object.keys(out)) {
  const v = out[k] as unknown
  console.log(`  ${k}: ${Array.isArray(v) ? `${v.length} rows` : `${Object.keys(v as object).length} fields`}`)
}
process.exit(0)
