import type { CollectionConfig } from 'payload'
import { isYouTubeManager, hasYouTubeAccess } from '../lib/youtubeAccess'

export const YtChecklistTemplates: CollectionConfig = {
  slug: 'yt-checklist-templates',
  labels: {
    singular: { de: 'Checklisten-Vorlage', en: 'Checklist Template' },
    plural: { de: 'Checklisten-Vorlagen', en: 'Checklist Templates' },
  },
  admin: {
    group: 'YouTube',
    useAsTitle: 'name',
    defaultColumns: ['name', 'type', 'channel', 'updatedAt'],
    description: { de: 'Wiederverwendbare Checklisten für Upload und Produktion', en: 'Reusable checklists for upload and production' },
  },
  access: {
    read: hasYouTubeAccess,
    create: isYouTubeManager,
    update: isYouTubeManager,
    delete: isYouTubeManager,
  },
  fields: [
    {
      type: 'row',
      fields: [
        {
          name: 'name',
          type: 'text',
          required: true,
          localized: true,
          label: { de: 'Vorlagen-Name', en: 'Template Name' },
          admin: {
            width: '50%',
            placeholder: 'z.B. "Standard Upload Checklist"',
          },
        },
        {
          name: 'channel',
          type: 'relationship',
          relationTo: 'youtube-channels',
          label: { de: 'Kanal', en: 'Channel' },
          admin: {
            width: '50%',
            description: { de: 'Optional: Kanal-spezifische Vorlage', en: 'Optional: Channel-specific template' },
          },
        },
      ],
    },
    {
      type: 'row',
      fields: [
        {
          name: 'type',
          type: 'select',
          required: true,
          label: { de: 'Typ', en: 'Type' },
          options: [
            { label: { de: 'Upload-Checkliste', en: 'Upload Checklist' }, value: 'upload' },
            { label: { de: 'Produktions-Checkliste', en: 'Production Checklist' }, value: 'production' },
            { label: { de: 'Review-Checkliste', en: 'Review Checklist' }, value: 'review' },
            { label: { de: 'Post-Publish-Checkliste', en: 'Post-Publish Checklist' }, value: 'post_publish' },
          ],
          admin: { width: '50%' },
        },
        {
          name: 'format',
          type: 'select',
          label: { de: 'Format', en: 'Format' },
          options: [
            { label: { de: 'Alle Formate', en: 'All Formats' }, value: 'all' },
            { label: 'Short', value: 'short' },
            { label: 'Longform', value: 'longform' },
          ],
          defaultValue: 'all',
          admin: { width: '50%' },
        },
      ],
    },
    {
      name: 'description',
      type: 'textarea',
      label: { de: 'Beschreibung', en: 'Description' },
      localized: true,
      admin: {
        rows: 2,
        description: { de: 'Wann diese Checkliste verwendet werden soll', en: 'When to use this checklist' },
      },
    },
    {
      name: 'items',
      type: 'array',
      required: true,
      label: { de: 'Checklisten-Punkte', en: 'Checklist Items' },
      labels: {
        singular: { de: 'Punkt', en: 'Item' },
        plural: { de: 'Punkte', en: 'Items' },
      },
      minRows: 1,
      fields: [
        {
          type: 'row',
          fields: [
            {
              name: 'order',
              type: 'number',
              label: { de: 'Reihenfolge', en: 'Order' },
              min: 1,
              admin: { width: '15%' },
            },
            {
              name: 'task',
              type: 'text',
              required: true,
              localized: true,
              label: { de: 'Aufgabe', en: 'Task' },
              admin: {
                width: '55%',
                placeholder: 'z.B. "Thumbnail hochladen"',
              },
            },
            {
              name: 'category',
              type: 'select',
              label: { de: 'Kategorie', en: 'Category' },
              options: [
                { label: 'Metadaten', value: 'metadata' },
                { label: 'Assets', value: 'assets' },
                { label: 'SEO', value: 'seo' },
                { label: 'Community', value: 'community' },
                { label: { de: 'Rechtliches', en: 'Legal' }, value: 'legal' },
                { label: { de: 'Sonstiges', en: 'Other' }, value: 'other' },
              ],
              admin: { width: '30%' },
            },
          ],
        },
        {
          name: 'details',
          type: 'textarea',
          localized: true,
          label: { de: 'Details/Hinweise', en: 'Details/Notes' },
          admin: {
            rows: 2,
            placeholder: 'Zusätzliche Anweisungen oder Hinweise',
          },
        },
        {
          name: 'isRequired',
          type: 'checkbox',
          label: { de: 'Pflichtfeld', en: 'Required' },
          defaultValue: true,
        },
      ],
    },
    {
      name: 'isDefault',
      type: 'checkbox',
      label: { de: 'Standard-Vorlage', en: 'Default Template' },
      admin: {
        position: 'sidebar',
        description: { de: 'Wird automatisch für neue Videos verwendet', en: 'Automatically used for new videos' },
      },
    },
    {
      name: 'isActive',
      type: 'checkbox',
      label: { de: 'Aktiv', en: 'Active' },
      defaultValue: true,
      admin: {
        position: 'sidebar',
      },
    },
  ],
  timestamps: true,
}
