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

export const VideoCategories: CollectionConfig = {
  slug: 'video-categories',
  admin: {
    useAsTitle: 'name',
    group: 'Medien',
    description: 'Kategorien für Video-Bibliothek (z.B. Tutorials, Produktvideos, Testimonials)',
    defaultColumns: ['name', 'slug', 'order', 'isActive'],
  },
  access: {
    read: tenantScopedPublicRead,
    create: authenticatedOnly,
    update: authenticatedOnly,
    delete: authenticatedOnly,
  },
  fields: [
    {
      name: 'name',
      type: 'text',
      required: true,
      localized: true,
      label: 'Kategoriename',
      admin: {
        description: 'z.B. "Tutorials", "Produktvideos", "Webinare"',
      },
    },
    {
      name: 'slug',
      type: 'text',
      required: true,
      unique: false, // Uniqueness per tenant/locale
      label: 'URL-Slug',
      admin: {
        description: 'URL-freundlicher Name (z.B. "tutorials", "produktvideos")',
      },
    },
    {
      name: 'description',
      type: 'textarea',
      localized: true,
      label: 'Beschreibung',
      admin: {
        description: 'Kurzbeschreibung der Kategorie für SEO und Übersichten',
      },
    },
    {
      name: 'icon',
      type: 'text',
      label: 'Icon',
      admin: {
        description: 'Icon-Name (z.B. Lucide Icon wie "play-circle", "video", "film")',
      },
    },
    {
      name: 'coverImage',
      type: 'upload',
      relationTo: 'media',
      label: 'Cover-Bild',
      admin: {
        description: 'Repräsentatives Bild für die Kategorieübersicht',
      },
    },
    {
      name: 'order',
      type: 'number',
      defaultValue: 0,
      label: 'Reihenfolge',
      admin: {
        position: 'sidebar',
        description: 'Niedrigere Zahlen erscheinen zuerst',
      },
    },
    {
      name: 'isActive',
      type: 'checkbox',
      defaultValue: true,
      label: 'Aktiv',
      admin: {
        position: 'sidebar',
        description: 'Inaktive Kategorien werden nicht angezeigt',
      },
    },
  ],
  hooks: {
    beforeValidate: [
      createSlugValidationHook({ collection: 'video-categories' }),
    ],
  },
}
