import { Block, CollectionConfig } from 'payload'
import { isAdmin, isAdminOrEmployee } from '@/collections/access/admin'

const Content: Block = {
  slug: 'content',
  fields: [
    {
      name: 'content',
      type: 'richText',
      required: true
    }
  ]
}

const Title: Block = {
  slug: 'title',
  fields: [
    {
      name: 'title',
      type: 'text',
      required: true,
    },
  ]
}

const Testimony: Block = {
  slug: 'testimony',
  labels: {
    singular: {
      de: 'Zeugnis'
    },
    plural: {
      de: 'Zeugnisse'
    }
  },
  fields: [
    {
      name: 'testimony',
      type: 'relationship',
      relationTo: 'testimony',
      required: true
    }
  ]
}

export const Pages: CollectionConfig = {
  slug: 'page',
  labels: {
    singular: {
      de: 'Seite'
    },
    plural: {
      de: "Seiten"
    },
  },
  fields: [
    {
      name: "title",
      type: "text",
      required: true,
      label: {
        de: "Titel"
      }
    },
    {
      name: 'slug',
      type: 'text',
      required: true,
      label: {
        de: "Slug"
      }
    },
    {
      name: 'content',
      type: 'blocks',
      required: true,
      blocks: [
        Content,
        Title,
        Testimony
      ]
    }
  ],
  admin: {
    useAsTitle: "title"
  },
  access: {
    read: () => true,
    create: isAdminOrEmployee(),
    update: isAdminOrEmployee(),
    delete: isAdmin(),
  }
}