import type { Block } from 'payload'

/**
 * DownloadsBlock
 *
 * Zeigt Downloads mit Filter- und Suchfunktion.
 * Unterstützt verschiedene Layouts und Kategoriefilter.
 */
export const DownloadsBlock: Block = {
  slug: 'downloads-block',
  labels: {
    singular: 'Downloads',
    plural: 'Downloads',
  },
  imageURL: '/assets/blocks/downloads.png',
  fields: [
    {
      name: 'title',
      type: 'text',
      label: 'Überschrift',
      localized: true,
      defaultValue: 'Downloads',
    },
    {
      name: 'subtitle',
      type: 'textarea',
      label: 'Untertitel',
      localized: true,
    },
    {
      name: 'introduction',
      type: 'richText',
      label: 'Einleitungstext',
      localized: true,
    },
    // Datenquelle
    {
      name: 'source',
      type: 'select',
      defaultValue: 'all',
      label: 'Downloads',
      options: [
        { label: 'Alle aktiven', value: 'all' },
        { label: 'Nach Kategorie', value: 'category' },
        { label: 'Nach Tags', value: 'tags' },
        { label: 'Nur hervorgehobene', value: 'featured' },
        { label: 'Zugehörig zu Service', value: 'service' },
        { label: 'Zugehörig zu Produkt', value: 'product' },
        { label: 'Manuell auswählen', value: 'manual' },
      ],
    },
    {
      name: 'category',
      type: 'select',
      label: 'Kategorie',
      options: [
        { label: 'Broschüre', value: 'brochure' },
        { label: 'Flyer', value: 'flyer' },
        { label: 'Katalog', value: 'catalog' },
        { label: 'Preisliste', value: 'pricelist' },
        { label: 'Formular', value: 'form' },
        { label: 'Anleitung', value: 'manual' },
        { label: 'Datenblatt', value: 'datasheet' },
        { label: 'Zertifikat', value: 'certificate' },
        { label: 'Pressematerial', value: 'press' },
        { label: 'Whitepaper', value: 'whitepaper' },
        { label: 'Präsentation', value: 'presentation' },
        { label: 'Vertrag/AGB', value: 'legal' },
      ],
      admin: {
        condition: (_, siblingData) => siblingData?.source === 'category',
      },
    },
    {
      name: 'filterTags',
      type: 'text',
      label: 'Tags (kommagetrennt)',
      admin: {
        condition: (_, siblingData) => siblingData?.source === 'tags',
        description: 'z.B. "produktinfo, 2024, neu"',
      },
    },
    {
      name: 'relatedService',
      type: 'relationship',
      relationTo: 'services',
      label: 'Zugehöriger Service',
      admin: {
        condition: (_, siblingData) => siblingData?.source === 'service',
      },
    },
    {
      name: 'relatedProduct',
      type: 'relationship',
      relationTo: 'products',
      label: 'Zugehöriges Produkt',
      admin: {
        condition: (_, siblingData) => siblingData?.source === 'product',
      },
    },
    {
      name: 'selectedDownloads',
      type: 'relationship',
      relationTo: 'downloads',
      hasMany: true,
      label: 'Downloads auswählen',
      admin: {
        condition: (_, siblingData) => siblingData?.source === 'manual',
      },
    },
    {
      name: 'limit',
      type: 'number',
      defaultValue: 20,
      min: 1,
      max: 100,
      label: 'Maximale Anzahl',
    },
    // Filter-UI
    {
      name: 'filters',
      type: 'group',
      label: 'Filter-Optionen',
      fields: [
        {
          name: 'showSearch',
          type: 'checkbox',
          defaultValue: true,
          label: 'Suchfeld',
        },
        {
          name: 'showCategoryFilter',
          type: 'checkbox',
          defaultValue: true,
          label: 'Kategorie-Filter',
        },
        {
          name: 'showFileTypeFilter',
          type: 'checkbox',
          defaultValue: false,
          label: 'Dateityp-Filter',
        },
        {
          name: 'filterLayout',
          type: 'select',
          defaultValue: 'horizontal',
          label: 'Filter-Layout',
          options: [
            { label: 'Horizontal', value: 'horizontal' },
            { label: 'Sidebar', value: 'sidebar' },
            { label: 'Dropdown', value: 'dropdown' },
          ],
        },
      ],
    },
    // Layout
    {
      name: 'layout',
      type: 'select',
      defaultValue: 'list',
      label: 'Layout',
      options: [
        { label: 'Liste', value: 'list' },
        { label: 'Karten', value: 'cards' },
        { label: 'Kompakt', value: 'compact' },
        { label: 'Tabelle', value: 'table' },
        { label: 'Akkordeon (nach Kategorie)', value: 'accordion' },
      ],
    },
    {
      name: 'columns',
      type: 'select',
      defaultValue: '1',
      label: 'Spalten',
      options: [
        { label: '1 Spalte', value: '1' },
        { label: '2 Spalten', value: '2' },
        { label: '3 Spalten', value: '3' },
        { label: '4 Spalten', value: '4' },
      ],
      admin: {
        condition: (_, siblingData) =>
          siblingData?.layout === 'cards' || siblingData?.layout === 'list',
      },
    },
    // Anzuzeigende Informationen
    {
      name: 'show',
      type: 'group',
      label: 'Anzeigen',
      fields: [
        {
          name: 'thumbnail',
          type: 'checkbox',
          defaultValue: true,
          label: 'Vorschaubild/Icon',
        },
        {
          name: 'description',
          type: 'checkbox',
          defaultValue: true,
          label: 'Beschreibung',
        },
        {
          name: 'fileSize',
          type: 'checkbox',
          defaultValue: true,
          label: 'Dateigröße',
        },
        {
          name: 'fileType',
          type: 'checkbox',
          defaultValue: true,
          label: 'Dateityp',
        },
        {
          name: 'category',
          type: 'checkbox',
          defaultValue: true,
          label: 'Kategorie',
        },
        {
          name: 'downloadCount',
          type: 'checkbox',
          defaultValue: false,
          label: 'Download-Zähler',
        },
        {
          name: 'version',
          type: 'checkbox',
          defaultValue: false,
          label: 'Version',
        },
        {
          name: 'lastUpdated',
          type: 'checkbox',
          defaultValue: false,
          label: 'Letztes Update',
        },
      ],
    },
    // Download-Verhalten
    {
      name: 'downloadBehavior',
      type: 'group',
      label: 'Download-Verhalten',
      fields: [
        {
          name: 'directDownload',
          type: 'checkbox',
          defaultValue: true,
          label: 'Direkter Download',
          admin: {
            description: 'Datei direkt herunterladen vs. Vorschau öffnen',
          },
        },
        {
          name: 'trackDownloads',
          type: 'checkbox',
          defaultValue: true,
          label: 'Downloads zählen',
        },
        {
          name: 'openInNewTab',
          type: 'checkbox',
          defaultValue: false,
          label: 'In neuem Tab öffnen',
        },
      ],
    },
    // File-Type Icons
    {
      name: 'fileIcons',
      type: 'group',
      label: 'Datei-Icons',
      fields: [
        {
          name: 'showFileTypeIcon',
          type: 'checkbox',
          defaultValue: true,
          label: 'Dateityp-Icon anzeigen',
        },
        {
          name: 'iconStyle',
          type: 'select',
          defaultValue: 'colored',
          label: 'Icon-Stil',
          options: [
            { label: 'Farbig', value: 'colored' },
            { label: 'Monochrom', value: 'mono' },
            { label: 'Outline', value: 'outline' },
          ],
          admin: {
            condition: (_, siblingData) => siblingData?.showFileTypeIcon,
          },
        },
      ],
    },
    // Sortierung
    {
      name: 'sortBy',
      type: 'select',
      defaultValue: 'order',
      label: 'Sortierung',
      options: [
        { label: 'Manuelle Reihenfolge', value: 'order' },
        { label: 'Titel (A-Z)', value: 'title_asc' },
        { label: 'Titel (Z-A)', value: 'title_desc' },
        { label: 'Neueste zuerst', value: 'date_desc' },
        { label: 'Älteste zuerst', value: 'date_asc' },
        { label: 'Beliebteste', value: 'downloads_desc' },
        { label: 'Dateigröße', value: 'size' },
      ],
    },
    // Gruppierung
    {
      name: 'groupBy',
      type: 'select',
      defaultValue: 'none',
      label: 'Gruppieren nach',
      options: [
        { label: 'Keine Gruppierung', value: 'none' },
        { label: 'Kategorie', value: 'category' },
        { label: 'Dateityp', value: 'fileType' },
      ],
    },
    // Pagination
    {
      name: 'pagination',
      type: 'group',
      label: 'Pagination',
      fields: [
        {
          name: 'type',
          type: 'select',
          defaultValue: 'none',
          label: 'Typ',
          options: [
            { label: 'Alle anzeigen', value: 'none' },
            { label: '"Mehr laden" Button', value: 'button' },
            { label: 'Pagination', value: 'pagination' },
          ],
        },
        {
          name: 'perPage',
          type: 'number',
          defaultValue: 10,
          label: 'Pro Seite',
          admin: {
            condition: (_, siblingData) =>
              siblingData?.type === 'pagination' || siblingData?.type === 'button',
          },
        },
      ],
    },
    // Styling
    {
      name: 'style',
      type: 'group',
      label: 'Darstellung',
      fields: [
        {
          name: 'bg',
          type: 'select',
          defaultValue: 'none',
          label: 'Hintergrund',
          options: [
            { label: 'Keiner', value: 'none' },
            { label: 'Hell', value: 'light' },
            { label: 'Dunkel', value: 'dark' },
          ],
        },
        {
          name: 'cardStyle',
          type: 'select',
          defaultValue: 'bordered',
          label: 'Karten-Stil',
          options: [
            { label: 'Einfach', value: 'simple' },
            { label: 'Mit Rahmen', value: 'bordered' },
            { label: 'Mit Schatten', value: 'shadow' },
          ],
        },
        {
          name: 'hoverEffect',
          type: 'select',
          defaultValue: 'highlight',
          label: 'Hover-Effekt',
          options: [
            { label: 'Keiner', value: 'none' },
            { label: 'Hervorheben', value: 'highlight' },
            { label: 'Anheben', value: 'lift' },
          ],
        },
        {
          name: 'gap',
          type: 'select',
          defaultValue: '16',
          label: 'Abstand',
          options: [
            { label: 'Klein (8px)', value: '8' },
            { label: 'Normal (16px)', value: '16' },
            { label: 'Groß (24px)', value: '24' },
          ],
        },
      ],
    },
    // Empty State
    {
      name: 'emptyState',
      type: 'group',
      label: 'Keine Downloads',
      fields: [
        {
          name: 'title',
          type: 'text',
          label: 'Überschrift',
          localized: true,
          defaultValue: 'Keine Downloads verfügbar',
        },
        {
          name: 'message',
          type: 'textarea',
          label: 'Nachricht',
          localized: true,
          defaultValue: 'Aktuell sind keine Downloads in dieser Kategorie verfügbar.',
        },
      ],
    },
    // CTA
    {
      name: 'showAllDownloadsLink',
      type: 'checkbox',
      defaultValue: false,
      label: '"Alle Downloads" Link',
    },
    {
      name: 'allDownloadsText',
      type: 'text',
      label: 'Link-Text',
      localized: true,
      defaultValue: 'Alle Downloads ansehen',
      admin: {
        condition: (_, siblingData) => siblingData?.showAllDownloadsLink,
      },
    },
    {
      name: 'allDownloadsUrl',
      type: 'text',
      label: 'Link-URL',
      defaultValue: '/downloads',
      admin: {
        condition: (_, siblingData) => siblingData?.showAllDownloadsLink,
      },
    },
  ],
}
