import type { CollectionConfig } from 'payload'
import { area, fromPlatform, group, list, plain, txt } from '../fields'

// The catalogue. Each course has its own page at /courses/<slug> (and /en/courses/<slug>).
export const Courses: CollectionConfig = {
  slug: 'courses',
  labels: { singular: { ar: 'منهج', en: 'Course' }, plural: { ar: 'المناهج', en: 'Courses' } },
  admin: {
    useAsTitle: 'title',
    defaultColumns: ['title', 'status', 'order', 'slug'],
    group: { ar: 'المحتوى', en: 'Content' },
  },
  access: { read: () => true },
  defaultSort: 'order',
  fields: [
    {
      type: 'row',
      fields: [
        plain('slug', 'المعرّف في الرابط', 'Address slug', {
          required: true,
          unique: true,
          index: true,
          validate: (v: string | null | undefined) =>
            /^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(v || '') || 'حروف إنجليزية صغيرة وأرقام وشرطات فقط / lowercase letters, digits and hyphens only',
          admin: { width: '40%' },
        }),
        fromPlatform({
          name: 'status',
          type: 'select',
          required: true,
          defaultValue: 'soon',
          label: { ar: 'الحالة', en: 'Status' },
          options: [
            { value: 'live', label: { ar: 'متاح الآن', en: 'Available now' } },
            { value: 'partial', label: { ar: 'متاح جزئياً', en: 'Partly available' } },
            { value: 'soon', label: { ar: 'قريباً', en: 'Coming soon' } },
          ],
          admin: { width: '30%' },
        }),
        fromPlatform({
          name: 'order',
          type: 'number',
          required: true,
          defaultValue: 10,
          label: { ar: 'الترتيب', en: 'Order' },
          admin: { width: '30%' },
        }),
      ],
    },
    fromPlatform(txt('title', 'اسم المنهج', 'Course title', { required: true })),
    fromPlatform(txt('titleShort', 'اسم مختصر (لعنوان الصفحة)', 'Short title (for the page title)')),
    fromPlatform(area('tagline', 'الوصف المختصر', 'Tagline', { required: true })),
    {
      type: 'tabs',
      tabs: [
        {
          label: { ar: 'التفاصيل', en: 'Details' },
          fields: [
            fromPlatform(area('audience', 'لمن هذا المنهج', 'Who it is for')),
            fromPlatform(txt('level', 'المستوى', 'Level')),
            fromPlatform(txt('instruction', 'لغة الشرح', 'Language of instruction')),
            fromPlatform(list('facts', 'حقائق سريعة', 'Quick facts', [txt('text', 'النص', 'Text', { required: true, localized: false })], {
              admin: { description: { ar: 'أول اثنتين تظهران على بطاقة المنهج.', en: 'The first two show on the course card.' } },
            })),
            fromPlatform(list('includes', 'المنهج يضم', 'What is included', [txt('text', 'النص', 'Text', { required: true, localized: false })])),
            fromPlatform(list('outcomes', 'بعد إتمام المنهج تستطيع أن', 'Outcomes', [txt('text', 'النص', 'Text', { required: true, localized: false })])),
            fromPlatform(area('availability', 'حالة النشر', 'Publishing status')),
          ],
        },
        {
          label: { ar: 'الوحدات', en: 'Units' },
          fields: [
            fromPlatform({
              name: 'units',
              type: 'array',
              label: { ar: 'الوحدات', en: 'Units' },
              labels: { singular: { ar: 'وحدة', en: 'Unit' }, plural: { ar: 'وحدات', en: 'Units' } },
              admin: { initCollapsed: true },
              fields: [
                {
                  type: 'row',
                  fields: [
                    { name: 'n', type: 'number', required: true, label: { ar: 'رقم الوحدة', en: 'Unit number' }, admin: { width: '20%' } },
                    txt('name', 'اسم الوحدة', 'Unit name', { required: true, admin: { width: '80%' } }),
                  ],
                },
                {
                  type: 'row',
                  fields: [
                    {
                      name: 'kind',
                      type: 'select',
                      defaultValue: 'lessons',
                      label: { ar: 'النوع', en: 'Kind' },
                      options: [
                        { value: 'lessons', label: { ar: 'دروس', en: 'Lessons' } },
                        { value: 'labs', label: { ar: 'معامل', en: 'Labs' } },
                        { value: 'projects', label: { ar: 'مشروعات', en: 'Projects' } },
                      ],
                    },
                    {
                      name: 'source',
                      type: 'select',
                      defaultValue: 'book',
                      label: { ar: 'المصدر', en: 'Source' },
                      // The stored values stay `book` and `added` — those are names in the code, not words anyone
                      // reads — but the labels no longer say «كتاب», because a work course has none.
                      options: [
                        { value: 'book', label: { ar: 'من المنهج نفسه', en: 'Part of the course itself' } },
                        { value: 'added', label: { ar: 'وحدة مضافة', en: 'Added unit' } },
                      ],
                    },
                    { name: 'lessons', type: 'number', required: true, label: { ar: 'عدد الدروس', en: 'Lessons' } },
                    {
                      name: 'book',
                      type: 'number',
                      label: { ar: 'منها من المنهج نفسه', en: 'Of which part of the course itself' },
                      admin: { description: { ar: 'اتركه فارغاً إن كانت كل الدروس من المنهج نفسه.', en: 'Leave empty when every lesson is part of the course itself.' } },
                    },
                  ],
                },
              ],
            }),
          ],
        },
        {
          label: { ar: 'السعر والتسجيل', en: 'Price and enrolment' },
          fields: [
            fromPlatform(group('price', 'السعر', 'Price', [
              {
                name: 'amount',
                type: 'number',
                min: 0,
                label: { ar: 'المبلغ', en: 'Amount' },
                admin: { description: { ar: 'السعر العادي. تُقرأ هذه الحقول الثلاثة من المنصة إن كان للدورة رابط عليها، لأن الدفع يتم هناك؛ وتُستخدم القيم المكتوبة هنا إن تعذّر الوصول إليها. اتركه فارغاً ليظهر النص أدناه بدل السعر.', en: 'The normal price. These three fields are read from the learning platform when the course has a link to it, because that is where payment happens; what is written here is used when the platform cannot be reached. Leave empty to show the note below instead of a price.' } },
              },
              {
                name: 'offer',
                type: 'number',
                min: 0,
                label: { ar: 'سعر العرض', en: 'Offer price' },
                admin: {
                  description: {
                    ar: 'إن وُضع مع تاريخ الانتهاء، يظهر هذا السعر والسعر العادي مشطوباً بجانبه. وبعد التاريخ يعود السعر العادي وحده، دون تدخل. يُقرأ من المنصة إن كان للدورة رابط عليها.',
                    en: 'With an end date, this price shows with the normal one struck through beside it. After the date the normal price returns on its own.',
                  },
                },
              },
              {
                name: 'offerEnds',
                type: 'date',
                label: { ar: 'العرض ساري حتى', en: 'Offer runs until' },
                admin: {
                  date: { pickerAppearance: 'dayOnly', displayFormat: 'd MMMM yyyy' },
                  description: {
                    ar: 'آخر يوم في العرض. اليوم نفسه محسوب داخل العرض.',
                    en: 'The last day of the offer. That day is itself inside the offer.',
                  },
                },
              },
              txt('note', 'ملاحظة السعر', 'Price note'),
            ])),
            fromPlatform(plain('platformUrl', 'رابط المنهج على المنصة', 'Course address on the learning platform', {
              admin: {
                description: {
                  ar: 'إن وُضع، يذهب زر التسجيل إلى صفحة المنهج على المنصة (التسجيل والدفع بإنستاباي). وإلا يذهب إلى نموذج «سجّل اهتمامك».',
                  en: 'When set, the enrol button goes to the course on the learning platform (sign-up and InstaPay payment). Otherwise it goes to the interest form.',
                },
              },
            })),
          ],
        },
        {
          label: { ar: 'المعاينة', en: 'Preview' },
          fields: [
            { name: 'poster', type: 'upload', relationTo: 'media', label: { ar: 'صورة المعاينة', en: 'Preview image' } },
            txt('posterNote', 'تعليق الصورة', 'Image caption'),
            plain('videoUrl', 'رابط فيديو الدرس المجاني', 'Free lesson video address', {
              admin: { description: { ar: 'رابط يوتيوب أو المنصة. إن وُضع تصبح صورة المعاينة رابطاً إليه.', en: 'A YouTube or platform link. When set, the preview image links to it.' } },
            }),
            {
              // the free lessons the visitor can watch before paying: one row per lesson, in order.
              // The address and the image are the same in both languages; only the title is translated.
              name: 'freeLessons',
              type: 'array',
              label: { ar: 'الدروس المجانية', en: 'Free lessons' },
              labels: { singular: { ar: 'درس', en: 'Lesson' }, plural: { ar: 'دروس', en: 'Lessons' } },
              admin: {
                description: {
                  ar: 'تظهر في قسم «شوف الدروس المجانية» في صفحة المنهج. الضغط على الدرس يفتحه على منصة التعلّم، حيث يُشغَّل الفيديو ومعه ملفات الدرس وتدريباته.',
                  en: 'They appear in the "free lessons" section of the course page. Tapping one opens it on the learning platform, where the video plays beside the lesson files and its practice.',
                },
                initCollapsed: true,
              },
              fields: [
                {
                  type: 'row',
                  fields: [
                    txt('title', 'عنوان الدرس', 'Lesson title', { required: true, admin: { width: '55%' } }),
                    plain('code', 'رقم الدرس', 'Lesson number', {
                      admin: { width: '45%', description: { ar: 'مثل 1-1. يظهر فوق العنوان.', en: 'For example 1-1. It shows above the title.' } },
                    }),
                  ],
                },
                plain('videoUrl', 'رابط يوتيوب', 'YouTube address', { required: true }),
                plain('platformUrl', 'رابط الدرس على المنصة', 'Lesson address on the platform', {
                  admin: {
                    description: {
                      ar: 'اختياري. الضغط على الدرس يروح هنا. لو فاضي، يروح لصفحة المنهج على المنصة.',
                      en: 'Optional. Tapping the lesson goes here. Left empty, it goes to the course on the platform.',
                    },
                  },
                }),
                { name: 'poster', type: 'upload', relationTo: 'media', label: { ar: 'صورة الدرس', en: 'Lesson image' } },
              ],
            },
            {
              name: 'tone',
              type: 'select',
              defaultValue: 'royal-700',
              label: { ar: 'لون البطاقة عند غياب الصورة', en: 'Card colour when there is no image' },
              options: ['royal-700', 'royal-800', 'azure-700', 'navy-900', 'navy-700'].map((v) => ({ value: v, label: v })),
            },
          ],
        },
      ],
    },
  ],
}
