import { CollectionConfig } from 'payload'
import { isAdminOrEmployee } from '@/collections/access/admin'
import { filterToAssigned, hideUnlessAssigned, isStaffOrAssigned } from '@/collections/access/assigned'
import { ParagraphBlock } from '@/collections/blocks/Paragraph'
import { GalleryBlock } from '@/collections/blocks/Gallery'
import { ContactformBlock } from '@/collections/blocks/Contactform'
import { DocumentBlock } from '@/collections/blocks/Document'
import { DonationBlock } from '@/collections/blocks/Donation'
import { ButtonBlock } from '@/collections/blocks/Button'
import { YoutubePlayerBlock } from '@/collections/blocks/YoutubePlayer'
import { isPublishedPublic } from '@/collections/access/public'
import { ImageCardsBlock } from '@/collections/blocks/ImageCards'
import { CollapsiblesBlock } from '@/collections/blocks/Collapsibles'
import { TitleBlock } from '@/collections/blocks/Title'
import { NextPrevButtonsBlock } from '@/collections/blocks/NextPrevButtons'
import { ContactPersonBlock } from '@/collections/blocks/ContactPersonBlock'
import { ImageWithTextBlock } from '@/collections/blocks/ImageWithText'

export const Groups: CollectionConfig = {
  slug: 'group',
  labels: {
    singular: {
      de: 'Gruppe',
    },
    plural: {
      de: 'Gruppen',
    },
  },
  fields: [
    {
      name: 'photo',
      label: {
        de: 'Foto',
      },
      type: 'upload',
      relationTo: 'media',
    },
    {
      name: 'name',
      type: 'text',
      label: {
        de: 'Name',
      },
      required: true,
    },
    {
      name: 'slug',
      type: 'text',
      label: {
        de: 'URL slug',
      },
      required: true,
      unique: true,
      admin: {
        description:
          'Slug der Gruppe (z.B. "ministrant" → Gruppenseite ist zu finden unter /gruppe/ministrant)',
        components: {
          Field: '@/admin/components/SlugField/SlugField#SlugField',
        },
      },
      hooks: {
        beforeValidate: [
          ({ value }) =>
            typeof value === 'string' ? value.replace(/^\/+/, '') : value,
        ],
      },
    },
    {
      name: 'shortDescription',
      type: 'textarea',
      label: {
        de: 'Kurzumschreibung',
      },
      required: true,
    },
    {
      name: 'text',
      type: 'richText',
      label: {
        de: 'Umschreibung',
      },
      required: false,
    },
    {
      name: 'content',
      type: 'blocks',
      blocks: [
        TitleBlock,
        ParagraphBlock,
        GalleryBlock,
        DocumentBlock,
        DonationBlock,
        YoutubePlayerBlock,
        ContactPersonBlock,
        ContactformBlock,
        ButtonBlock,
        ImageCardsBlock,
        CollapsiblesBlock,
        NextPrevButtonsBlock,
        ImageWithTextBlock
      ]
    }
  ],
  defaultPopulate: {
    slug: true
  },
  admin: {
    useAsTitle: 'name',
    hidden: hideUnlessAssigned('groups'),
    baseFilter: filterToAssigned('groups'),
    livePreview: {
      url: ({ data }) => `/api/draft?url=/gruppe/${data.slug}`,
    },
  },
  versions: {
    drafts: {
      validate: true
    },
  },
  access: {
    read: isPublishedPublic(),
    create: isAdminOrEmployee(),
    update: isStaffOrAssigned('groups'),
    delete: isAdminOrEmployee(),
  },
}
