import type { CollectionConfig } from 'payload'

export const Navigations: CollectionConfig = {
  slug: 'navigations',
  labels: {
    singular: 'Navigation',
    plural: 'Navigationen',
  },
  admin: {
    useAsTitle: 'title',
    group: 'Einstellungen',
    description: 'Navigationsmenüs pro Tenant',
  },
  access: {
    read: () => true,
    create: ({ req }) => Boolean(req.user?.isSuperAdmin),
    update: ({ req }) => Boolean(req.user),
    delete: ({ req }) => Boolean(req.user?.isSuperAdmin),
  },
  fields: [
    {
      name: 'title',
      type: 'text',
      label: 'Titel',
      required: true,
      defaultValue: 'Hauptnavigation',
      admin: {
        description: 'Interner Name zur Identifikation',
      },
    },
    {
      name: 'mainMenu',
      type: 'array',
      label: 'Hauptmenü',
      fields: [
        {
          name: 'label',
          type: 'text',
          label: 'Bezeichnung',
          required: true,
          localized: true,
        },
        {
          name: 'type',
          type: 'select',
          label: 'Typ',
          defaultValue: 'page',
          options: [
            { label: 'Seite', value: 'page' },
            { label: 'Eigener Link', value: 'custom' },
            { label: 'Untermenü', value: 'submenu' },
          ],
        },
        {
          name: 'page',
          type: 'relationship',
          label: 'Seite',
          relationTo: 'pages',
          admin: {
            condition: (data, siblingData) => siblingData?.type === 'page',
          },
        },
        {
          name: 'url',
          type: 'text',
          label: 'URL',
          admin: {
            condition: (data, siblingData) => siblingData?.type === 'custom',
          },
        },
        {
          name: 'openInNewTab',
          type: 'checkbox',
          label: 'In neuem Tab öffnen',
          defaultValue: false,
        },
        {
          name: 'submenu',
          type: 'array',
          label: 'Untermenü',
          admin: {
            condition: (data, siblingData) => siblingData?.type === 'submenu',
          },
          fields: [
            {
              name: 'label',
              type: 'text',
              label: 'Bezeichnung',
              required: true,
              localized: true,
            },
            {
              name: 'linkType',
              type: 'select',
              label: 'Link-Typ',
              defaultValue: 'page',
              options: [
                { label: 'Seite', value: 'page' },
                { label: 'Eigener Link', value: 'custom' },
              ],
            },
            {
              name: 'page',
              type: 'relationship',
              label: 'Seite',
              relationTo: 'pages',
              admin: {
                condition: (data, siblingData) => siblingData?.linkType === 'page',
              },
            },
            {
              name: 'url',
              type: 'text',
              label: 'URL',
              admin: {
                condition: (data, siblingData) => siblingData?.linkType === 'custom',
              },
            },
          ],
        },
      ],
    },
    {
      name: 'footerMenu',
      type: 'array',
      label: 'Footer-Menü',
      fields: [
        {
          name: 'label',
          type: 'text',
          label: 'Bezeichnung',
          required: true,
          localized: true,
        },
        {
          name: 'linkType',
          type: 'select',
          label: 'Link-Typ',
          defaultValue: 'page',
          options: [
            { label: 'Seite', value: 'page' },
            { label: 'Eigener Link', value: 'custom' },
          ],
        },
        {
          name: 'page',
          type: 'relationship',
          label: 'Seite',
          relationTo: 'pages',
          admin: {
            condition: (data, siblingData) => siblingData?.linkType === 'page',
          },
        },
        {
          name: 'url',
          type: 'text',
          label: 'URL',
          admin: {
            condition: (data, siblingData) => siblingData?.linkType === 'custom',
          },
        },
      ],
    },
  ],
}
