import type { CollectionConfig } from 'payload'

export const News: CollectionConfig = {
  slug: 'news',
  admin: {
    useAsTitle: 'title',
  },
  fields: [
    {
      name: 'title',
      type: 'text',
      label: 'Title',
      localized: true,
      required: true,
    },
    {
      name: 'subtitle',
      type: 'text',
      label: 'Subtitle',
      localized: true,
      required: true,
    },
    {
      name: 'image',
      type: 'upload',
      relationTo: 'media',
      label: 'Image',
      required: true,
    },
    {
      name: 'author',
      type: 'text',
      label: 'Author',
      required: true,
    },
    {
      name: 'authorImage',
      type: 'upload',
      relationTo: 'media',
      label: 'Author Image',
      required: true,
    },
    {
      name: 'newsId',
      type: 'text',
      label: 'news unique ID',
      admin: {
        readOnly: true,
      },
      hooks: {
        beforeChange: [
          ({ value }) => {
            return value || crypto.randomUUID()
          },
        ],
      },
    },
    {
      name: 'contents',
      type: 'array',
      label: 'Contents',
      fields: [
        {
          name: 'title',
          type: 'text',
          label: 'Title',
          localized: true,
          required: true,
        },
        {
          name: 'content',
          type: 'richText',
          label: 'Content Block',
          localized: true,
          required: true,
        },
        {
          name: 'id',
          type: 'text',
          label: 'ID',
          admin: {
            readOnly: true,
          },
          hooks: {
            beforeChange: [
              ({ value }) => {
                return value || crypto.randomUUID()
              },
            ],
          },
        },
      ],
    },
    {
      name: 'tags',
      type: 'array',
      label: 'Tags',
      fields: [
        {
          name: 'title',
          type: 'text',
          label: 'Title',
          localized: true,
          required: true,
        },
      ],
    },
  ],
  timestamps: true,
}
