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

/**
 * Events Collection
 *
 * Veranstaltungen, Workshops, Seminare, Messen etc.
 * Unterstützt einmalige und wiederkehrende Events, Online/Offline,
 * Ticketing und Anmeldungen.
 */
export const Events: CollectionConfig = {
  slug: 'events',
  labels: {
    singular: 'Veranstaltung',
    plural: 'Veranstaltungen',
  },
  admin: {
    useAsTitle: 'title',
    group: 'Content',
    defaultColumns: ['title', 'eventType', 'startDate', 'location', 'status'],
    description: 'Veranstaltungen und Events',
  },
  access: {
    read: tenantScopedPublicRead,
    create: authenticatedOnly,
    update: authenticatedOnly,
    delete: authenticatedOnly,
  },
  fields: [
    // Basis-Informationen
    {
      name: 'title',
      type: 'text',
      required: true,
      label: 'Titel',
      localized: true,
    },
    {
      name: 'slug',
      type: 'text',
      required: true,
      unique: true,
      label: 'Slug',
      admin: {
        description: 'URL-Pfad für das Event',
      },
    },
    {
      name: 'subtitle',
      type: 'text',
      label: 'Untertitel',
      localized: true,
    },
    {
      name: 'description',
      type: 'richText',
      label: 'Beschreibung',
      localized: true,
    },
    {
      name: 'excerpt',
      type: 'textarea',
      label: 'Kurzbeschreibung',
      localized: true,
      admin: {
        description: 'Für Übersichtsseiten und SEO',
      },
    },
    // Event-Typ
    {
      name: 'eventType',
      type: 'select',
      required: true,
      defaultValue: 'event',
      label: 'Event-Typ',
      options: [
        { label: 'Veranstaltung', value: 'event' },
        { label: 'Workshop', value: 'workshop' },
        { label: 'Seminar', value: 'seminar' },
        { label: 'Webinar', value: 'webinar' },
        { label: 'Konferenz', value: 'conference' },
        { label: 'Messe', value: 'tradeshow' },
        { label: 'Networking', value: 'networking' },
        { label: 'Kurs', value: 'course' },
        { label: 'Vortrag', value: 'talk' },
        { label: 'Sonstiges', value: 'other' },
      ],
    },
    {
      name: 'category',
      type: 'text',
      label: 'Kategorie',
      admin: {
        description: 'Freies Textfeld für Kategorisierung',
      },
    },
    // Format
    {
      name: 'format',
      type: 'select',
      required: true,
      defaultValue: 'onsite',
      label: 'Format',
      options: [
        { label: 'Vor Ort', value: 'onsite' },
        { label: 'Online', value: 'online' },
        { label: 'Hybrid', value: 'hybrid' },
      ],
    },
    // Datum & Zeit
    {
      type: 'row',
      fields: [
        {
          name: 'startDate',
          type: 'date',
          required: true,
          label: 'Startdatum',
          admin: {
            date: {
              pickerAppearance: 'dayAndTime',
            },
            width: '50%',
          },
        },
        {
          name: 'endDate',
          type: 'date',
          label: 'Enddatum',
          admin: {
            date: {
              pickerAppearance: 'dayAndTime',
            },
            width: '50%',
            description: 'Leer lassen für eintägige Events',
          },
        },
      ],
    },
    {
      name: 'isAllDay',
      type: 'checkbox',
      defaultValue: false,
      label: 'Ganztägig',
    },
    {
      name: 'timezone',
      type: 'text',
      defaultValue: 'Europe/Berlin',
      label: 'Zeitzone',
      admin: {
        description: 'z.B. Europe/Berlin, America/New_York',
      },
    },
    // Wiederkehrende Events
    {
      name: 'isRecurring',
      type: 'checkbox',
      defaultValue: false,
      label: 'Wiederkehrendes Event',
    },
    {
      name: 'recurrence',
      type: 'group',
      label: 'Wiederholung',
      admin: {
        condition: (data, siblingData) => siblingData?.isRecurring,
      },
      fields: [
        {
          name: 'frequency',
          type: 'select',
          label: 'Häufigkeit',
          options: [
            { label: 'Täglich', value: 'daily' },
            { label: 'Wöchentlich', value: 'weekly' },
            { label: 'Monatlich', value: 'monthly' },
            { label: 'Jährlich', value: 'yearly' },
          ],
        },
        {
          name: 'interval',
          type: 'number',
          min: 1,
          defaultValue: 1,
          label: 'Intervall',
          admin: {
            description: 'Alle X Tage/Wochen/Monate/Jahre',
          },
        },
        {
          name: 'endRecurrence',
          type: 'date',
          label: 'Ende der Wiederholung',
        },
        {
          name: 'description',
          type: 'text',
          label: 'Wiederholungs-Beschreibung',
          localized: true,
          admin: {
            description: 'z.B. "Jeden ersten Montag im Monat"',
          },
        },
      ],
    },
    // Ort (für Vor-Ort Events)
    {
      name: 'location',
      type: 'group',
      label: 'Veranstaltungsort',
      admin: {
        condition: (data, siblingData) =>
          siblingData?.format === 'onsite' || siblingData?.format === 'hybrid',
      },
      fields: [
        {
          name: 'locationRef',
          type: 'relationship',
          relationTo: 'locations' as 'users',
          label: 'Standort auswählen',
          admin: {
            description: 'Aus bestehenden Standorten wählen',
          },
        },
        {
          name: 'customLocation',
          type: 'checkbox',
          defaultValue: false,
          label: 'Eigene Adresse angeben',
        },
        {
          name: 'venueName',
          type: 'text',
          label: 'Veranstaltungsort Name',
          localized: true,
          admin: {
            condition: (data, siblingData) => siblingData?.customLocation,
          },
        },
        {
          name: 'street',
          type: 'text',
          label: 'Straße',
          admin: {
            condition: (data, siblingData) => siblingData?.customLocation,
          },
        },
        {
          name: 'zip',
          type: 'text',
          label: 'PLZ',
          admin: {
            condition: (data, siblingData) => siblingData?.customLocation,
          },
        },
        {
          name: 'city',
          type: 'text',
          label: 'Stadt',
          admin: {
            condition: (data, siblingData) => siblingData?.customLocation,
          },
        },
        {
          name: 'country',
          type: 'text',
          defaultValue: 'Deutschland',
          label: 'Land',
          admin: {
            condition: (data, siblingData) => siblingData?.customLocation,
          },
        },
        {
          name: 'room',
          type: 'text',
          label: 'Raum/Saal',
          localized: true,
        },
        {
          name: 'directions',
          type: 'textarea',
          label: 'Anfahrt',
          localized: true,
        },
      ],
    },
    // Online-Details
    {
      name: 'online',
      type: 'group',
      label: 'Online-Details',
      admin: {
        condition: (data, siblingData) =>
          siblingData?.format === 'online' || siblingData?.format === 'hybrid',
      },
      fields: [
        {
          name: 'platform',
          type: 'select',
          label: 'Plattform',
          options: [
            { label: 'Zoom', value: 'zoom' },
            { label: 'Microsoft Teams', value: 'teams' },
            { label: 'Google Meet', value: 'google-meet' },
            { label: 'Webex', value: 'webex' },
            { label: 'GoToWebinar', value: 'gotowebinar' },
            { label: 'Livestream', value: 'livestream' },
            { label: 'Sonstiges', value: 'other' },
          ],
        },
        {
          name: 'accessLink',
          type: 'text',
          label: 'Zugangslink',
          admin: {
            description: 'Wird nur angemeldeten Teilnehmern angezeigt',
          },
        },
        {
          name: 'accessInfo',
          type: 'textarea',
          label: 'Zugangsinformationen',
          localized: true,
          admin: {
            description: 'z.B. Meeting-ID, Passwort',
          },
        },
      ],
    },
    // Medien
    {
      name: 'image',
      type: 'upload',
      relationTo: 'media',
      label: 'Event-Bild',
    },
    {
      name: 'gallery',
      type: 'array',
      label: 'Galerie',
      fields: [
        {
          name: 'image',
          type: 'upload',
          relationTo: 'media',
          required: true,
        },
        {
          name: 'caption',
          type: 'text',
          localized: true,
        },
      ],
    },
    // Anmeldung & Tickets
    {
      name: 'registration',
      type: 'group',
      label: 'Anmeldung',
      fields: [
        {
          name: 'required',
          type: 'checkbox',
          defaultValue: false,
          label: 'Anmeldung erforderlich',
        },
        {
          name: 'method',
          type: 'select',
          label: 'Anmeldemethode',
          defaultValue: 'form',
          options: [
            { label: 'Internes Formular', value: 'form' },
            { label: 'E-Mail', value: 'email' },
            { label: 'Externer Link', value: 'external' },
            { label: 'Telefon', value: 'phone' },
          ],
          admin: {
            condition: (data, siblingData) => siblingData?.required,
          },
        },
        {
          name: 'formId',
          type: 'relationship',
          relationTo: 'forms',
          label: 'Anmeldeformular',
          admin: {
            condition: (data, siblingData) => siblingData?.required && siblingData?.method === 'form',
          },
        },
        {
          name: 'email',
          type: 'email',
          label: 'Anmelde-E-Mail',
          admin: {
            condition: (data, siblingData) => siblingData?.required && siblingData?.method === 'email',
          },
        },
        {
          name: 'externalUrl',
          type: 'text',
          label: 'Externer Anmeldelink',
          admin: {
            condition: (data, siblingData) => siblingData?.required && siblingData?.method === 'external',
          },
        },
        {
          name: 'phone',
          type: 'text',
          label: 'Telefonnummer',
          admin: {
            condition: (data, siblingData) => siblingData?.required && siblingData?.method === 'phone',
          },
        },
        {
          name: 'deadline',
          type: 'date',
          label: 'Anmeldeschluss',
          admin: {
            condition: (data, siblingData) => siblingData?.required,
            date: {
              pickerAppearance: 'dayAndTime',
            },
          },
        },
        {
          name: 'maxParticipants',
          type: 'number',
          min: 0,
          label: 'Maximale Teilnehmer',
          admin: {
            condition: (data, siblingData) => siblingData?.required,
            description: '0 = unbegrenzt',
          },
        },
        {
          name: 'currentParticipants',
          type: 'number',
          defaultValue: 0,
          label: 'Aktuelle Anmeldungen',
          admin: {
            condition: (data, siblingData) => siblingData?.required,
            readOnly: true,
          },
        },
        {
          name: 'waitlistEnabled',
          type: 'checkbox',
          defaultValue: false,
          label: 'Warteliste aktivieren',
          admin: {
            condition: (data, siblingData) => siblingData?.required && siblingData?.maxParticipants > 0,
          },
        },
      ],
    },
    // Preise
    {
      name: 'pricing',
      type: 'group',
      label: 'Preise',
      fields: [
        {
          name: 'isFree',
          type: 'checkbox',
          defaultValue: true,
          label: 'Kostenlos',
        },
        {
          name: 'prices',
          type: 'array',
          label: 'Preisoptionen',
          admin: {
            condition: (data, siblingData) => !siblingData?.isFree,
          },
          fields: [
            {
              name: 'name',
              type: 'text',
              required: true,
              label: 'Bezeichnung',
              localized: true,
              admin: {
                description: 'z.B. "Standard", "Frühbucher", "Ermäßigt"',
              },
            },
            {
              name: 'price',
              type: 'number',
              required: true,
              min: 0,
              label: 'Preis',
            },
            {
              name: 'currency',
              type: 'text',
              defaultValue: 'EUR',
              label: 'Währung',
            },
            {
              name: 'description',
              type: 'text',
              label: 'Beschreibung',
              localized: true,
            },
            {
              name: 'validUntil',
              type: 'date',
              label: 'Gültig bis',
              admin: {
                description: 'Für Frühbucher-Preise',
              },
            },
          ],
        },
        {
          name: 'priceNote',
          type: 'text',
          label: 'Preis-Hinweis',
          localized: true,
          admin: {
            description: 'z.B. "zzgl. MwSt.", "inkl. Verpflegung"',
          },
        },
      ],
    },
    // Sprecher/Referenten
    {
      name: 'speakers',
      type: 'array',
      label: 'Referenten',
      fields: [
        {
          name: 'teamMember',
          type: 'relationship',
          relationTo: 'team' as 'users',
          label: 'Aus Team wählen',
        },
        {
          name: 'isExternal',
          type: 'checkbox',
          defaultValue: false,
          label: 'Externer Referent',
        },
        {
          name: 'name',
          type: 'text',
          label: 'Name',
          admin: {
            condition: (data, siblingData) => siblingData?.isExternal,
          },
        },
        {
          name: 'title',
          type: 'text',
          label: 'Titel/Position',
          localized: true,
          admin: {
            condition: (data, siblingData) => siblingData?.isExternal,
          },
        },
        {
          name: 'company',
          type: 'text',
          label: 'Unternehmen',
          admin: {
            condition: (data, siblingData) => siblingData?.isExternal,
          },
        },
        {
          name: 'photo',
          type: 'upload',
          relationTo: 'media',
          label: 'Foto',
          admin: {
            condition: (data, siblingData) => siblingData?.isExternal,
          },
        },
        {
          name: 'bio',
          type: 'textarea',
          label: 'Kurz-Bio',
          localized: true,
          admin: {
            condition: (data, siblingData) => siblingData?.isExternal,
          },
        },
        {
          name: 'role',
          type: 'text',
          label: 'Rolle beim Event',
          localized: true,
          admin: {
            description: 'z.B. "Hauptredner", "Workshop-Leiter"',
          },
        },
      ],
    },
    // Agenda/Programm
    {
      name: 'agenda',
      type: 'array',
      label: 'Programm',
      fields: [
        {
          name: 'time',
          type: 'text',
          label: 'Uhrzeit',
          admin: {
            description: 'z.B. "09:00 - 10:30"',
          },
        },
        {
          name: 'title',
          type: 'text',
          required: true,
          label: 'Titel',
          localized: true,
        },
        {
          name: 'description',
          type: 'textarea',
          label: 'Beschreibung',
          localized: true,
        },
        {
          name: 'speaker',
          type: 'text',
          label: 'Referent',
        },
        {
          name: 'type',
          type: 'select',
          label: 'Typ',
          options: [
            { label: 'Vortrag', value: 'talk' },
            { label: 'Workshop', value: 'workshop' },
            { label: 'Pause', value: 'break' },
            { label: 'Networking', value: 'networking' },
            { label: 'Panel', value: 'panel' },
            { label: 'Q&A', value: 'qa' },
          ],
        },
      ],
    },
    // Kontakt
    {
      name: 'contact',
      type: 'group',
      label: 'Ansprechpartner',
      fields: [
        {
          name: 'teamMember',
          type: 'relationship',
          relationTo: 'team' as 'users',
          label: 'Aus Team wählen',
        },
        {
          name: 'name',
          type: 'text',
          label: 'Name',
        },
        {
          name: 'email',
          type: 'email',
          label: 'E-Mail',
        },
        {
          name: 'phone',
          type: 'text',
          label: 'Telefon',
        },
      ],
    },
    // Verknüpfungen
    {
      name: 'relatedServices',
      type: 'relationship',
      relationTo: 'services' as 'users',
      hasMany: true,
      label: 'Verwandte Leistungen',
    },
    {
      name: 'relatedEvents',
      type: 'relationship',
      relationTo: 'events' as 'users',
      hasMany: true,
      label: 'Verwandte Events',
    },
    // 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,
        },
        {
          name: 'ogImage',
          type: 'upload',
          relationTo: 'media',
          label: 'Social Media Bild',
        },
      ],
    },
    // Status
    {
      name: 'status',
      type: 'select',
      required: true,
      defaultValue: 'draft',
      label: 'Status',
      options: [
        { label: 'Entwurf', value: 'draft' },
        { label: 'Veröffentlicht', value: 'published' },
        { label: 'Ausgebucht', value: 'soldout' },
        { label: 'Abgesagt', value: 'cancelled' },
        { label: 'Verschoben', value: 'postponed' },
        { label: 'Beendet', value: 'past' },
      ],
      admin: {
        position: 'sidebar',
      },
    },
    {
      name: 'isFeatured',
      type: 'checkbox',
      defaultValue: false,
      label: 'Hervorgehoben',
      admin: {
        position: 'sidebar',
      },
    },
    {
      name: 'publishedAt',
      type: 'date',
      label: 'Veröffentlichungsdatum',
      admin: {
        position: 'sidebar',
        date: {
          pickerAppearance: 'dayAndTime',
        },
      },
    },
  ],
}
