import { CollectionConfig } from 'payload'
import { isAdminOrEmployee } from '@/collections/access/admin'

export const Events: CollectionConfig = {
  slug: 'event',
  labels: {
    singular: {
      de: 'Veranstaltung',
    },
    plural: {
      de: 'Veranstaltungen',
    },
  },
  fields: [
    {
      name: 'photo',
      label: {
        de: 'Foto',
      },
      type: 'upload',
      relationTo: 'media',
    },
    {
      name: 'title',
      type: 'text',
      required: true,
      label: {
        de: 'Titel',
      },
    },
    {
      name: 'date',
      type: 'date',
      required: true,
      label: {
        de: 'Datum',
      },
      admin: {
        date: {
          pickerAppearance: 'dayAndTime',
          timeIntervals: 15,
          timeFormat: 'HH:mm'
        },
      },
    },
    {
      name: 'location',
      type: 'relationship',
      relationTo: 'locations',
      required: true,
      label: {
        de: 'Location'
      },
    },
    {
      name: 'parish',
      type: 'relationship',
      relationTo: 'parish',
      hasMany: true,
      label: {
        de: 'Gemeinde',
      },
      validate: (value, options) => {
        let user = options.req.user

        if (!user) {
          return 'You are not allowed to do this'
        }

        if (user.roles === 'user' && value) {
          return 'Sie sind nur erlaubt Veranstaltungen für Gruppen zu erstellen.'
        }

        return true
      },
    },
    {
      name: 'group',
      type: 'relationship',
      relationTo: 'group',
      hasMany: true,
      label: {
        de: 'Gruppe',
      },
      validate: (value: any, options: { req: { user: any } }) => {
        let user = options.req.user

        if (!user) {
          return 'You are not allowed to do this'
        }

        if (user.roles === 'user') {
          if (!user.groups || (user.groups && !user.groups.includes(value))) {
            return 'Sie können nur Veranstaltungen für Ihre eigene Gruppen erstellen!'
          }
        }

        return true
      },
    },
    {
      name: 'contact',
      type: 'relationship',
      relationTo: 'contactPerson',
      label: {
        de: "Ansprechperson"
      }
    },
    {
      name: 'shortDescription',
      type: 'textarea',
      required: true,
      label: {
        de: 'Kurzumschreibung',
      },
    },
    {
      name: 'description',
      type: 'textarea',
      required: true,
      label: {
        de: 'Umschreibung',
      },
    },
    {
      name: 'rsvpLink',
      type: 'text',
      required: false,
      label: {
        de: "Anmeldelink"
      }
    },
    {
      name: 'flyer',
      type: 'upload',
      relationTo: 'documents'
    },
    {
      name: 'cancelled',
      type: 'checkbox',
      required: true,
      label: {
        de: 'Abgesagt',
      },
      defaultValue: false,
    },
    {
      name: 'isRecurring',
      type: 'checkbox',
      required: true,
      label: {
        de: 'Regelmäßig',
      },
      defaultValue: false,
    },
  ],
  admin: {
    useAsTitle: 'title',
  },
  access: {
    read: () => true,
    delete: isAdminOrEmployee(),
  },
}
