import { CollectionConfig } from 'payload'
import { isAdmin } 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 { DonationBlock } from '@/collections/blocks/Donation'
import { YoutubePlayerBlock } from '@/collections/blocks/YoutubePlayer'
import { DonationAppeal } from '@/collections/blocks/DonationAppeal'
import { ImageCardsBlock } from '@/collections/blocks/ImageCards'
import { TitleBlock } from '@/collections/blocks/Title'
import { CollapsiblesBlock } from '@/collections/blocks/Collapsibles'
import { isPublishedPublic } from '@/collections/access/public'
import { NextPrevButtonsBlock } from '@/collections/blocks/NextPrevButtons'
import { ImageWithTextBlock } from '@/collections/blocks/ImageWithText'

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,
      admin: {
        description:
          'Slug der Gemeinde (z.B. "st-clara" → Gemeindeseite ist zu finden unter /gemeinde/st-clara)',
        components: {
          Field: '@/admin/components/SlugField/SlugField#SlugField',
        },
      },
      hooks: {
        beforeValidate: [
          ({ value }) =>
            typeof value === 'string' ? value.replace(/^\/+/, '') : value,
        ],
      },
    },
    {
      name: 'churches',
      label: {
        de: 'Kirchengebäuden',
      },
      type: 'relationship',
      relationTo: 'church',
      hasMany: true,
      required: true,
      admin: {
        allowCreate: false,
      },
    },
    {
      name: 'contactPersons',
      label: {
        de: "Kontakt"
      },
      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: 'content',
      label: {
        de: "Extra Kontent"
      },
      type: 'blocks',
      blocks: [
        ParagraphBlock,
        DocumentBlock,
        DonationBlock,
        YoutubePlayerBlock,
        DonationAppeal,
        ImageCardsBlock,
        TitleBlock,
        CollapsiblesBlock,
        NextPrevButtonsBlock,
        ImageWithTextBlock
      ]
    },
    {
      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
        },
        {
          name: 'caption',
          label: {
            de: 'Bildunterschrift'
          },
          type: 'text'
        },
      ],
      minRows: 3,
      maxRows: 12
    }
  ],
  admin: {
    useAsTitle: 'name',
    hidden: hideUnlessAssigned('parishes'),
    baseFilter: filterToAssigned('parishes'),
    livePreview: {
      url: ({ data }) => `/api/draft?url=/gemeinde/${data.slug}`,
    },
  },
  versions: {
    drafts: {
      validate: true
    },
  },
  access: {
    read: isPublishedPublic(),
    create: isAdmin(),
    update: isStaffOrAssigned('parishes'),
    delete: isAdmin(),
  },
}
