import type { Block } from 'payload'

/**
 * Events Block
 *
 * Zeigt Events aus der Events Collection oder inline-definierte Events.
 * Unterstützt verschiedene Layouts (Kalender, Liste, Karten, Timeline)
 * und Filter nach Event-Typ, Zeitraum oder Kategorie.
 */
export const EventsBlock: Block = {
  slug: 'events',
  labels: {
    singular: 'Events',
    plural: 'Events',
  },
  fields: [
    {
      name: 'title',
      type: 'text',
      label: 'Überschrift',
      localized: true,
    },
    {
      name: 'subtitle',
      type: 'text',
      label: 'Untertitel',
      localized: true,
    },
    // Quelle
    {
      name: 'sourceMode',
      type: 'select',
      defaultValue: 'collection',
      label: 'Quelle',
      options: [
        { label: 'Aus Events Collection', value: 'collection' },
        { label: 'Handverlesene Auswahl', value: 'selected' },
      ],
    },
    // Collection Filter
    {
      name: 'filterMode',
      type: 'select',
      defaultValue: 'upcoming',
      label: 'Filter',
      options: [
        { label: 'Kommende Events', value: 'upcoming' },
        { label: 'Vergangene Events', value: 'past' },
        { label: 'Alle Events', value: 'all' },
        { label: 'Nur hervorgehobene', value: 'featured' },
        { label: 'Nach Typ', value: 'type' },
        { label: 'Nach Kategorie', value: 'category' },
      ],
      admin: {
        condition: (data, siblingData) => siblingData?.sourceMode === 'collection',
      },
    },
    {
      name: 'eventType',
      type: 'select',
      label: 'Event-Typ filtern',
      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' },
      ],
      admin: {
        condition: (data, siblingData) =>
          siblingData?.sourceMode === 'collection' && siblingData?.filterMode === 'type',
      },
    },
    {
      name: 'category',
      type: 'text',
      label: 'Kategorie filtern',
      admin: {
        condition: (data, siblingData) =>
          siblingData?.sourceMode === 'collection' && siblingData?.filterMode === 'category',
      },
    },
    {
      name: 'selectedEvents',
      type: 'relationship',
      relationTo: 'events' as 'users',
      hasMany: true,
      label: 'Events auswählen',
      admin: {
        condition: (data, siblingData) => siblingData?.sourceMode === 'selected',
      },
    },
    {
      name: 'limit',
      type: 'number',
      defaultValue: 6,
      min: 1,
      max: 50,
      label: 'Maximale Anzahl',
      admin: {
        condition: (data, siblingData) => siblingData?.sourceMode === 'collection',
      },
    },
    // Layout
    {
      name: 'layout',
      type: 'select',
      defaultValue: 'cards',
      label: 'Layout',
      options: [
        { label: 'Karten (Grid)', value: 'cards' },
        { label: 'Liste', value: 'list' },
        { label: 'Kompakte Liste', value: 'compact' },
        { label: 'Timeline', value: 'timeline' },
        { label: 'Kalender', value: 'calendar' },
        { label: 'Agenda', value: 'agenda' },
      ],
    },
    {
      name: 'columns',
      type: 'select',
      defaultValue: '3',
      label: 'Spalten',
      options: [
        { label: '2 Spalten', value: '2' },
        { label: '3 Spalten', value: '3' },
        { label: '4 Spalten', value: '4' },
      ],
      admin: {
        condition: (data, siblingData) => siblingData?.layout === 'cards',
      },
    },
    // Anzeigeoptionen
    {
      name: 'showImage',
      type: 'checkbox',
      defaultValue: true,
      label: 'Bild anzeigen',
    },
    {
      name: 'showExcerpt',
      type: 'checkbox',
      defaultValue: true,
      label: 'Beschreibung anzeigen',
    },
    {
      name: 'showDate',
      type: 'checkbox',
      defaultValue: true,
      label: 'Datum anzeigen',
    },
    {
      name: 'showTime',
      type: 'checkbox',
      defaultValue: true,
      label: 'Uhrzeit anzeigen',
    },
    {
      name: 'showLocation',
      type: 'checkbox',
      defaultValue: true,
      label: 'Ort anzeigen',
    },
    {
      name: 'showPrice',
      type: 'checkbox',
      defaultValue: false,
      label: 'Preis anzeigen',
    },
    {
      name: 'showEventType',
      type: 'checkbox',
      defaultValue: false,
      label: 'Event-Typ Badge anzeigen',
    },
    {
      name: 'showRegistrationButton',
      type: 'checkbox',
      defaultValue: true,
      label: 'Anmelde-Button anzeigen',
    },
    {
      name: 'registrationButtonText',
      type: 'text',
      defaultValue: 'Jetzt anmelden',
      label: 'Button-Text',
      localized: true,
      admin: {
        condition: (data, siblingData) => siblingData?.showRegistrationButton,
      },
    },
    // Kalender-Optionen
    {
      name: 'calendarOptions',
      type: 'group',
      label: 'Kalender-Optionen',
      admin: {
        condition: (data, siblingData) => siblingData?.layout === 'calendar',
      },
      fields: [
        {
          name: 'defaultView',
          type: 'select',
          defaultValue: 'month',
          label: 'Standard-Ansicht',
          options: [
            { label: 'Monat', value: 'month' },
            { label: 'Woche', value: 'week' },
            { label: 'Tag', value: 'day' },
          ],
        },
        {
          name: 'showNavigation',
          type: 'checkbox',
          defaultValue: true,
          label: 'Navigation anzeigen',
        },
        {
          name: 'showViewToggle',
          type: 'checkbox',
          defaultValue: false,
          label: 'Ansichts-Wechsel anzeigen',
        },
      ],
    },
    // Gruppierung
    {
      name: 'groupBy',
      type: 'select',
      defaultValue: 'none',
      label: 'Gruppieren nach',
      options: [
        { label: 'Keine Gruppierung', value: 'none' },
        { label: 'Monat', value: 'month' },
        { label: 'Event-Typ', value: 'type' },
        { label: 'Kategorie', value: 'category' },
      ],
      admin: {
        condition: (data, siblingData) =>
          siblingData?.layout === 'list' || siblingData?.layout === 'compact',
      },
    },
    // Sortierung
    {
      name: 'sortOrder',
      type: 'select',
      defaultValue: 'asc',
      label: 'Sortierung',
      options: [
        { label: 'Aufsteigend (älteste zuerst)', value: 'asc' },
        { label: 'Absteigend (neueste zuerst)', value: 'desc' },
      ],
    },
    // CTA
    {
      name: 'showAllLink',
      type: 'checkbox',
      defaultValue: true,
      label: '"Alle Events" Link anzeigen',
    },
    {
      name: 'allEventsLink',
      type: 'text',
      defaultValue: '/events',
      label: 'Link zu allen Events',
      admin: {
        condition: (data, siblingData) => siblingData?.showAllLink,
      },
    },
    {
      name: 'allEventsText',
      type: 'text',
      defaultValue: 'Alle Veranstaltungen',
      label: 'Link-Text',
      localized: true,
      admin: {
        condition: (data, siblingData) => siblingData?.showAllLink,
      },
    },
    // Leerer Zustand
    {
      name: 'emptyMessage',
      type: 'text',
      defaultValue: 'Aktuell sind keine Veranstaltungen geplant.',
      label: 'Nachricht wenn keine Events',
      localized: true,
    },
    // Styling
    {
      name: 'backgroundColor',
      type: 'select',
      defaultValue: 'white',
      label: 'Hintergrund',
      options: [
        { label: 'Weiß', value: 'white' },
        { label: 'Hell (Grau)', value: 'light' },
        { label: 'Dunkel', value: 'dark' },
        { label: 'Akzentfarbe', value: 'accent' },
      ],
    },
    {
      name: 'cardStyle',
      type: 'select',
      defaultValue: 'elevated',
      label: 'Karten-Stil',
      options: [
        { label: 'Erhöht (Schatten)', value: 'elevated' },
        { label: 'Umrandet', value: 'bordered' },
        { label: 'Flach', value: 'flat' },
      ],
      admin: {
        condition: (data, siblingData) => siblingData?.layout === 'cards',
      },
    },
  ],
}
