import { Block } from 'payload'

export const ImageCardsBlock: Block = {
  slug: 'imageCards',
  labels: {
    singular: { de: 'Bildkarten' },
    plural: { de: 'Bildkarten' },
  },
  fields: [
    {
      name: 'items',
      label: { de: 'Karten' },
      type: 'array',
      required: true,
      minRows: 1,
      fields: [
        {
          name: 'title',
          label: { de: 'Titel' },
          type: 'text',
          required: true,
        },
        {
          name: 'image',
          label: { de: 'Bild' },
          type: 'upload',
          relationTo: 'media',
          required: true,
        },
        {
          name: 'link',
          label: { de: 'Verknüpfung (Seite oder Gruppe)' },
          type: 'relationship',
          relationTo: ['pages', 'group'],
          required: false,
          admin: {
            condition: (_, siblingData) => !siblingData?.customLink,
          },
        },
        {
          name: 'customLink',
          label: { de: 'Eigener Link (URL)' },
          type: 'text',
          required: false,
          admin: {
            condition: (_, siblingData) => !siblingData?.link,
            description:
              'Alternative zu "Verknüpfung". Nur eins von beiden kann gesetzt werden.',
          },
        },
      ],
    },
  ],
}
