import { CollectionConfig } from 'payload'
import { revalidateTagHook } from '@/utils/revalidate'
import {
  canCreateAssignedWorship,
  canMutateAssignedWorship,
  filterWorshipToAssigned,
  hideUnlessAssigned,
  resolveAssignedChurchIds,
} from '@/collections/access/assigned'

export const Worship: CollectionConfig = {
  slug: 'worship',
  labels: {
    singular: {
      de: 'Gottesdienst',
    },
    plural: {
      de: 'Gottesdienst',
    },
  },
  fields: [
    {
      name: 'date',
      label: {
        de: 'Datum',
      },
      type: 'date',
      required: true,
      admin: {
        date: {
          pickerAppearance: 'dayAndTime',
          timeIntervals: 15,
          timeFormat: 'HH:mm'
        },
      },
    },
    {
      name: 'location',
      label: {
        de: 'Location',
      },
      type: 'relationship',
      relationTo: 'church',
      required: true,
      // The update access Where only constrains which docs may be targeted;
      // this keeps a Gläubiger from moving a worship doc to a foreign church.
      validate: async (value: unknown, options: any) => {
        const user = options.req?.user
        if (!user || user.roles !== 'user') return true
        const churchIds = await resolveAssignedChurchIds(options.req, user)
        if (!value || !churchIds.includes(String(value))) {
          return 'Sie sind nur berechtigt, Gottesdienste für Kirchen Ihrer zugewiesenen Gemeinden zu verwalten.'
        }
        return true
      },
    },
    {
      name: 'type',
      label: {
        de: 'Categorie',
      },
      type: 'radio',
      options: [
        {
          label: 'Eucharistiefeier',
          value: 'MASS',
        },
        {
          label: 'Familiengottesdienst',
          value: 'FAMILY',
        },
        {
          label: 'Wort-Gottes-Feier',
          value: 'WORD',
        },
        {
          label: 'Fremdsprache',
          value: 'LANGUAGE',
        },
        {
          label: 'Andere',
          value: 'OTHER',
        },
      ],
      required: true,
    },
    {
      name: 'title',
      type: 'text',
      required: false,
      label: {
        de: 'Titel'
      }
    },
    {
      name: 'cancelled',
      type: 'checkbox',
      required: true,
      defaultValue: false,
      label: {
        de: 'Abgesagt',
      },
      admin: {
        position: 'sidebar',
      },
    },
    {
      name: 'liturgicalDay',
      type: 'text',
      required: false,
      label: {
        de: 'Liturgischer Tag',
      },
    },
    {
      name: 'celebrant',
      type: 'text',
      required: false,
      label: {
        de: 'Zelebrant'
      }
    },
    {
      name: 'description',
      type: 'textarea',
      label: {
        de: 'Hinweise',
      },
    },
    {
      name: 'generated',
      type: 'checkbox',
      defaultValue: false,
      label: {
        de: 'Automatisch erzeugt',
      },
      admin: {
        readOnly: true,
        position: 'sidebar',
      },
    },
  ],
  admin: {
    defaultColumns: ["date", 'location', 'type', 'celebrant'],
    listSearchableFields: ['date', 'location'],
    hidden: hideUnlessAssigned('parishes'),
    baseFilter: filterWorshipToAssigned(),
  },
  access: {
    read: () => true,
    create: canCreateAssignedWorship(),
    update: canMutateAssignedWorship(),
    delete: canMutateAssignedWorship(),
  },
  hooks: {
    afterChange: [revalidateTagHook('worship')],
    afterDelete: [revalidateTagHook('worship')],
  },
}
