import { CollectionConfig } from 'payload'
import { revalidateTag } from 'next/cache'
import { isAdminOrEmployee } from '@/collections/access/admin'
import { filterToAssigned, hideUnlessAssigned, isStaffOrAssigned } from '@/collections/access/assigned'
import { ParagraphBlock } from '@/collections/blocks/Paragraph'
import { DocumentBlock } from '@/collections/blocks/Document'
import { ContactformBlock } from '@/collections/blocks/Contactform'
import { GalleryBlock } from '@/collections/blocks/Gallery'
import { DonationBlock } from '@/collections/blocks/Donation'
import { ButtonBlock } from '@/collections/blocks/Button'
import { YoutubePlayerBlock } from '@/collections/blocks/YoutubePlayer'
import { PageHeaderBlock } from '@/collections/blocks/PageHeader'
import { SectionBlock } from '@/collections/blocks/Section'
import { TitleBlock } from '@/collections/blocks/Title'
import { BannerBlock } from '@/collections/blocks/Banner'
import { MainTextBlock } from '@/collections/blocks/MainText'
import { HorizontalRuleBlock } from '@/collections/blocks/HorizontalRule'
import { BlogSliderBlock } from '@/collections/blocks/BlogSlider'
import { MassTimesBlock } from '@/collections/blocks/MassTimes'
import { CollapsibleImageWithTextBlock } from '@/collections/blocks/CollapsibleImageWithText'
import { ImageWithTextBlock } from '@/collections/blocks/ImageWithText'
import { CollapsibleMapWithTextBlock } from '@/collections/blocks/CollapsibleMapWithText'
import { CollapsiblesBlock } from '@/collections/blocks/Collapsibles'
import { EventsBlock } from '@/collections/blocks/Events'
import { GroupEventsBlock } from '@/collections/blocks/GroupEvents'
import { PublicationAndNewsletterBlock } from '@/collections/blocks/PublicationAndNewsletter'
import { ContactPersonBlock } from '@/collections/blocks/ContactPersonBlock'
import { ImageCardsBlock } from '@/collections/blocks/ImageCards'
import { ClassifiedsBlock } from '@/collections/blocks/Classifieds'
import { NextPrevButtonsBlock } from '@/collections/blocks/NextPrevButtons'
import { isPublishedPublic } from '@/collections/access/public'

export const Pages: CollectionConfig = {
  slug: 'pages',
  labels: {
    singular: {
      de: 'Seite',
    },
    plural: {
      de: 'Seiten',
    },
  },
  fields: [
    {
      name: 'title',
      type: 'text',
      required: true,
      label: {
        de: 'Titel',
      },
    },
    {
      name: 'description',
      type: 'textarea',
      label: {
        de: 'Beschreibung',
      },
    },
    {
      name: 'slug',
      type: 'text',
      defaultValue: '',
      required: false,
      unique: true,
      label: {
        de: 'Slug',
      },
      admin: {
        description: 'URL-Pfad der Seite (z.B. "meine-seite" → /meine-seite)',
        components: {
          Field: {
            path: '@/admin/components/SlugField/SlugField#SlugField',
            clientProps: {
              sourceField: 'title',
            },
          },
        },
      },
      hooks: {
        beforeValidate: [
          ({ value }) =>
            typeof value === 'string' ? value.replace(/^\/+/, '') : value,
        ],
      },
    },
    {
      name: 'content',
      type: 'blocks',
      label: {
        de: 'Inhalt',
      },
      blocks: [
        PageHeaderBlock,
        ParagraphBlock,
        TitleBlock,
        SectionBlock,
        GalleryBlock,
        DocumentBlock,
        YoutubePlayerBlock,
        ButtonBlock,
        ContactformBlock,
        DonationBlock,
        BannerBlock,
        MainTextBlock,
        HorizontalRuleBlock,
        BlogSliderBlock,
        CollapsibleImageWithTextBlock,
        ImageWithTextBlock,
        CollapsibleMapWithTextBlock,
        CollapsiblesBlock,
        MassTimesBlock,
        EventsBlock,
        GroupEventsBlock,
        ContactPersonBlock,
        ImageCardsBlock,
        ClassifiedsBlock,
        NextPrevButtonsBlock,
      ],
    },
  ],
  admin: {
    useAsTitle: 'title',
    hidden: hideUnlessAssigned('pages'),
    baseFilter: filterToAssigned('pages'),
    livePreview: {
      url: ({ data }) => `/api/draft?url=/${data.slug}`,
    },
  },
  versions: {
    drafts: {
      validate: true
    },
  },
  access: {
    read: isPublishedPublic(),
    create: isAdminOrEmployee(),
    update: isStaffOrAssigned('pages'),
    delete: isAdminOrEmployee(),
  },
  hooks: {
    afterChange: [
      ({ doc }) => {
        if (doc.slug) revalidateTag(`pages-${doc.slug}`)
      },
    ],
  },
}
