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' },
                      options: [
                        { value: 'book', label: { ar: 'من الكتاب', en: 'From the book' } },
                        { 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 from the book' },
                      admin: { description: { ar: 'اتركه فارغاً إن كانت كل الدروس من الكتاب.', en: 'Leave empty when every lesson is from the book.' } },
                    },
                  ],
                },
              ],
            }),
          ],
        },
        {
          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. Each one opens inside the page.',
                },
                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 }),
                { 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 })),
            },
          ],
        },
      ],
    },
  ],
}
