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

export const Parish: CollectionConfig = {
  slug: 'parish',
  labels: {
    singular: {
      de: 'Gemeinde',
    },
    plural: {
      de: 'Gemeinden',
    },
  },
  fields: [
    {
      name: 'name',
      label: {
        de: 'Name',
      },
      type: 'text',
      required: true,
    },
    {
      name: 'slug',
      label: {
        de: 'URL slug',
      },
      type: 'text',
      required: true
    },
    {
      name: 'churches',
      label: {
        de: 'Kirchengebäuden',
      },
      type: 'relationship',
      relationTo: 'church',
      hasMany: true,
      required: true,
      admin: {
        allowCreate: false,
      },
    },
    {
      name: 'employees',
      label: {
        de: 'Mitarbeiter',
      },
      type: 'relationship',
      relationTo: 'employees',
      hasMany: true,
    },
    {
      name: 'contactPersons',
      label: {
        de: "Ansprechpartner"
      },
      type: 'array',
      fields: [
        {
          name: 'title',
          label: {
            de: 'Titel'
          },
          type: 'text',
          required: true
        },
        {
          name: 'description',
          label: {
            de: 'Umschreibung'
          },
          type: 'textarea',
          required: true
        }
      ]
    },
    {
      name: 'description',
      label: {
        de: 'Umschreibung',
      },
      type: 'textarea',
      required: true,
      admin: {
        rows: 15,
      },
    },
    {
      name: 'history',
      label: {
        de: 'Geschichte',
      },
      type: 'textarea',
      required: true,
      admin: {
        rows: 15,
      },
    },
    {
      name: 'contact',
      label: {
        de: 'Kontaktinformation',
      },
      type: 'textarea',
      required: true,
      admin: {
        rows: 10,
      },
    },
    {
      name: 'photo',
      label: {
        de: "Hauptbild"
      },
      type: 'upload',
      relationTo: 'media',
      required: true
    },
    {
      name: 'gallery',
      label: {
        de: 'Weitere Bilder'
      },
      type: 'array',
      fields: [
        {
          name: 'photo',
          label: {
            de: 'Bild'
          },
          type: 'upload',
          relationTo: 'media',
          required: true
        },
      ],
      minRows: 3,
      maxRows: 12
    }
  ],
  admin: {
    useAsTitle: 'name',
  },
  access: {
    read: () => true,
    create: isAdmin(),
    update: isAdminOrEmployee(),
    delete: isAdmin(),
  },
}
