// src/collections/CookieInventory.ts

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

/**
 * CookieInventory Collection
 *
 * Dokumentation aller verwendeten Cookies für die Datenschutzerklärung.
 * Öffentlich lesbar, aber nur für den eigenen Tenant (Domain-basiert).
 */
export const CookieInventory: CollectionConfig = {
  slug: 'cookie-inventory',
  admin: {
    useAsTitle: 'name',
    group: 'Consent Management',
    description: 'Cookie-Dokumentation für die Datenschutzerklärung',
    defaultColumns: ['name', 'provider', 'category', 'duration', '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,
    },
    {
      name: 'name',
      type: 'text',
      required: true,
      admin: {
        description: 'Technischer Name des Cookies (z.B. "_ga")',
      },
    },
    {
      name: 'provider',
      type: 'text',
      required: true,
      admin: {
        description: 'Anbieter (z.B. "Google LLC")',
      },
    },
    {
      name: 'category',
      type: 'select',
      required: true,
      options: [
        { label: 'Notwendig', value: 'necessary' },
        { label: 'Funktional', value: 'functional' },
        { label: 'Statistik', value: 'analytics' },
        { label: 'Marketing', value: 'marketing' },
      ],
    },
    {
      name: 'duration',
      type: 'text',
      required: true,
      admin: {
        description: 'Speicherdauer (z.B. "2 Jahre")',
      },
    },
    {
      name: 'description',
      type: 'textarea',
      required: true,
      admin: {
        description: 'Verständliche Erklärung für Endnutzer',
      },
    },
    {
      name: 'isActive',
      type: 'checkbox',
      defaultValue: true,
    },
  ],
}
