/**
 * Content update, 29 September 2026: every course gets its own picture on the site.
 *
 * The pictures exist — they were generated for each course and set in its settings on the learning platform.
 * The site could not use them: a course's overview file there is served behind a login, so an anonymous
 * visitor's browser gets a redirect where an image should be, and the card would show a broken image rather
 * than a picture. The feed now carries the address, which is right for anything that can sign in, but the
 * public site needs a copy it can serve itself.
 *
 * So the five pictures were copied out of the platform into src/scripts/course-images and this loads them
 * into the CMS as media, one per course, and sets each course's poster. After it has run the site serves the
 * pictures from its own media, and a course added later gets its poster the same way or through the admin.
 *
 *   docker compose --profile tools run --rm -e NODE_ENV=production tools npx tsx src/scripts/2026-09-29-course-posters.ts
 *
 * Safe to re-run: a course that already has a poster is left alone, and a picture already loaded is reused.
 */
import fs from 'node:fs'
import path from 'node:path'
import { getPayload } from 'payload'
import config from '@payload-config'

const payload = await getPayload({ config })
const dir = path.join(process.cwd(), 'src/scripts/course-images')

const PICTURES: Record<string, { file: string; ar: string; en: string }> = {
  'bacc-en': {
    file: 'BACC-EN-PROGRAMMING.jpg',
    ar: 'غلاف منهج البرمجة والذكاء الاصطناعي لمدارس اللغات',
    en: 'Cover for the programming and AI course for language schools',
  },
  'bacc-ar': {
    file: 'BACC-AR-PROGRAMMING.jpg',
    ar: 'غلاف منهج البرمجة والذكاء الاصطناعي بشاشات عربية',
    en: 'Cover for the programming and AI course with Arabic screens',
  },
  kids: {
    file: 'KIDS-PROGRAMMING.jpg',
    ar: 'غلاف منهج البرمجة للأطفال',
    en: 'Cover for the programming course for children',
  },
  advanced: {
    file: 'ADVANCED-PROGRAMMING.jpg',
    ar: 'غلاف منهج البرمجة المتقدمة والتأهيل لسوق العمل',
    en: 'Cover for the advanced programming course',
  },
  'ai-daily': {
    file: 'AI-DAILY.jpg',
    ar: 'غلاف منهج الذكاء الاصطناعي في حياتنا اليومية',
    en: 'Cover for the AI in everyday life course',
  },
}

for (const [slug, pic] of Object.entries(PICTURES)) {
  const filePath = path.join(dir, pic.file)
  if (!fs.existsSync(filePath)) {
    console.log(`${slug}: ${pic.file} is not here, skipped`)
    continue
  }
  const found = await payload.find({ collection: 'courses', where: { slug: { equals: slug } }, limit: 1, depth: 0 })
  const course = found.docs[0]
  if (!course) {
    console.log(`${slug}: no CMS row, skipped`)
    continue
  }
  // A poster left over from the static site is not a poster: bacc-en wore a still from lesson 1-1, ai-daily
  // wore its old preview, and bacc-ar wore *the English course's* preview. The platform now has a cover made
  // for each course, so REPLACE=1 puts those in place of whatever is there.
  if (course.poster && !process.env.REPLACE) {
    console.log(`${slug}: already has a poster, left alone (REPLACE=1 to replace it)`)
    continue
  }

  const already = await payload.find({ collection: 'media', where: { filename: { equals: pic.file } }, limit: 1, depth: 0 })
  const media =
    already.docs[0] ||
    (await payload.create({ collection: 'media', data: { alt: pic.ar }, filePath, locale: 'ar' }))
  if (!already.docs[0]) {
    // the alt text is what a reader who cannot see the picture is told, so it is written in both languages
    await payload.update({ collection: 'media', id: media.id, data: { alt: pic.en }, locale: 'en' })
  }

  await payload.update({ collection: 'courses', id: course.id, data: { poster: media.id }, depth: 0 })
  console.log(`${slug}: poster ← ${pic.file} (media ${media.id})`)
}

console.log('done')
process.exit(0)
