// src/collections/CommunityTemplates.ts

import type { CollectionConfig } from 'payload'
import {
  isCommunityManager,
  isCommunityModeratorOrAbove,
  hasCommunityAccess,
} from '../lib/communityAccess'

/**
 * CommunityTemplates Collection
 *
 * Antwort-Vorlagen für Community-Interaktionen mit Variablen.
 * Teil des Community Management Systems.
 */
export const CommunityTemplates: CollectionConfig = {
  slug: 'community-templates',
  labels: {
    singular: 'Response Template',
    plural: 'Response Templates',
  },
  admin: {
    group: 'Community',
    useAsTitle: 'name',
    defaultColumns: ['name', 'category', 'channel', 'usageCount'],
  },
  access: {
    read: hasCommunityAccess,
    create: isCommunityModeratorOrAbove,
    update: isCommunityModeratorOrAbove,
    delete: isCommunityManager,
  },
  fields: [
    {
      type: 'row',
      fields: [
        {
          name: 'name',
          type: 'text',
          required: true,
          localized: true,
          label: 'Name',
          admin: { width: '50%' },
        },
        {
          name: 'category',
          type: 'select',
          required: true,
          label: 'Kategorie',
          options: [
            { label: 'Danke', value: 'thank_you' },
            { label: 'Frage beantworten', value: 'question_answer' },
            { label: 'Hotline-Verweis', value: 'redirect_hotline' },
            { label: 'Medizinischer Disclaimer', value: 'medical_disclaimer' },
            { label: 'Produkt-Info', value: 'product_info' },
            { label: 'Content-Verweis', value: 'content_reference' },
            { label: 'Follow-up', value: 'follow_up' },
            { label: 'Negatives Feedback', value: 'negative_feedback' },
            { label: 'Spam-Antwort', value: 'spam_response' },
            { label: 'Begrüßung', value: 'welcome' },
          ],
          admin: { width: '50%' },
        },
      ],
    },
    {
      type: 'row',
      fields: [
        {
          name: 'channel',
          type: 'relationship',
          relationTo: 'youtube-channels',
          label: 'Kanal (optional)',
          admin: {
            width: '50%',
            description: 'Leer = für alle Kanäle',
          },
        },
        {
          name: 'platforms',
          type: 'relationship',
          relationTo: 'social-platforms',
          hasMany: true,
          label: 'Plattformen',
          admin: {
            width: '50%',
            description: 'Leer = für alle Plattformen',
          },
        },
      ],
    },

    // Template Text mit Variablen
    {
      name: 'template',
      type: 'textarea',
      required: true,
      localized: true,
      label: 'Template Text',
      admin: {
        rows: 6,
        description:
          'Variablen: {{author_name}}, {{video_title}}, {{channel_name}}, {{hotline_number}}',
      },
    },

    // Verfügbare Variablen
    {
      name: 'variables',
      type: 'array',
      label: 'Verfügbare Variablen',
      admin: {
        description: 'Dokumentation der Variablen in diesem Template',
      },
      fields: [
        {
          type: 'row',
          fields: [
            {
              name: 'variable',
              type: 'text',
              required: true,
              label: 'Variable',
              admin: {
                width: '30%',
                placeholder: '{{author_name}}',
              },
            },
            {
              name: 'description',
              type: 'text',
              label: 'Beschreibung',
              admin: {
                width: '50%',
                placeholder: 'Name des Kommentar-Autors',
              },
            },
            {
              name: 'defaultValue',
              type: 'text',
              label: 'Fallback',
              admin: {
                width: '20%',
              },
            },
          ],
        },
      ],
    },

    // Auto-Suggest Keywords
    {
      name: 'autoSuggestKeywords',
      type: 'array',
      label: 'Auto-Suggest Keywords',
      admin: {
        description: 'Bei diesen Keywords wird das Template vorgeschlagen',
      },
      fields: [
        {
          name: 'keyword',
          type: 'text',
          required: true,
          label: 'Keyword',
        },
      ],
    },

    // Flags
    {
      type: 'row',
      fields: [
        {
          name: 'requiresReview',
          type: 'checkbox',
          label: 'Review erforderlich',
          admin: {
            width: '33%',
            description: 'Für medizinische Antworten',
          },
        },
        {
          name: 'isActive',
          type: 'checkbox',
          label: 'Aktiv',
          defaultValue: true,
          admin: { width: '33%' },
        },
        {
          name: 'usageCount',
          type: 'number',
          label: 'Verwendungen',
          defaultValue: 0,
          admin: {
            width: '33%',
            readOnly: true,
          },
        },
      ],
    },

    // Beispiel-Output
    {
      name: 'exampleOutput',
      type: 'textarea',
      label: 'Beispiel-Output',
      admin: {
        rows: 3,
        description: 'So sieht die Antwort mit ausgefüllten Variablen aus',
      },
    },
  ],
  timestamps: true,
}
