import type { CollectionConfig } from 'payload'

export const Users: CollectionConfig = {
  slug: 'users',
  labels: {
    singular: 'Benutzer',
    plural: 'Benutzer',
  },
  admin: {
    useAsTitle: 'email',
    group: 'System',
    defaultColumns: ['email', 'firstName', 'lastName', 'role'],
  },
  auth: true,
  access: {
    read:   ({ req }) => Boolean(req.user),
    create: ({ req }) => req.user?.role === 'admin',
    update: ({ req }) => Boolean(req.user),
    delete: ({ req }) => req.user?.role === 'admin',
  },
  fields: [
    {
      type: 'row',
      fields: [
        {
          name: 'firstName',
          label: 'Vorname',
          type: 'text',
        },
        {
          name: 'lastName',
          label: 'Nachname',
          type: 'text',
        },
      ],
    },
    {
      name: 'role',
      label: 'Rolle',
      type: 'select',
      required: true,
      defaultValue: 'author',
      options: [
        { label: 'Administrator', value: 'admin' },
        { label: 'Redakteur',     value: 'editor' },
        { label: 'Autor',         value: 'author' },
      ],
      access: {
        // only admins can change roles
        update: ({ req }) => req.user?.role === 'admin',
      },
    },
  ],
}