/**
 * Content update, 3 October 2026: the academy stops being a school academy.
 *
 * Thinkera now teaches two kinds of person: students at school, and people at work. The catalogue already
 * filters by track (the platform's own `tk_track` field), but every word on the site still spoke to a student
 * sitting an exam — «مقرّرك الرسمي», «الكتاب المدرسي هو المرجع», «اختار مرحلتك». A manager reading about
 * digital transformation has no syllabus, no textbook and no stage.
 *
 * The decision behind this script is «a promise per track». The school promise is the strongest writing on the
 * site and it stays, word for word; it only gains a heading that says whose promise it is. The work courses get
 * their own, in a section of equal weight. The hero above both says what is true of everything.
 *
 * Three things this script deliberately does NOT do, because each is a claim rather than a sentence:
 *   - it does not say which framework or standard a work course follows. That belongs on the course's own page,
 *     where it can be named with its edition and stand behind itself.
 *   - it does not widen «لجنة أكاديمية تراجع كل درس» to the work courses. That block is left alone and is the
 *     owner's to decide: the committee that reads a school syllabus is not the one that reads cybersecurity.
 *   - it does not relabel «الكتاب» wherever the site says it. Only the unit counter is made track-aware, in
 *     code; the blocks that are plainly about a school syllabus keep the word, because there it is true.
 *
 *   ./tk cms-tools npx tsx src/scripts/2026-10-03-two-tracks.ts
 *
 * Safe to re-run. Writes src/scripts/backup-2026-10-03-two-tracks.json first.
 */
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 locale of ['ar', 'en'] as L[]) {
  backup[`home.${locale}`] = await get('home', locale)
  backup[`catalogue.${locale}`] = await get('catalogue', locale)
  backup[`settings.${locale}`] = await get('settings', locale)
}
const file = path.join(process.cwd(), 'src/scripts/backup-2026-10-03-two-tracks.json')
fs.writeFileSync(file, JSON.stringify(backup, null, 2), 'utf8')
console.log(`backup → ${file}`)

// ── the home page ──────────────────────────────────────────────────────────────────────────────────────────

const HOME_AR = {
  eyebrow: 'للدراسة وللشغل · شرح بالعامية المصرية',
  h1: 'Thinkera بتغيّر طريقة تنمية مهاراتك.',
  h1Accent: ' مش بنحفّظك… بنعلّمك إزاي تفكّر وتكون مستعد وجاهز لبكره.',
  lead:
    'مش منهج واحد ولا مادة واحدة. برمجة وذكاء اصطناعي، إنجليزي وعربي، إدارة الأعمال، تحوّل رقمي، ' +
    'أمن سيبراني: لطلاب المدارس وللناس اللي في شغلها. بعضها متاح دلوقتي وبعضها لسه بيتجهّز، وكل منهج ' +
    'كاتب حالته على صفحته. وكلها مبنية بنفس الطريقة: كل درس معاه شرح بالعامية المصرية، وملفات، ' +
    'وتقييمات، وتدريب، ومساعد ذكي بيجاوب أسئلتك من دروس منهجك إنت.',

  // the school promise, unchanged but now named as the school's
  bookTitle: 'في مناهج الدراسة: المقرر مش السقف.',
  bookLead:
    'ده وعدنا في كل منهج دراسي. الامتحان بيقف عند المقرر الرسمي، وإحنا عارفين ده كويس، فالمقرر عندنا ' +
    'كامل بترتيبه ومصطلحاته. بس اللي بيخلّص مرحلته بالمقرر لوحده بيلاقي نفسه بيبدأ من الصفر في اللي ' +
    'بعدها. عشان كده في كل منهج بنبني فوق المقرر، وبنوضّح كل مرة إيه اللي هتتمتحن فيه وإيه اللي زيادة.',

  // the work promise, new
  workTitle: 'في مناهج الشغل: المرجع هو الشغل نفسه.',
  workLead:
    'منهج التحوّل الرقمي أو الأمن السيبراني مالوش كتاب تخلّصه لحد آخر صفحة. المرجع فيه هو الشغل نفسه: ' +
    'مواقف وقرارات حقيقية، وأدوات بتتستعمل فعلاً. وكل منهج كاتب على صفحته مرجعه بالاسم ولمين اتكتب، ' +
    'عشان تعرف قبل ما تبدأ ده مكانك ولا لأ.',

  coursesLead:
    'اختار الأول: المناهج لمين، للدراسة ولا للشغل؟ وبعدها المرحلة أو الجمهور. وكل منهج كاتب حالته ' +
    'بصراحة: إيه المتاح دلوقتي، وإيه اللي لسه بيتجهّز.',

  ctaLead:
    'كل مناهجنا على طريقة واحدة: شرح بالعامية، وملفات، وتقييمات، ومساعد بيجاوب من منهجك إنت. ' +
    'والوحدة الأولى كاملة مجاناً، من غير أي دفع.',
}

