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

export const ProductCategories: CollectionConfig = {
  slug: 'product-categories',
  admin: {
    useAsTitle: 'name',
    group: 'Produkte',
    defaultColumns: ['name', 'slug', 'order', 'isActive'],
    description: 'Kategorien zur Gruppierung von Produkten',
  },
  access: {
    read: tenantScopedPublicRead,
    create: authenticatedOnly,
    update: authenticatedOnly,
    delete: authenticatedOnly,
  },
  fields: [
    {
      name: 'name',
      type: 'text',
      required: true,
      label: 'Name',
      localized: true,
    },
    {
      name: 'slug',
      type: 'text',
      required: true,
      unique: false, // Uniqueness per tenant handled by multi-tenant plugin
      label: 'URL-Slug',
      admin: {
        description: 'URL-freundlicher Name (z.B. "elektronik", "software")',
      },
    },
    {
      name: 'description',
      type: 'textarea',
      label: 'Beschreibung',
      localized: true,
      admin: {
        description: 'Kurze Beschreibung der Kategorie',
      },
    },
    {
      name: 'image',
      type: 'upload',
      relationTo: 'media',
      label: 'Kategorie-Bild',
    },
    {
      name: 'icon',
      type: 'text',
      label: 'Icon',
      admin: {
        description: 'Icon-Name (z.B. Lucide-Icons: "package", "cpu", "code")',
      },
    },
    {
      name: 'parent',
      type: 'relationship',
      relationTo: 'product-categories',
      label: 'Übergeordnete Kategorie',
      admin: {
        description: 'Optional: Für verschachtelte Kategorien',
      },
    },
    {
      type: 'row',
      fields: [
        {
          name: 'order',
          type: 'number',
          label: 'Sortierung',
          defaultValue: 0,
          admin: {
            width: '50%',
            description: 'Kleinere Zahlen erscheinen zuerst',
          },
        },
        {
          name: 'isActive',
          type: 'checkbox',
          label: 'Aktiv',
          defaultValue: true,
          admin: {
            width: '50%',
            description: 'Inaktive Kategorien werden nicht angezeigt',
          },
        },
      ],
    },
    // SEO
    {
      name: 'seo',
      type: 'group',
      label: 'SEO',
      admin: {
        description: 'Suchmaschinenoptimierung für Kategorieseiten',
      },
      fields: [
        {
          name: 'metaTitle',
          type: 'text',
          label: 'Meta-Titel',
          localized: true,
        },
        {
          name: 'metaDescription',
          type: 'textarea',
          label: 'Meta-Beschreibung',
          localized: true,
        },
      ],
    },
  ],
}
