/**
 * Content update, 29 September 2026: the site stops being one course's site.
 *
 * Thinkera is widening from a single Grade 11 programming course to courses across subjects — programming,
 * English, Arabic, business, and more — and across stages, from preparatory school through the three years of
 * secondary. Everything the home page said was written for one course and one grade: the eyebrow named the
 * grade, the headline named the subject, the three figures counted that course's units and lessons, the bar
 * compared its 23 book lessons with its 9 added ones, and one answer in the FAQ priced it.
 *
 * What this rewrite keeps is the argument, because the argument was never about programming: the official
 * syllabus in full, what the syllabus skips added on top of it and marked as an addition, and questions that
 * ask a student to think rather than to recall. What it changes is everything that assumed there was only one
 * of them. Per-course numbers move to the course pages, where they are true.
 *
 * It also brings the home page up to what the platform actually does now: a lesson is four steps, not three,
 * and the assistant that answers from the course's own lessons is worth a step of its own.
 *
 *   docker compose --profile tools run --rm -e NODE_ENV=production tools npx tsx src/scripts/2026-09-29-all-stages.ts
 *
 * Safe to re-run. Before changing anything it writes the current content to
 * src/scripts/backup-2026-09-29-all-stages.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>

// ── the backup, before anything is touched ───────────────────────────────────────────────────────────────
const SLUGS = ['settings', 'home', 'catalogue']
const backup: Record<string, unknown> = {}
for (const slug of SLUGS) {
  for (const locale of ['ar', 'en'] as L[]) backup[`${slug}.${locale}`] = await get(slug, locale)
}
const file = path.join(process.cwd(), 'src/scripts/backup-2026-09-29-all-stages.json')
fs.writeFileSync(file, JSON.stringify(backup, null, 2), 'utf8')
console.log(`backup → ${file}`)

// ── the copy ─────────────────────────────────────────────────────────────────────────────────────────────
const AR = {
  settings: {
    footer: {
      tagline:
        'مناهج المرحلتين الإعدادية والثانوية على طريقة واحدة: مقرّرك الرسمي كامل، وفوقه اللي بيجهّزك لما بعد الامتحان.',
    },
  },
  home: {
    seo: {
      title: 'أكاديمية Thinkera | مناهج الإعدادي والثانوي: تفكير مش حفظ',
      description:
        'مناهج للمرحلتين الإعدادية والثانوية في أكتر من مادة، كلها بطريقة واحدة: مقرّرك الرسمي درس درس، وفوقه اللي المقرر بيعدّي عليه بسرعة. شرح بالعامية المصرية، وملفات وتقييمات وتدريب، ومساعد ذكي بيجاوبك من دروس منهجك. والوحدة الأولى كاملة مجاناً في أي منهج.',
    },
    eyebrow: 'الإعدادي والثانوي · البكالوريا المصرية',
    h1: 'Thinkera بتغيّر طريقة مذاكرتك.',
    h1Accent: ' مش بنحفّظك… بنعلّمك تفكّر.',
    lead:
      'مش منهج واحد ولا مادة واحدة. أي منهج هنا — برمجة، إنجليزي، عربي، إدارة أعمال، وغيرها — مبني بنفس الطريقة: مقرّرك الرسمي كامل بترتيبه ومصطلحاته، وفوقه دروس إضافية للي المقرر بيمرّ عليه بسرعة. وكل درس معاه شرح وملفات وتقييمات وتدريب، ومساعد ذكي بيجاوب أسئلتك من دروس منهجك نفسه.',
    facts: [
      { n: '4', label: 'خطوات في كل درس' },
      { n: '1', label: 'وحدة كاملة مجاناً في أي منهج' },
      { n: '2', label: 'مرحلة: إعدادي وثانوي' },
    ],
    bookTitle: 'المقرر مش السقف.',
    bookLead:
      'الامتحان بيقف عند المقرر الرسمي، وإحنا عارفين ده كويس، فالمقرر عندنا كامل بترتيبه ومصطلحاته. بس اللي بيخلّص مرحلته بالمقرر لوحده بيلاقي نفسه بيبدأ من الصفر في اللي بعدها. عشان كده في كل منهج بنبني فوق المقرر، وبنوضّح كل مرة إيه اللي هتتمتحن فيه وإيه اللي زيادة.',
    bookBarTitle: '',
    bookBar: [],
    bookCode: '',
    layersTitle: 'إزاي تعرف اللي هتتمتحن فيه',
    layers: [
      { badge: 'بلا شارة', name: 'نص المقرر', text: 'زي ما هو حرف بحرف، بترقيمه ومصطلحه. ده اللي هتتمتحن فيه.', plain: true },
      { badge: 'DEPTH', name: 'عمق', text: 'اللي المقرر ذكره في سطر وما شرحوش. بنحصره بند بند، وبنبني لكل بند شرح بآليته ومثاله.', plain: false },
      { badge: 'BEYOND', name: 'ما بعد الامتحان', text: 'اللي المقرر ما ذكرهوش: الدروس اللي زوّدناها جوّه الوحدات، والمسارات المتقدمة بعد الامتحان.', plain: false },
    ],
    paths:
      'مسارين في منهج واحد: الأساسي للامتحان، والموسّع بعده. ذاكر اللي هتتمتحن فيه دلوقتي، وارجع للباقي بعد الامتحان من غير ما تبدأ من الأول.',
    stepsTitle: 'كل درس في أربع خطوات',
    steps: [
      { n: '1', title: 'اتفرّج على الشرح', text: 'فيديو بالعامية المصرية، وفي النسخة التفاعلية بيقف ويسألك قبل ما يكمّل.' },
      { n: '2', title: 'افتح ملفات الدرس', text: 'الملزمة وشيت الأسئلة وإجاباته، بتتفتح جوّه الصفحة وتقدر تنزّلها.' },
      { n: '3', title: 'شوف التقييمات', text: 'فيديو قصير بيوريك تستخدم الشيت والإجابات إزاي: تحلّ الأول، وتصحّح بعدين.' },
      { n: '4', title: 'اتدرّب', text: 'اختبارات بتتصحّح وتديك درجة، وتدريب تفاعلي تتمرّن بيه من غير درجة.' },
    ],
    coursesTitle: 'المناهج',
    coursesLead:
      'المناهج متقسّمة بالتصنيف والمرحلة، وكل منهج كاتب حالته بصراحة: إيه المتاح دلوقتي، وإيه اللي لسه بيتجهّز.',
    ctaTitle: 'ابدأ الوحدة الأولى، مجاناً',
    ctaLead: 'سجّل في دقيقة، وبعدها الوحدة الأولى كاملة بدروسها وملفاتها وأسئلتها. اتفرّج وقارن، وبعدين قرّر.',
  },
  catalogue: {
    lead:
      'اختار مرحلتك والمادة اللي محتاجها. المناهج مرتّبة بتصنيفها، وكل منهج كاتب حالته بصراحة: إيه المتاح دلوقتي، وإيه اللي لسه بيتجهّز.',
    seo: {
      title: 'المناهج | أكاديمية Thinkera',
      description: 'مناهج أكاديمية Thinkera للمرحلتين الإعدادية والثانوية، مرتّبة بتصنيفها: إيه المتاح دلوقتي وإيه اللي بيتجهّز.',
    },
    labels: {
      previewNote: 'الدرس الأول كامل، بالعامية المصرية وترجمة نصية بالعربي والإنجليزي، زي ما الطالب هيشوفه بالظبط.',
    },
  },
}

const EN = {
  settings: {
    footer: {
      tagline:
        'Courses for preparatory and secondary school, all built one way: your official syllabus in full, and above it what prepares you for after the exam.',
    },
  },
  home: {
    seo: {
      title: 'Thinkera Academy | Preparatory and secondary courses: thinking, not memorising',
      description:
        'Courses for preparatory and secondary school in more than one subject, all built one way: your official syllabus lesson by lesson, and above it what the syllabus passes over. An explanation in Egyptian Arabic, files, assessments and practice, and an AI assistant that answers from your own course. The whole of Unit 1 is free in every course.',
    },
    eyebrow: 'Preparatory and secondary · Egyptian Baccalaureate',
    h1: 'Thinkera changes how you study.',
    h1Accent: ' Not memorising. Thinking.',
    lead:
      'Not one course and not one subject. Every course here — programming, English, Arabic, business and more — is built the same way: your official syllabus in full, in its own order and its own terms, with extra lessons above it wherever the syllabus passes something over quickly. Every lesson has an explanation, files, assessments and practice, and an AI assistant that answers your questions from your own course.',
    facts: [
      { n: '4', label: 'steps in every lesson' },
      { n: '1', label: 'whole unit free in every course' },
      { n: '2', label: 'stages: preparatory and secondary' },
    ],
    bookTitle: 'The syllabus is not the ceiling.',
    bookLead:
      'The exam stops at the official syllabus, and we know it: the syllabus is here in full, in its order and its terms. But a student who finishes a stage on the syllabus alone starts the next one from nothing. So in every course we build above it, and every time we make clear what is examinable and what is extra.',
    bookBarTitle: '',
    bookBar: [],
    bookCode: '',
    layersTitle: 'How you know what you will be examined on',
    layers: [
      { badge: 'no badge', name: 'The syllabus itself', text: 'Word for word, with its numbering and its terms. This is what you are examined on.', plain: true },
      { badge: 'DEPTH', name: 'Depth', text: 'What the syllabus names in a line and never explains. We list it item by item and build each one an explanation with its mechanism and its example.', plain: false },
      { badge: 'BEYOND', name: 'After the exam', text: 'What the syllabus does not mention: the lessons we added inside the units, and the advanced tracks that come after the exam.', plain: false },
    ],
    paths:
      'Two paths in one course: the core one for the exam, and the wider one after it. Study what you are examined on now, and come back to the rest without starting again.',
    stepsTitle: 'Every lesson in four steps',
    steps: [
      { n: '1', title: 'Watch the explanation', text: 'A video in Egyptian Arabic; the interactive version stops and asks you before it carries on.' },
      { n: '2', title: 'Open the lesson files', text: 'The booklet, the question sheet and its answers, shown in the page and ready to download.' },
      { n: '3', title: 'See the assessments', text: 'A short video showing how to use the sheet and its answers: answer first, mark second.' },
      { n: '4', title: 'Practise', text: 'Quizzes that are marked, and interactive practice to work through without a mark.' },
    ],
    coursesTitle: 'Courses',
    coursesLead:
      'Courses are grouped by subject and stage, and each one states its position plainly: what is available now and what is still being prepared.',
    ctaTitle: 'Start Unit 1, free',
    ctaLead: 'Register in a minute, and the whole of Unit 1 is yours — its lessons, its files and its questions. Watch, compare, then decide.',
  },
  catalogue: {
    lead:
      'Pick your stage and the subject you need. Courses are grouped by subject, and each one states its position plainly: what is available now and what is still being prepared.',
    seo: {
      title: 'Courses | Thinkera Academy',
      description: 'Thinkera Academy courses for preparatory and secondary school, grouped by subject: what is available now and what is coming.',
    },
    labels: {
      previewNote: 'The first lesson in full, in Egyptian Arabic with Arabic and English subtitles, exactly as a student sees it.',
    },
  },
}

// ── apply ────────────────────────────────────────────────────────────────────────────────────────────────
/** Merge one level deep so a group (seo, footer, labels) keeps the fields this script does not name. */
const merge = (now: any, next: any) => {
  const out: any = {}
  for (const [k, v] of Object.entries(next)) {
    out[k] = v && typeof v === 'object' && !Array.isArray(v) ? { ...(now?.[k] || {}), ...(v as object) } : v
  }
  return out
}

for (const [locale, copy] of [['ar', AR], ['en', EN]] as [L, any][]) {
  for (const slug of SLUGS) {
    if (!copy[slug]) continue
    const now = await get(slug, locale)
    await set(slug, locale, merge(now, copy[slug]))
    console.log(`${slug}.${locale} ← ${Object.keys(copy[slug]).join(', ')}`)
  }
}

// The brand-guidelines page was taken down on 27 September 2026, but its footer link stayed in the database
// and has been a dead link since. It goes with this pass.
for (const locale of ['ar', 'en'] as L[]) {
  const s = await get('settings', locale)
  const links = (s.footer?.links || []).filter((l: any) => !/الهوية|brand/i.test(`${l.label} ${l.href}`))
  if (links.length !== (s.footer?.links || []).length) {
    await set('settings', locale, { footer: { ...s.footer, links } })
    console.log(`settings.${locale} ← dropped the brand-guidelines footer link`)
  }
}

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