// src/collections/CookieConfigurations.ts

import type { CollectionConfig } from 'payload'
import { tenantScopedPublicRead, authenticatedOnly } from '../lib/tenantAccess'

/**
 * CookieConfigurations Collection
 *
 * Mandantenspezifische Cookie-Banner-Konfiguration.
 * Öffentlich lesbar, aber nur für den eigenen Tenant (Domain-basiert).
 */
export const CookieConfigurations: CollectionConfig = {
  slug: 'cookie-configurations',
  admin: {
    useAsTitle: 'title',
    group: 'Consent Management',
    description: 'Cookie-Banner Konfiguration pro Tenant',
  },
  access: {
    // Öffentlich, aber tenant-isoliert (Domain-Check)
    read: tenantScopedPublicRead,
    create: authenticatedOnly,
    update: authenticatedOnly,
    delete: authenticatedOnly,
  },
  fields: [
    {
      name: 'tenant',
      type: 'relationship',
      relationTo: 'tenants',
      required: true,
      unique: true,
      admin: {
        description: 'Jeder Tenant kann nur eine Konfiguration haben',
      },
    },
    {
      name: 'title',
      type: 'text',
      required: true,
      defaultValue: 'Cookie-Einstellungen',
      admin: {
        description: 'Interner Titel zur Identifikation',
      },
    },
    {
      name: 'revision',
      type: 'number',
      required: true,
      defaultValue: 1,
      admin: {
        description:
          'Bei inhaltlichen Änderungen erhöhen → erzwingt erneuten Consent bei allen Nutzern',
      },
    },
    {
      name: 'enabledCategories',
      type: 'select',
      hasMany: true,
      required: true,
      defaultValue: ['necessary', 'analytics'],
      options: [
        { label: 'Notwendig', value: 'necessary' },
        { label: 'Funktional', value: 'functional' },
        { label: 'Statistik', value: 'analytics' },
        { label: 'Marketing', value: 'marketing' },
      ],
      admin: {
        description: 'Welche Kategorien sollen im Banner angezeigt werden?',
      },
    },
    {
      name: 'translations',
      type: 'group',
      fields: [
        {
          name: 'de',
          type: 'group',
          label: 'Deutsch',
          fields: [
            {
              name: 'bannerTitle',
              type: 'text',
              defaultValue: 'Wir respektieren Ihre Privatsphäre',
            },
            {
              name: 'bannerDescription',
              type: 'textarea',
              defaultValue:
                'Diese Website verwendet Cookies, um Ihnen die bestmögliche Erfahrung zu bieten.',
            },
            {
              name: 'acceptAllButton',
              type: 'text',
              defaultValue: 'Alle akzeptieren',
            },
            {
              name: 'acceptNecessaryButton',
              type: 'text',
              defaultValue: 'Nur notwendige',
            },
            {
              name: 'settingsButton',
              type: 'text',
              defaultValue: 'Einstellungen',
            },
            {
              name: 'saveButton',
              type: 'text',
              defaultValue: 'Auswahl speichern',
            },
            {
              name: 'privacyPolicyUrl',
              type: 'text',
              defaultValue: '/datenschutz',
            },
            {
              name: 'categoryLabels',
              type: 'group',
              fields: [
                {
                  name: 'necessary',
                  type: 'group',
                  fields: [
                    { name: 'title', type: 'text', defaultValue: 'Notwendig' },
                    {
                      name: 'description',
                      type: 'textarea',
                      defaultValue:
                        'Diese Cookies sind für die Grundfunktionen der Website erforderlich.',
                    },
                  ],
                },
                {
                  name: 'functional',
                  type: 'group',
                  fields: [
                    { name: 'title', type: 'text', defaultValue: 'Funktional' },
                    {
                      name: 'description',
                      type: 'textarea',
                      defaultValue: 'Diese Cookies ermöglichen erweiterte Funktionen.',
                    },
                  ],
                },
                {
                  name: 'analytics',
                  type: 'group',
                  fields: [
                    { name: 'title', type: 'text', defaultValue: 'Statistik' },
                    {
                      name: 'description',
                      type: 'textarea',
                      defaultValue:
                        'Diese Cookies helfen uns zu verstehen, wie Besucher die Website nutzen.',
                    },
                  ],
                },
                {
                  name: 'marketing',
                  type: 'group',
                  fields: [
                    { name: 'title', type: 'text', defaultValue: 'Marketing' },
                    {
                      name: 'description',
                      type: 'textarea',
                      defaultValue: 'Diese Cookies werden für Werbezwecke verwendet.',
                    },
                  ],
                },
              ],
            },
          ],
        },
      ],
    },
    {
      name: 'styling',
      type: 'group',
      fields: [
        {
          name: 'position',
          type: 'select',
          defaultValue: 'bottom',
          options: [
            { label: 'Unten', value: 'bottom' },
            { label: 'Oben', value: 'top' },
            { label: 'Mitte (Modal)', value: 'middle' },
          ],
        },
        {
          name: 'theme',
          type: 'select',
          defaultValue: 'dark',
          options: [
            { label: 'Dunkel', value: 'dark' },
            { label: 'Hell', value: 'light' },
            { label: 'Auto (System)', value: 'auto' },
          ],
        },
      ],
    },
  ],
}
