// Small builders for the CMS fields. Every visible string is localized (Arabic first, English second);
// labels are bilingual so the admin reads naturally in either language.
import type { ArrayField, Field, GroupField, TextField, TextareaField } from 'payload'

type Label = { ar: string; en: string }
const L = (ar: string, en: string): Label => ({ ar, en })

export const txt = (name: string, ar: string, en: string, extra: Partial<TextField> = {}): TextField =>
  ({ name, type: 'text', label: L(ar, en), localized: true, ...extra }) as TextField

export const area = (name: string, ar: string, en: string, extra: Partial<TextareaField> = {}): TextareaField =>
  ({ name, type: 'textarea', label: L(ar, en), localized: true, ...extra }) as TextareaField

/** A plain (not translated) text field: numbers shown as text, addresses, codes. */
export const plain = (name: string, ar: string, en: string, extra: Partial<TextField> = {}): TextField =>
  ({ name, type: 'text', label: L(ar, en), ...extra }) as TextField

/** A repeating list whose rows differ per language (each language keeps its own rows). */
export const list = (name: string, ar: string, en: string, fields: Field[], extra: Partial<ArrayField> = {}): ArrayField =>
  ({ name, type: 'array', label: L(ar, en), localized: true, fields, ...extra }) as ArrayField

export const group = (name: string, ar: string, en: string, fields: Field[]): GroupField =>
  ({ name, type: 'group', label: L(ar, en), fields }) as GroupField

/** Title and description for search engines and link previews. */
export const seo = (): GroupField =>
  group('seo', 'بيانات محركات البحث', 'Search and sharing', [
    txt('title', 'عنوان الصفحة', 'Page title', { required: true }),
    area('description', 'الوصف', 'Description'),
  ])

/** A site link: a page of this site ("/courses", "/courses/bacc-en#preview") or a full address. */
export const href = (name = 'href', required = true): TextField =>
  plain(name, 'الرابط', 'Link', {
    required,
    admin: {
      description: {
        ar: 'صفحة من الموقع تبدأ بـ / (مثل ‎/courses‎) أو عنوان كامل يبدأ بـ https://. روابط الموقع تُفتح تلقائياً بلغة الصفحة.',
        en: 'A page of this site starting with / (like /courses) or a full address starting with https://. Site links open in the page language automatically.',
      },
    },
  })

/**
 * A course field whose value belongs to the learning platform. It stays in the CMS as the copy the site falls
 * back on when the platform cannot be reached, but it is not edited here: the admin shows it, read-only, with
 * a line saying where it is changed. It is never required, so a row made here only to give a platform course
 * its picture needs nothing but the course's address.
 */
export const fromPlatform = <T extends Field>(field: T): T => {
  const f = field as T & { required?: boolean; admin?: Record<string, unknown> }
  const note = {
    ar: 'يُدار من المنصة: إعدادات الدورة هناك. القيمة هنا نسخة احتياطية يستخدمها الموقع لو المنصة مش متاحة.',
    en: 'Managed on the learning platform, in the course settings there. This is the copy the site uses if the platform cannot be reached.',
  }
  const prior = (f.admin?.description as Label | undefined) || null
  return {
    ...f,
    required: false,
    admin: {
      ...(f.admin || {}),
      readOnly: true,
      description: prior ? { ar: `${note.ar} ${prior.ar}`, en: `${note.en} ${prior.en}` } : note,
    },
  } as T
}
