import type { CollectionConfig } from 'payload'
import { authenticatedOnly, tenantScopedPublicRead } from '../lib/tenantAccess'

/**
 * Jobs Collection
 *
 * Stellenanzeigen und Karriere-Seite.
 * Unterstützt verschiedene Beschäftigungsarten, Standorte und Bewerbungsoptionen.
 */
export const Jobs: CollectionConfig = {
  slug: 'jobs',
  labels: {
    singular: 'Stellenanzeige',
    plural: 'Stellenanzeigen',
  },
  admin: {
    useAsTitle: 'title',
    group: 'Content',
    defaultColumns: ['title', 'department', 'type', 'location', 'status', 'publishedAt'],
    description: 'Stellenanzeigen und Karriere',
  },
  access: {
    read: tenantScopedPublicRead,
    create: authenticatedOnly,
    update: authenticatedOnly,
    delete: authenticatedOnly,
  },
  fields: [
    // Basis-Informationen
    {
      name: 'title',
      type: 'text',
      required: true,
      label: 'Stellentitel',
      localized: true,
      admin: {
        description: 'z.B. "Pflegefachkraft (m/w/d)", "Senior Developer"',
      },
    },
    {
      name: 'slug',
      type: 'text',
      required: true,
      unique: true,
      label: 'Slug',
    },
    {
      name: 'reference',
      type: 'text',
      label: 'Referenznummer',
      admin: {
        description: 'Interne Stellennummer',
      },
    },
    // Kategorisierung
    {
      name: 'department',
      type: 'text',
      label: 'Abteilung',
      localized: true,
      admin: {
        description: 'z.B. "Pflege", "IT", "Marketing"',
      },
    },
    {
      name: 'category',
      type: 'select',
      label: 'Kategorie',
      options: [
        { label: 'Pflege & Betreuung', value: 'care' },
        { label: 'Medizin', value: 'medical' },
        { label: 'Verwaltung', value: 'admin' },
        { label: 'IT & Technik', value: 'it' },
        { label: 'Marketing', value: 'marketing' },
        { label: 'Vertrieb', value: 'sales' },
        { label: 'Finanzen', value: 'finance' },
        { label: 'Personal', value: 'hr' },
        { label: 'Produktion', value: 'production' },
        { label: 'Logistik', value: 'logistics' },
        { label: 'Sonstiges', value: 'other' },
      ],
    },
    {
      name: 'type',
      type: 'select',
      required: true,
      defaultValue: 'fulltime',
      label: 'Beschäftigungsart',
      options: [
        { label: 'Vollzeit', value: 'fulltime' },
        { label: 'Teilzeit', value: 'parttime' },
        { label: 'Minijob', value: 'minijob' },
        { label: 'Werkstudent', value: 'working_student' },
        { label: 'Praktikum', value: 'internship' },
        { label: 'Ausbildung', value: 'apprenticeship' },
        { label: 'Duales Studium', value: 'dual_study' },
        { label: 'Freelance', value: 'freelance' },
        { label: 'Befristet', value: 'temporary' },
      ],
      admin: {
        position: 'sidebar',
      },
    },
    {
      name: 'workModel',
      type: 'select',
      label: 'Arbeitsmodell',
      options: [
        { label: 'Vor Ort', value: 'onsite' },
        { label: 'Remote', value: 'remote' },
        { label: 'Hybrid', value: 'hybrid' },
      ],
      admin: {
        position: 'sidebar',
      },
    },
    {
      name: 'experienceLevel',
      type: 'select',
      label: 'Erfahrungslevel',
      options: [
        { label: 'Berufseinsteiger', value: 'entry' },
        { label: 'Mit Berufserfahrung', value: 'experienced' },
        { label: 'Senior', value: 'senior' },
        { label: 'Führungskraft', value: 'management' },
      ],
    },
    // Standort
    {
      name: 'location',
      type: 'group',
      label: 'Standort',
      fields: [
        {
          name: 'locationRef',
          type: 'relationship',
          relationTo: 'locations',
          label: 'Standort (aus Collection)',
        },
        {
          name: 'city',
          type: 'text',
          label: 'Stadt',
          admin: {
            description: 'Falls kein Standort aus Collection',
          },
        },
        {
          name: 'region',
          type: 'text',
          label: 'Region/Bundesland',
        },
        {
          name: 'country',
          type: 'text',
          defaultValue: 'Deutschland',
          label: 'Land',
        },
      ],
    },
    // Beschreibung
    {
      name: 'summary',
      type: 'textarea',
      label: 'Kurzbeschreibung',
      localized: true,
      maxLength: 300,
      admin: {
        description: 'Für Übersichten und SEO (max. 300 Zeichen)',
      },
    },
    {
      name: 'description',
      type: 'richText',
      label: 'Beschreibung',
      localized: true,
    },
    // Strukturierte Abschnitte
    {
      name: 'responsibilities',
      type: 'richText',
      label: 'Aufgaben',
      localized: true,
    },
    {
      name: 'requirements',
      type: 'richText',
      label: 'Anforderungen',
      localized: true,
    },
    {
      name: 'qualifications',
      type: 'richText',
      label: 'Wünschenswerte Qualifikationen',
      localized: true,
    },
    {
      name: 'benefits',
      type: 'richText',
      label: 'Was wir bieten',
      localized: true,
    },
    // Benefits als Liste
    {
      name: 'benefitsList',
      type: 'array',
      label: 'Benefits (Stichpunkte)',
      admin: {
        description: 'Für visuelle Darstellung mit Icons',
      },
      fields: [
        {
          name: 'benefit',
          type: 'text',
          required: true,
          label: 'Benefit',
          localized: true,
        },
        {
          name: 'icon',
          type: 'select',
          label: 'Icon',
          options: [
            { label: 'Gehalt', value: 'money' },
            { label: 'Urlaub', value: 'vacation' },
            { label: 'Homeoffice', value: 'home' },
            { label: 'Weiterbildung', value: 'education' },
            { label: 'Gesundheit', value: 'health' },
            { label: 'Team', value: 'team' },
            { label: 'Flexibel', value: 'flexible' },
            { label: 'Jobticket', value: 'transport' },
            { label: 'Essen', value: 'food' },
            { label: 'Kinderbetreuung', value: 'childcare' },
            { label: 'Fitness', value: 'fitness' },
            { label: 'Parkplatz', value: 'parking' },
          ],
        },
      ],
    },
    // Gehalt
    {
      name: 'salary',
      type: 'group',
      label: 'Gehalt',
      fields: [
        {
          name: 'show',
          type: 'checkbox',
          defaultValue: false,
          label: 'Gehalt anzeigen',
        },
        {
          name: 'type',
          type: 'select',
          label: 'Typ',
          options: [
            { label: 'Jahresgehalt', value: 'yearly' },
            { label: 'Monatsgehalt', value: 'monthly' },
            { label: 'Stundenlohn', value: 'hourly' },
          ],
          admin: {
            condition: (_, siblingData) => siblingData?.show,
          },
        },
        {
          name: 'min',
          type: 'number',
          label: 'Von',
          admin: {
            condition: (_, siblingData) => siblingData?.show,
          },
        },
        {
          name: 'max',
          type: 'number',
          label: 'Bis',
          admin: {
            condition: (_, siblingData) => siblingData?.show,
          },
        },
        {
          name: 'currency',
          type: 'text',
          defaultValue: 'EUR',
          label: 'Währung',
          admin: {
            condition: (_, siblingData) => siblingData?.show,
          },
        },
        {
          name: 'note',
          type: 'text',
          label: 'Hinweis',
          localized: true,
          admin: {
            condition: (_, siblingData) => siblingData?.show,
            description: 'z.B. "je nach Qualifikation"',
          },
        },
      ],
    },
    // Bewerbung
    {
      name: 'application',
      type: 'group',
      label: 'Bewerbung',
      fields: [
        {
          name: 'method',
          type: 'select',
          defaultValue: 'email',
          label: 'Bewerbungsweg',
          options: [
            { label: 'Per E-Mail', value: 'email' },
            { label: 'Online-Formular', value: 'form' },
            { label: 'Externes Portal', value: 'external' },
            { label: 'Per Post', value: 'mail' },
          ],
        },
        {
          name: 'email',
          type: 'email',
          label: 'Bewerbungs-E-Mail',
          admin: {
            condition: (_, siblingData) => siblingData?.method === 'email',
          },
        },
        {
          name: 'formId',
          type: 'relationship',
          relationTo: 'forms',
          label: 'Bewerbungsformular',
          admin: {
            condition: (_, siblingData) => siblingData?.method === 'form',
          },
        },
        {
          name: 'externalUrl',
          type: 'text',
          label: 'Externes Portal (URL)',
          admin: {
            condition: (_, siblingData) => siblingData?.method === 'external',
          },
        },
        {
          name: 'contact',
          type: 'relationship',
          relationTo: 'team',
          label: 'Ansprechpartner',
        },
        {
          name: 'deadline',
          type: 'date',
          label: 'Bewerbungsfrist',
        },
      ],
    },
    // Media
    {
      name: 'image',
      type: 'upload',
      relationTo: 'media',
      label: 'Bild',
      admin: {
        description: 'Bild für die Stellenanzeige',
      },
    },
    // Datum & Status
    {
      name: 'status',
      type: 'select',
      required: true,
      defaultValue: 'draft',
      label: 'Status',
      options: [
        { label: 'Entwurf', value: 'draft' },
        { label: 'Veröffentlicht', value: 'published' },
        { label: 'Besetzt', value: 'filled' },
        { label: 'Archiviert', value: 'archived' },
      ],
      admin: {
        position: 'sidebar',
      },
    },
    {
      name: 'publishedAt',
      type: 'date',
      label: 'Veröffentlicht am',
      admin: {
        position: 'sidebar',
        date: {
          pickerAppearance: 'dayOnly',
        },
      },
    },
    {
      name: 'isFeatured',
      type: 'checkbox',
      defaultValue: false,
      label: 'Hervorgehoben',
      admin: {
        position: 'sidebar',
      },
    },
    {
      name: 'isUrgent',
      type: 'checkbox',
      defaultValue: false,
      label: 'Dringend zu besetzen',
      admin: {
        position: 'sidebar',
      },
    },
    // SEO
    {
      name: 'seo',
      type: 'group',
      label: 'SEO',
      fields: [
        {
          name: 'metaTitle',
          type: 'text',
          label: 'Meta-Titel',
          localized: true,
        },
        {
          name: 'metaDescription',
          type: 'textarea',
          label: 'Meta-Beschreibung',
          localized: true,
          maxLength: 160,
        },
      ],
    },
  ],
  hooks: {
    beforeChange: [
      ({ data }) => {
        if (data && !data.slug && data.title) {
          const titleStr = typeof data.title === 'string' ? data.title : ''
          data.slug = titleStr
            .toLowerCase()
            .replace(/[äöüß]/g, (match: string) => {
              const map: Record<string, string> = { ä: 'ae', ö: 'oe', ü: 'ue', ß: 'ss' }
              return map[match] || match
            })
            .replace(/\(m\/w\/d\)/gi, '')
            .replace(/[^a-z0-9]+/g, '-')
            .replace(/^-|-$/g, '')
        }
        return data
      },
    ],
  },
}
