import type { Block } from 'payload'

/**
 * Pricing Block
 *
 * Preistabellen für Produkte, Dienstleistungen oder Abonnements.
 * Unterstützt verschiedene Layouts, Vergleichstabellen und
 * Highlight-Features für empfohlene Pläne.
 */
export const PricingBlock: Block = {
  slug: 'pricing',
  labels: {
    singular: 'Preistabelle',
    plural: 'Preistabellen',
  },
  fields: [
    {
      name: 'title',
      type: 'text',
      label: 'Überschrift',
      localized: true,
    },
    {
      name: 'subtitle',
      type: 'text',
      label: 'Untertitel',
      localized: true,
    },
    {
      name: 'description',
      type: 'textarea',
      label: 'Beschreibung',
      localized: true,
    },
    // Preismodell
    {
      name: 'pricingType',
      type: 'select',
      defaultValue: 'one-time',
      label: 'Preismodell',
      options: [
        { label: 'Einmalig', value: 'one-time' },
        { label: 'Monatlich', value: 'monthly' },
        { label: 'Jährlich', value: 'yearly' },
        { label: 'Monatlich/Jährlich Toggle', value: 'toggle' },
        { label: 'Individuell', value: 'custom' },
      ],
    },
    {
      name: 'currency',
      type: 'text',
      defaultValue: '€',
      label: 'Währung',
    },
    {
      name: 'showCurrencyBefore',
      type: 'checkbox',
      defaultValue: true,
      label: 'Währung vor dem Preis',
    },
    // Toggle-Optionen für Monatlich/Jährlich
    {
      name: 'toggleOptions',
      type: 'group',
      label: 'Toggle-Optionen',
      admin: {
        condition: (data, siblingData) => siblingData?.pricingType === 'toggle',
      },
      fields: [
        {
          name: 'monthlyLabel',
          type: 'text',
          defaultValue: 'Monatlich',
          label: 'Monatlich Label',
          localized: true,
        },
        {
          name: 'yearlyLabel',
          type: 'text',
          defaultValue: 'Jährlich',
          label: 'Jährlich Label',
          localized: true,
        },
        {
          name: 'yearlyDiscount',
          type: 'text',
          label: 'Rabatt-Badge',
          localized: true,
          admin: {
            description: 'z.B. "2 Monate gratis"',
          },
        },
        {
          name: 'defaultToYearly',
          type: 'checkbox',
          defaultValue: true,
          label: 'Standard: Jährlich',
        },
      ],
    },
    // Preispläne
    {
      name: 'plans',
      type: 'array',
      label: 'Preispläne',
      minRows: 1,
      maxRows: 6,
      fields: [
        {
          name: 'name',
          type: 'text',
          required: true,
          label: 'Plan-Name',
          localized: true,
          admin: {
            description: 'z.B. "Basic", "Pro", "Enterprise"',
          },
        },
        {
          name: 'subtitle',
          type: 'text',
          label: 'Untertitel',
          localized: true,
          admin: {
            description: 'z.B. "Für Einsteiger"',
          },
        },
        {
          name: 'description',
          type: 'textarea',
          label: 'Beschreibung',
          localized: true,
        },
        // Preis
        {
          type: 'row',
          fields: [
            {
              name: 'price',
              type: 'number',
              label: 'Preis',
              admin: {
                width: '25%',
              },
            },
            {
              name: 'priceMonthly',
              type: 'number',
              label: 'Preis Monatlich',
              admin: {
                width: '25%',
                condition: (data, siblingData, { blockData }) =>
                  blockData?.pricingType === 'toggle',
              },
            },
            {
              name: 'priceYearly',
              type: 'number',
              label: 'Preis Jährlich',
              admin: {
                width: '25%',
                condition: (data, siblingData, { blockData }) =>
                  blockData?.pricingType === 'toggle',
              },
            },
            {
              name: 'priceSuffix',
              type: 'text',
              label: 'Preis-Suffix',
              localized: true,
              admin: {
                width: '25%',
                description: 'z.B. "/Monat", "/Nutzer"',
              },
            },
          ],
        },
        {
          name: 'originalPrice',
          type: 'number',
          label: 'Originalpreis (durchgestrichen)',
          admin: {
            description: 'Für Rabatt-Darstellung',
          },
        },
        {
          name: 'priceNote',
          type: 'text',
          label: 'Preis-Hinweis',
          localized: true,
          admin: {
            description: 'z.B. "zzgl. MwSt.", "Bei jährlicher Zahlung"',
          },
        },
        {
          name: 'customPriceText',
          type: 'text',
          label: 'Individueller Preis-Text',
          localized: true,
          admin: {
            description: 'z.B. "Auf Anfrage", "Individuell"',
            condition: (data, siblingData, { blockData }) =>
              blockData?.pricingType === 'custom',
          },
        },
        // Features
        {
          name: 'features',
          type: 'array',
          label: 'Features',
          fields: [
            {
              name: 'text',
              type: 'text',
              required: true,
              label: 'Feature',
              localized: true,
            },
            {
              name: 'included',
              type: 'checkbox',
              defaultValue: true,
              label: 'Enthalten',
            },
            {
              name: 'highlight',
              type: 'checkbox',
              defaultValue: false,
              label: 'Hervorheben',
            },
            {
              name: 'tooltip',
              type: 'text',
              label: 'Tooltip',
              localized: true,
            },
          ],
        },
        // CTA
        {
          name: 'ctaText',
          type: 'text',
          defaultValue: 'Jetzt starten',
          label: 'Button-Text',
          localized: true,
        },
        {
          name: 'ctaLink',
          type: 'text',
          label: 'Button-Link',
        },
        {
          name: 'ctaStyle',
          type: 'select',
          defaultValue: 'primary',
          label: 'Button-Stil',
          options: [
            { label: 'Primär', value: 'primary' },
            { label: 'Sekundär', value: 'secondary' },
            { label: 'Outline', value: 'outline' },
          ],
        },
        // Hervorhebung
        {
          name: 'isPopular',
          type: 'checkbox',
          defaultValue: false,
          label: 'Als "Beliebt" markieren',
        },
        {
          name: 'popularLabel',
          type: 'text',
          defaultValue: 'Beliebt',
          label: 'Beliebt-Label',
          localized: true,
          admin: {
            condition: (data, siblingData) => siblingData?.isPopular,
          },
        },
        {
          name: 'isRecommended',
          type: 'checkbox',
          defaultValue: false,
          label: 'Als "Empfohlen" markieren',
        },
        {
          name: 'recommendedLabel',
          type: 'text',
          defaultValue: 'Empfohlen',
          label: 'Empfohlen-Label',
          localized: true,
          admin: {
            condition: (data, siblingData) => siblingData?.isRecommended,
          },
        },
        // Styling
        {
          name: 'accentColor',
          type: 'select',
          label: 'Akzentfarbe',
          options: [
            { label: 'Standard', value: 'default' },
            { label: 'Primär', value: 'primary' },
            { label: 'Sekundär', value: 'secondary' },
            { label: 'Grün', value: 'green' },
            { label: 'Blau', value: 'blue' },
            { label: 'Lila', value: 'purple' },
          ],
        },
        {
          name: 'icon',
          type: 'upload',
          relationTo: 'media',
          label: 'Icon/Logo',
        },
      ],
    },
    // Vergleichstabelle
    {
      name: 'showComparison',
      type: 'checkbox',
      defaultValue: false,
      label: 'Vergleichstabelle anzeigen',
    },
    {
      name: 'comparisonFeatures',
      type: 'array',
      label: 'Vergleichs-Features',
      admin: {
        condition: (data, siblingData) => siblingData?.showComparison,
      },
      fields: [
        {
          name: 'category',
          type: 'text',
          label: 'Kategorie',
          localized: true,
        },
        {
          name: 'feature',
          type: 'text',
          required: true,
          label: 'Feature',
          localized: true,
        },
        {
          name: 'tooltip',
          type: 'text',
          label: 'Tooltip',
          localized: true,
        },
        {
          name: 'values',
          type: 'array',
          label: 'Werte pro Plan',
          fields: [
            {
              name: 'planIndex',
              type: 'number',
              label: 'Plan-Index (0-basiert)',
            },
            {
              name: 'value',
              type: 'text',
              label: 'Wert',
              localized: true,
              admin: {
                description: '✓, ✗, oder Text wie "10 GB", "Unbegrenzt"',
              },
            },
          ],
        },
      ],
    },
    // Layout
    {
      name: 'layout',
      type: 'select',
      defaultValue: 'cards',
      label: 'Layout',
      options: [
        { label: 'Karten', value: 'cards' },
        { label: 'Tabelle', value: 'table' },
        { label: 'Kompakt', value: 'compact' },
      ],
    },
    {
      name: 'alignment',
      type: 'select',
      defaultValue: 'center',
      label: 'Ausrichtung',
      options: [
        { label: 'Links', value: 'left' },
        { label: 'Zentriert', value: 'center' },
        { label: 'Rechts', value: 'right' },
      ],
    },
    {
      name: 'highlightPopular',
      type: 'checkbox',
      defaultValue: true,
      label: 'Beliebten Plan hervorheben',
    },
    // Geld-zurück-Garantie etc.
    {
      name: 'guarantee',
      type: 'group',
      label: 'Garantie/Trust-Elemente',
      fields: [
        {
          name: 'show',
          type: 'checkbox',
          defaultValue: false,
          label: 'Garantie anzeigen',
        },
        {
          name: 'text',
          type: 'text',
          defaultValue: '30 Tage Geld-zurück-Garantie',
          label: 'Garantie-Text',
          localized: true,
          admin: {
            condition: (data, siblingData) => siblingData?.show,
          },
        },
        {
          name: 'icon',
          type: 'select',
          label: 'Icon',
          options: [
            { label: 'Schild', value: 'shield' },
            { label: 'Häkchen', value: 'check' },
            { label: 'Stern', value: 'star' },
            { label: 'Schloss', value: 'lock' },
          ],
          admin: {
            condition: (data, siblingData) => siblingData?.show,
          },
        },
      ],
    },
    // FAQ
    {
      name: 'showFAQ',
      type: 'checkbox',
      defaultValue: false,
      label: 'FAQ anzeigen',
    },
    {
      name: 'faqTitle',
      type: 'text',
      defaultValue: 'Häufig gestellte Fragen',
      label: 'FAQ-Überschrift',
      localized: true,
      admin: {
        condition: (data, siblingData) => siblingData?.showFAQ,
      },
    },
    {
      name: 'faqItems',
      type: 'array',
      label: 'FAQ-Einträge',
      admin: {
        condition: (data, siblingData) => siblingData?.showFAQ,
      },
      fields: [
        {
          name: 'question',
          type: 'text',
          required: true,
          label: 'Frage',
          localized: true,
        },
        {
          name: 'answer',
          type: 'richText',
          required: true,
          label: 'Antwort',
          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: 'Gradient', value: 'gradient' },
      ],
    },
    {
      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: 'Glass', value: 'glass' },
      ],
    },
  ],
}
