import type { Block } from 'payload'

/**
 * FAQ Block
 *
 * Zeigt FAQs entweder aus der FAQs Collection oder als inline-definierte Fragen.
 * Unterstützt verschiedene Layouts (Accordion, Grid, Liste) und
 * generiert automatisch Schema.org FAQPage Structured Data.
 */
export const FAQBlock: Block = {
  slug: 'faq-block',
  labels: {
    singular: 'FAQ',
    plural: 'FAQs',
  },
  fields: [
    {
      name: 'title',
      type: 'text',
      defaultValue: 'Häufig gestellte Fragen',
      label: 'Überschrift',
      localized: true,
    },
    {
      name: 'subtitle',
      type: 'text',
      label: 'Untertitel',
      localized: true,
    },
    {
      name: 'sourceMode',
      type: 'select',
      defaultValue: 'collection',
      label: 'Quelle',
      options: [
        { label: 'Aus FAQ Collection', value: 'collection' },
        { label: 'Inline (nur auf dieser Seite)', value: 'inline' },
      ],
    },
    // Collection Mode Fields
    {
      name: 'displayMode',
      type: 'select',
      defaultValue: 'all',
      label: 'Auswahl',
      options: [
        { label: 'Alle aktiven FAQs', value: 'all' },
        { label: 'Nur hervorgehobene', value: 'featured' },
        { label: 'Nach Kategorie', value: 'category' },
        { label: 'Handverlesene Auswahl', value: 'selected' },
      ],
      admin: {
        condition: (data, siblingData) => siblingData?.sourceMode === 'collection',
      },
    },
    {
      name: 'category',
      type: 'text',
      label: 'Kategorie filtern',
      admin: {
        condition: (data, siblingData) =>
          siblingData?.sourceMode === 'collection' && siblingData?.displayMode === 'category',
        description: 'Zeigt nur FAQs mit dieser Kategorie',
      },
    },
    {
      name: 'selectedFAQs',
      type: 'relationship',
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
      relationTo: 'faqs' as any,
      hasMany: true,
      label: 'FAQs auswählen',
      admin: {
        condition: (data, siblingData) =>
          siblingData?.sourceMode === 'collection' && siblingData?.displayMode === 'selected',
      },
    },
    {
      name: 'limit',
      type: 'number',
      defaultValue: 10,
      min: 1,
      max: 50,
      label: 'Maximale Anzahl',
      admin: {
        condition: (data, siblingData) =>
          siblingData?.sourceMode === 'collection' &&
          (siblingData?.displayMode === 'all' || siblingData?.displayMode === 'category'),
      },
    },
    // Inline Mode Fields
    {
      name: 'inlineFAQs',
      type: 'array',
      label: 'FAQs',
      labels: {
        singular: 'FAQ',
        plural: 'FAQs',
      },
      admin: {
        condition: (data, siblingData) => siblingData?.sourceMode === 'inline',
      },
      fields: [
        {
          name: 'question',
          type: 'text',
          required: true,
          label: 'Frage',
          localized: true,
        },
        {
          name: 'answer',
          type: 'richText',
          required: true,
          label: 'Antwort',
          localized: true,
        },
        {
          name: 'answerPlainText',
          type: 'textarea',
          label: 'Antwort (Nur-Text für Schema.org)',
          localized: true,
          admin: {
            description: 'Kurzfassung als reiner Text für SEO Structured Data',
          },
        },
      ],
    },
    // Layout Options
    {
      name: 'layout',
      type: 'select',
      defaultValue: 'accordion',
      label: 'Layout',
      options: [
        { label: 'Accordion (aufklappbar)', value: 'accordion' },
        { label: 'Grid (Karten)', value: 'grid' },
        { label: 'Liste', value: 'list' },
        { label: 'Zweispaltig', value: 'two-column' },
      ],
    },
    {
      name: 'columns',
      type: 'select',
      defaultValue: '2',
      label: 'Spalten',
      options: [
        { label: '2 Spalten', value: '2' },
        { label: '3 Spalten', value: '3' },
      ],
      admin: {
        condition: (data, siblingData) => siblingData?.layout === 'grid',
      },
    },
    {
      name: 'expandFirst',
      type: 'checkbox',
      defaultValue: true,
      label: 'Erste Frage 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',
      },
    },
    {
      name: 'showCategory',
      type: 'checkbox',
      defaultValue: false,
      label: 'Kategorie anzeigen',
    },
    {
      name: 'showIcon',
      type: 'checkbox',
      defaultValue: false,
      label: 'Icons anzeigen',
    },
    {
      name: 'groupByCategory',
      type: 'checkbox',
      defaultValue: false,
      label: 'Nach Kategorie gruppieren',
      admin: {
        condition: (data, siblingData) =>
          siblingData?.sourceMode === 'collection' && siblingData?.displayMode !== 'category',
      },
    },
    // Schema.org Options
    {
      name: 'enableSchemaOrg',
      type: 'checkbox',
      defaultValue: true,
      label: 'Schema.org FAQPage generieren',
      admin: {
        description: 'Generiert SEO-optimiertes JSON-LD Structured Data',
      },
    },
    // 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' },
      ],
    },
    // CTA
    {
      name: 'showContactCTA',
      type: 'checkbox',
      defaultValue: false,
      label: 'Kontakt-CTA anzeigen',
      admin: {
        description: 'Zeigt einen "Frage nicht gefunden? Kontaktieren Sie uns" Button',
      },
    },
    {
      name: 'contactCTAText',
      type: 'text',
      defaultValue: 'Ihre Frage war nicht dabei?',
      label: 'CTA Text',
      localized: true,
      admin: {
        condition: (data, siblingData) => siblingData?.showContactCTA,
      },
    },
    {
      name: 'contactCTALink',
      type: 'text',
      defaultValue: '/kontakt',
      label: 'CTA Link',
      admin: {
        condition: (data, siblingData) => siblingData?.showContactCTA,
      },
    },
    {
      name: 'contactCTAButtonText',
      type: 'text',
      defaultValue: 'Kontaktieren Sie uns',
      label: 'CTA Button Text',
      localized: true,
      admin: {
        condition: (data, siblingData) => siblingData?.showContactCTA,
      },
    },
  ],
}
