/**
 * Content update, 19 September 2026: the course price is final, 299 EGP for the full course (the owner's decision).
 * The platform sells the full course only (InstaPay, 299 EGP), so the pricing page keeps two plans: Unit 1 free and
 * the full course. The "One unit" plan is removed (it showed the full-course price); the payment answer in the FAQ
 * and the pricing lead now carry the amount; the interest form no longer promises to "send the price"; the plans heading drops "suggested".
 *
 *   docker compose --profile tools run --rm -e NODE_ENV=production tools npx tsx src/scripts/2026-09-19-price-299.ts
 *
 * Safe to re-run. Before changing anything it writes the current content to src/scripts/backup-2026-09-19-price.json.
 */
import fs from 'node:fs'
import path from 'node:path'
import { getPayload } from 'payload'
import config from '@payload-config'

type L = 'ar' | 'en'
const payload = await getPayload({ config })
const get = (slug: string, locale: L) => payload.findGlobal({ slug: slug as never, locale, depth: 0 }) as Promise<any>
const set = (slug: string, locale: L, data: any) =>
  payload.updateGlobal({ slug: slug as never, locale, data, depth: 0 }) as Promise<any>

const backup: Record<string, unknown> = {}
for (const g of ['home', 'pricing']) backup[g] = { ar: await get(g, 'ar'), en: await get(g, 'en') }
fs.writeFileSync(path.join(process.cwd(), 'src/scripts/backup-2026-09-19-price.json'), JSON.stringify(backup, null, 2))

const T = {
  ar: {
    oneUnit: 'وحدة واحدة',
    plansTitle: 'الخطط',
    lead: 'الوحدة الأولى كاملة مجاناً. والدورة كاملة بـ299 جنيه للعام الدراسي، والدفع بإنستاباي من المنصة.',
    seo: 'خطط الاشتراك في أكاديمية Thinkera: الوحدة الأولى كاملة مجاناً، والدورة كاملة بـ299 جنيه للعام الدراسي، والدفع بإنستاباي من المنصة.',
    formLead: 'املأ البيانات، وسنرسل لك مواعيد نشر الدروس الجديدة.',
    payQ: 'كيف أدفع؟',
    payA: 'الدورة كاملة بـ299 جنيه للعام الدراسي. تدفع من المنصة بإنستاباي: تحوّل المبلغ، وترفع صورة الإيصال، ونفتح لك الدورة بعد مراجعته. والوحدة الأولى لا تحتاج أي دفع.',
  },
  en: {
    oneUnit: 'One unit',
    plansTitle: 'Plans',
    lead: 'Unit 1 is free in full. The full course is 299 EGP for the school year, paid with InstaPay on the platform.',
    seo: 'Thinkera Academy plans: Unit 1 free in full, and the full course at 299 EGP for the school year, paid with InstaPay on the platform.',
    formLead: 'Fill this in and we will tell you when new lessons are published.',
    payQ: 'How do I pay?',
    payA: 'The full course is 299 EGP for the school year. You pay on the platform with InstaPay: transfer the amount, upload the receipt, and we open the course once it is checked. Unit 1 needs no payment at all.',
  },
}

for (const locale of ['ar', 'en'] as L[]) {
  const t = T[locale]
  const p = await get('pricing', locale)
  const plans = (p.plans || []).filter((pl: any) => pl.name !== t.oneUnit)
  // one field per call (two arrays or groups in one update hit a unique-id error on PostgreSQL)
  await set('pricing', locale, { plans })
  await set('pricing', locale, { plansTitle: t.plansTitle, lead: t.lead, formLead: t.formLead, seo: { ...p.seo, description: t.seo } })

  const h = await get('home', locale)
  const faq = (h.faq || []).map((f: any) => (f.q === t.payQ ? { ...f, text: t.payA } : f))
  await set('home', locale, { faq })
  console.log(`${locale}: pricing (${plans.length} plans) and the payment answer updated`)
}
process.exit(0)