const HOME_EN = {
  eyebrow: 'For school and for work · taught in Egyptian Arabic',
  h1: 'Thinkera changes how you build your skills.',
  h1Accent: ' Not memorising. Learning how to think, and to be ready for tomorrow.',
  lead:
    'Not one course and not one subject. Programming and AI, English and Arabic, business administration, ' +
    'digital transformation, cybersecurity: for students at school and for people at work. Some are open now ' +
    'and some are still being built, and every course states where it stands on its own page. All of them are ' +
    'built the same way: every lesson has an explanation, files, assessments and practice, and an AI assistant ' +
    'that answers your questions from your own course.',

  bookTitle: 'In the school courses: the syllabus is not the ceiling.',
  bookLead:
    'This is our promise in every school course. 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.',

  workTitle: 'In the work courses: the work itself is the reference.',
  workLead:
    'A course in digital transformation or cybersecurity has no textbook to work through to the last page. ' +
    'Its reference is the work itself: real situations and real decisions, and tools people actually use. ' +
    'Every course names its own reference on its own page, and says who it was written for, so you know ' +
    'before you start whether it is for you.',

  coursesLead:
    'Start with who the courses are for, school or work, then the stage or the audience. Every course states ' +
    'plainly where it stands: what is open now and what is still being built.',

  ctaLead:
    'All our courses are built one way: an explanation in Egyptian Arabic, files, assessments, and an ' +
    'assistant that answers from your own course. The whole of Unit 1 is free, with nothing to pay.',
}

// The question a person at work asks first. Added once: re-running must not stack it, so it is matched by its
// question text rather than by position.
const FAQ_AR = {
  q: 'أنا مش طالب، أنا بشتغل. المناهج دي تنفعني؟',
  text:
    'أيوه. في مناهج مكتوبة للشغل مش للامتحان، زي التحوّل الرقمي والأمن السيبراني وإدارة الأعمال. ' +
    'اختار «للشغل» فوق صفحة المناهج، وكل منهج كاتب لمين اتكتب بالظبط.',
}
const FAQ_EN = {
  q: 'I am not a student, I am working. Are these courses for me?',
  text:
    'Yes. Some courses are written for work rather than for an exam: digital transformation, cybersecurity, ' +
    'business administration. Choose «For work» at the top of the courses page, and each course says exactly ' +
    'who it was written for.',
}

for (const [locale, copy, faq] of [['ar', HOME_AR, FAQ_AR], ['en', HOME_EN, FAQ_EN]] as const) {
  const current = await get('home', locale)
  const existing = (current.faq || []) as { q?: string; text?: string }[]
  const already = existing.some((f) => (f.q || '').trim() === faq.q)
  await set('home', locale, {
    ...copy,
    faq: already ? existing : [...existing, faq],
  })
  console.log(`home.${locale}: hero, both promises, courses lead, call to action${already ? '' : ', +1 question'}`)
}

// ── the courses page ───────────────────────────────────────────────────────────────────────────────────────

const CAT_AR = {
  lead:
    'اختار الأول: المناهج لمين، للدراسة ولا للشغل؟ وبعدها المرحلة أو الجمهور اللي يخصّك. المناهج مرتّبة ' +
    'بمادتها، وكل منهج كاتب حالته بصراحة: إيه المتاح دلوقتي، وإيه اللي لسه بيتجهّز.',
}
const CAT_EN = {
  lead:
    'Start with who the courses are for, school or work, then the stage or the audience that fits you. ' +
    'Courses are grouped by subject, and each one states plainly where it stands: what is open now and what ' +
    'is still being built.',
}

// «أساسي» is the word for the lessons that are the course itself, where «من الكتاب» cannot be said: a work
// course has no book, and «معتمد» would claim an accreditation nobody granted.
const LABELS_AR = { fromBook: 'من الكتاب', fromCore: 'أساسي' }
const LABELS_EN = { fromBook: 'from the book', fromCore: 'core' }

for (const [locale, copy, labels] of [['ar', CAT_AR, LABELS_AR], ['en', CAT_EN, LABELS_EN]] as const) {
  const current = await get('catalogue', locale)
  await set('catalogue', locale, { ...copy, labels: { ...(current.labels || {}), ...labels } })
  console.log(`catalogue.${locale}: lead, unit labels`)
}

// ── the site's own name in a search result ─────────────────────────────────────────────────────────────────

const SEO = {
  ar: 'أكاديمية Thinkera | مناهج للدراسة وللشغل: تفكير مش حفظ',
  en: 'Thinkera Academy | Courses for school and for work: thinking, not memorising',
}
const SEO_DESC = {
  ar:
    'مناهج للدراسة وللشغل بالعامية المصرية: برمجة وذكاء اصطناعي، إنجليزي وعربي، إدارة الأعمال، تحوّل رقمي، ' +
    'أمن سيبراني. كل درس معاه شرح وملفات وتقييمات وتدريب ومساعد ذكي. والوحدة الأولى كاملة مجاناً.',
  en:
    'Courses for school and for work, taught in Egyptian Arabic: programming and AI, English and Arabic, ' +
    'business administration, digital transformation, cybersecurity. Every lesson has an explanation, files, ' +
    'assessments, practice and an AI assistant. The whole of Unit 1 is free.',
}

for (const locale of ['ar', 'en'] as L[]) {
  const current = await get('home', locale)
  await set('home', locale, {
    seo: { ...(current.seo || {}), title: SEO[locale], description: SEO_DESC[locale] },
  })
  console.log(`home.${locale}: page title and description`)
}

console.log('\ndone. The academic-committee block and the «السبع وحدات» block were left alone on purpose.')
