/**
 * Put a new picture in the home page's hero frame.
 *
 *     ./tk cms-tools npx payload run src/seed/hero-image.ts src/seed/_hero.webp
 *
 * Uploads the file as a Media row — so Payload makes its `card` and `wide` sizes and the site can use
 * them — and points `home.heroImage` at it. The old row is left alone: nothing is deleted, and the
 * previous picture can be chosen again in /admin at any time.
 *
 * `alt` is required on Media and is read aloud by a screen reader, so it is written here in both
 * languages rather than left to be filled in later. The hero art is decorative in the layout
 * (`aria-hidden` on the frame), but the row's alt still has to be right: the same image may be used
 * somewhere else tomorrow.
 */
import { getPayload } from 'payload'
import config from '@payload-config'
import path from 'path'

const ALT = {
  ar: 'أدوات التعلّم على منصة Thinkera: الدروس المصوّرة، والاختبارات، والمساعد الذكي، والفصول الافتراضية، وملفات الدرس.',
  en: 'The learning tools on the Thinkera platform: video lessons, quizzes, the AI assistant, live classes and the lesson files.',
}

const file = process.argv[2]
if (!file) {
  console.error('usage: payload run src/seed/hero-image.ts <path to the image inside the container>')
  process.exit(1)
}

const payload = await getPayload({ config })
const filePath = path.resolve(file)

// Created in Arabic, then the English alt written onto the same row: Payload keeps one file and one
// row per image, with a translation of the text beside it.
const media = await payload.create({
  collection: 'media',
  locale: 'ar',
  data: { alt: ALT.ar },
  filePath,
})
await payload.update({ collection: 'media', id: media.id, locale: 'en', data: { alt: ALT.en } })

const before = await payload.findGlobal({ slug: 'home', depth: 0 })
await payload.updateGlobal({ slug: 'home', data: { heroImage: media.id } as never })

payload.logger.info(`hero image: #${media.id} ${media.filename} (${media.width}x${media.height})`)
payload.logger.info(`home.heroImage: ${String(before.heroImage ?? 'none')} -> ${media.id}`)
