import { CollectionConfig } from 'payload'
import { hide, isAdminOrEmployee } from '@/collections/access/admin'
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 { ImageCardsBlock } from '@/collections/blocks/ImageCards'
import { isPublishedPublic } from '@/collections/access/public'


export const Blog: CollectionConfig = {
  slug: 'blog',
  labels: {
    singular: {
      de: 'Blogpost',
    },
    plural: {
      de: 'Blog',
    },
  },
  fields: [
    {
      name: 'photo',
      type: 'upload',
      label: {
        de: 'Bild'
      },
      relationTo: 'media',
    },
    {
      name: 'title',
      type: 'text',
      required: true,
      label: {
        de: 'Titel',
      },
    },
    {
      name: 'pinned',
      type: 'checkbox',
      required: true,
      defaultValue: false,
      label: {
        de: 'Angepinnt',
      },
      admin: {
        position: 'sidebar',
        description: 'Angepinnte Beiträge werden im Blog und auf der Startseite zuerst angezeigt.',
      },
    },
    {
      type: 'tabs',
      tabs: [
        {
          name: 'content',
          fields: [
            {
              name: 'excerpt',
              type: 'textarea',
              label: {
                de: 'Auszug',
              },
              required: true,
            },
            {
              name: 'content',
              type: 'blocks',
              minRows: 1,
              maxRows: 20,
              blocks: [
                ParagraphBlock,
                DocumentBlock,
                DonationBlock,
                ContactformBlock,
                GalleryBlock,
                YoutubePlayerBlock,
                ButtonBlock,
                ImageCardsBlock,
              ],
              required: true,
            },
          ],
        },
        {
          name: 'configuration',
          label: {
            de: 'Einstellungen'
          },
          fields: [
            {
              name: 'showOnFrontpage',
              type: 'checkbox',
              label: {
                de: 'Anzeigen auf Startseite?',
              },
              defaultValue: true,
              required: true,
            },
            {
              name: 'displayFromDate',
              type: 'date',
              label: {
                de: 'Anzeigen von',
              },
              required: false,
            },
            {
              name: 'displayTillDate',
              type: 'date',
              label: {
                de: 'Anzeigen bis',
              },
              required: false,
            },
            {
              name: 'parish',
              type: 'relationship',
              relationTo: 'parish',
              hasMany: true,
              label: {
                de: 'Gemeinde',
              },
            },
          ],
        },

      ],
    },
  ],
  defaultSort: ['-pinned', '-createdAt'],
  admin: {
    useAsTitle: 'title',
    defaultColumns: ['title', 'pinned', 'updatedAt'],
    hidden: hide,
    livePreview: {
      url: ({ data }) => `/api/draft?url=/blog/${data.id}`,
    }
  },
  versions: {
    drafts: {
      validate: true
    }
  },
  access: {
    read: isPublishedPublic(),
    create: isAdminOrEmployee(),
    update: isAdminOrEmployee(),
    delete: isAdminOrEmployee(),
  },
}