import { CollectionConfig } from 'payload'
import { hide, isAdminOrEmployee } from '@/collections/access/admin'
import { nextSunday } from '@/utils/sunday'

export const LiturgicalCalendar: CollectionConfig = {
  slug: "calendar",
  labels: {
    singular: {
      de: "Liturgischer Kalender"
    },
    plural: {
      de: "Liturgischer Kalender"
    }
  },
  fields: [
    {
      name: 'date',
      type: 'date',
      required: true,
      label: {
        de: 'Datum'
      },
      defaultValue: nextSunday()
    },
    {
      name: 'parish',
      type: "relationship",
      relationTo: 'parish',
      required: true,
      hasMany: true,
      label: {
        de: "Gemeinde"
      },
      admin: {
        allowCreate: false,
        allowEdit: false
      }
    },
    {
      name: 'document',
      label: {
        de: "PDF-Dokument"
      },
      type: 'upload',
      relationTo: 'documents',
      required: true
    }
  ],
  admin: {
    hidden: hide,
    defaultColumns: ['date', 'parish', 'document'],
    description: "Der Kalender wird jeden Samstag automatisch veröffentlicht.",
  },
  access: {
    read: () => true,
    create: isAdminOrEmployee(),
    update: isAdminOrEmployee(),
    delete: isAdminOrEmployee(),
  }
}