import type { Block } from 'payload'

/**
 * Services Block
 *
 * Zeigt Leistungen/Dienstleistungen aus der Services Collection.
 * Unterstützt verschiedene Layouts und Filteroptionen.
 */
export const ServicesBlock: Block = {
  slug: 'services-block',
  labels: {
    singular: 'Leistungen',
    plural: 'Leistungen',
  },
  fields: [
    {
      name: 'title',
      type: 'text',
      defaultValue: 'Unsere Leistungen',
      label: 'Überschrift',
      localized: true,
    },
    {
      name: 'subtitle',
      type: 'text',
      label: 'Untertitel',
      localized: true,
    },
    {
      name: 'introduction',
      type: 'richText',
      label: 'Einleitungstext',
      localized: true,
      admin: {
        description: 'Optionaler Text vor der Leistungsübersicht',
      },
    },
    // Auswahl-Modus
    {
      name: 'displayMode',
      type: 'select',
      defaultValue: 'all',
      label: 'Auswahl',
      options: [
        { label: 'Alle aktiven Leistungen', value: 'all' },
        { label: 'Nur hervorgehobene', value: 'featured' },
        { label: 'Nach Kategorie', value: 'category' },
        { label: 'Handverlesene Auswahl', value: 'selected' },
      ],
    },
    {
      name: 'category',
      type: 'relationship',
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
      relationTo: 'service-categories' as any,
      label: 'Kategorie filtern',
      admin: {
        condition: (data, siblingData) => siblingData?.displayMode === 'category',
        description: 'Zeigt nur Leistungen dieser Kategorie',
      },
    },
    {
      name: 'selectedServices',
      type: 'relationship',
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
      relationTo: 'services' as any,
      hasMany: true,
      label: 'Leistungen auswählen',
      admin: {
        condition: (data, siblingData) => siblingData?.displayMode === 'selected',
      },
    },
    {
      name: 'limit',
      type: 'number',
      defaultValue: 12,
      min: 1,
      max: 50,
      label: 'Maximale Anzahl',
      admin: {
        condition: (data, siblingData) =>
          siblingData?.displayMode === 'all' ||
          siblingData?.displayMode === 'featured' ||
          siblingData?.displayMode === 'category',
      },
    },
    // Layout
    {
      name: 'layout',
      type: 'select',
      defaultValue: 'grid',
      label: 'Layout',
      options: [
        { label: 'Grid (Karten)', value: 'grid' },
        { label: 'Liste mit Icons', value: 'list' },
        { label: 'Tabs (nach Kategorie)', value: 'tabs' },
        { label: 'Accordion', value: 'accordion' },
        { label: 'Featured + Grid', value: 'featured-grid' },
        { label: 'Slider/Karussell', value: 'slider' },
        { label: 'Kompakt (nur Titel + Icon)', value: 'compact' },
        { label: 'Masonry', value: 'masonry' },
      ],
    },
    {
      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) =>
          ['grid', 'compact', 'masonry', 'featured-grid'].includes(siblingData?.layout),
      },
    },
    {
      name: 'featuredCount',
      type: 'number',
      defaultValue: 1,
      min: 1,
      max: 3,
      label: 'Anzahl Featured',
      admin: {
        condition: (data, siblingData) => siblingData?.layout === 'featured-grid',
        description: 'Anzahl der hervorgehobenen Leistungen oben',
      },
    },
    // Tabs-Optionen
    {
      name: 'tabsStyle',
      type: 'select',
      defaultValue: 'horizontal',
      label: 'Tabs-Stil',
      options: [
        { label: 'Horizontal', value: 'horizontal' },
        { label: 'Vertikal (Sidebar)', value: 'vertical' },
        { label: 'Pills', value: 'pills' },
      ],
      admin: {
        condition: (data, siblingData) => siblingData?.layout === 'tabs',
      },
    },
    {
      name: 'showAllTab',
      type: 'checkbox',
      defaultValue: true,
      label: '"Alle" Tab anzeigen',
      admin: {
        condition: (data, siblingData) => siblingData?.layout === 'tabs',
      },
    },
    // Accordion-Optionen
    {
      name: 'expandFirst',
      type: 'checkbox',
      defaultValue: true,
      label: 'Erste Leistung automatisch öffnen',
      admin: {
        condition: (data, siblingData) => siblingData?.layout === 'accordion',
      },
    },
    {
      name: 'allowMultipleOpen',
      type: 'checkbox',
      defaultValue: false,
      label: 'Mehrere gleichzeitig öffnen erlauben',
      admin: {
        condition: (data, siblingData) => siblingData?.layout === 'accordion',
      },
    },
    // Slider-Optionen
    {
      name: 'autoplay',
      type: 'checkbox',
      defaultValue: true,
      label: 'Automatisch wechseln',
      admin: {
        condition: (data, siblingData) => siblingData?.layout === 'slider',
      },
    },
    {
      name: 'autoplaySpeed',
      type: 'number',
      defaultValue: 5000,
      min: 2000,
      max: 15000,
      label: 'Wechselintervall (ms)',
      admin: {
        condition: (data, siblingData) =>
          siblingData?.layout === 'slider' && siblingData?.autoplay,
      },
    },
    {
      name: 'slidesPerView',
      type: 'select',
      defaultValue: '3',
      label: 'Sichtbare Slides',
      options: [
        { label: '1', value: '1' },
        { label: '2', value: '2' },
        { label: '3', value: '3' },
        { label: '4', value: '4' },
      ],
      admin: {
        condition: (data, siblingData) => siblingData?.layout === 'slider',
      },
    },
    // Anzeigeoptionen
    {
      name: 'showImage',
      type: 'checkbox',
      defaultValue: true,
      label: 'Bild anzeigen',
      admin: {
        condition: (data, siblingData) =>
          !['compact', 'list'].includes(siblingData?.layout),
      },
    },
    {
      name: 'showIcon',
      type: 'checkbox',
      defaultValue: true,
      label: 'Icon anzeigen',
    },
    {
      name: 'showDescription',
      type: 'checkbox',
      defaultValue: true,
      label: 'Kurzbeschreibung anzeigen',
      admin: {
        condition: (data, siblingData) => siblingData?.layout !== 'compact',
      },
    },
    {
      name: 'showCategory',
      type: 'checkbox',
      defaultValue: false,
      label: 'Kategorie anzeigen',
    },
    {
      name: 'showPricing',
      type: 'checkbox',
      defaultValue: true,
      label: 'Preis anzeigen',
    },
    {
      name: 'showFeatures',
      type: 'checkbox',
      defaultValue: false,
      label: 'Leistungsmerkmale anzeigen',
      admin: {
        condition: (data, siblingData) =>
          ['list', 'accordion', 'featured-grid'].includes(siblingData?.layout),
      },
    },
    {
      name: 'featuresLimit',
      type: 'number',
      defaultValue: 3,
      min: 1,
      max: 10,
      label: 'Max. Anzahl Merkmale',
      admin: {
        condition: (data, siblingData) => siblingData?.showFeatures,
      },
    },
    {
      name: 'showCTA',
      type: 'checkbox',
      defaultValue: true,
      label: 'CTA-Button anzeigen',
    },
    {
      name: 'showNewBadge',
      type: 'checkbox',
      defaultValue: true,
      label: '"Neu"-Badge anzeigen',
    },
    // Gruppierung
    {
      name: 'groupByCategory',
      type: 'checkbox',
      defaultValue: false,
      label: 'Nach Kategorie gruppieren',
      admin: {
        condition: (data, siblingData) =>
          siblingData?.displayMode !== 'category' &&
          !['tabs', 'slider'].includes(siblingData?.layout),
        description: 'Zeigt Kategorie-Überschriften zwischen den Leistungen',
      },
    },
    // Detail-Link
    {
      name: 'linkToDetail',
      type: 'checkbox',
      defaultValue: true,
      label: 'Zur Detail-Seite verlinken',
      admin: {
        description: 'Karten/Einträge verlinken zur Leistungs-Detail-Seite',
      },
    },
    {
      name: 'detailLinkText',
      type: 'text',
      defaultValue: 'Mehr erfahren',
      label: 'Detail-Link Text',
      localized: true,
      admin: {
        condition: (data, siblingData) => siblingData?.linkToDetail,
      },
    },
    {
      name: 'servicesBasePath',
      type: 'text',
      defaultValue: '/leistungen',
      label: 'Basis-Pfad für Detail-Seiten',
      admin: {
        condition: (data, siblingData) => siblingData?.linkToDetail,
        description: 'z.B. "/leistungen" → "/leistungen/intensivpflege"',
      },
    },
    // Styling
    {
      name: 'cardStyle',
      type: 'select',
      defaultValue: 'elevated',
      label: 'Karten-Stil',
      options: [
        { label: 'Erhöht (Schatten)', value: 'elevated' },
        { label: 'Umrandet', value: 'bordered' },
        { label: 'Flach', value: 'flat' },
        { label: 'Gefüllt', value: 'filled' },
      ],
      admin: {
        condition: (data, siblingData) =>
          ['grid', 'masonry', 'featured-grid', 'slider'].includes(siblingData?.layout),
      },
    },
    {
      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' },
      ],
    },
    // Globaler CTA
    {
      type: 'collapsible',
      label: 'Abschnitt-CTA',
      admin: {
        initCollapsed: true,
        description: 'Optionaler Button am Ende des gesamten Blocks',
      },
      fields: [
        {
          name: 'showSectionCTA',
          type: 'checkbox',
          defaultValue: false,
          label: 'CTA-Button anzeigen',
        },
        {
          name: 'sectionCTAText',
          type: 'text',
          defaultValue: 'Alle Leistungen ansehen',
          label: 'Button-Text',
          localized: true,
          admin: {
            condition: (data, siblingData) => siblingData?.showSectionCTA,
          },
        },
        {
          name: 'sectionCTALink',
          type: 'text',
          defaultValue: '/leistungen',
          label: 'Button-Link',
          admin: {
            condition: (data, siblingData) => siblingData?.showSectionCTA,
          },
        },
      ],
    },
  ],
}
