// src/collections/Studies.ts
import type { CollectionConfig } from 'payload';

// A simple function to convert a string to a slug
const formatSlug = (val: string): string => {
  return val
    .normalize("NFKC")
    .replace(/ /g, '-')
    .replace(/[^\w-]+/g, '')
    .toLowerCase();
};


const Studies: CollectionConfig = {
  // The 'slug' is the name of your collection in the database
  // and the URL to access it in the API
  slug: 'studies',
  labels: {
      singular: 'Story',
      plural: 'Stories', // This will change the name in the sidebar
  },
  admin: {
    // This will use the 'title' field as the display name for documents
    useAsTitle: 'title',
    // Default columns to show in the collection list view
    defaultColumns: ['title', 'author', 'status', 'publishedDate'],
  },
  access: {
    // Anyone can read posts
    read: () => true,
  },
  fields: [
    {
      name: 'title',
      label: 'Title',
      type: 'text',
      required: true,
    },
    {
      name: 'description',
      label: 'Description',
      type: 'richText', // Use 'textarea' for a multi-line description
      required: false, // You can set this to true if it's mandatory
    },
    {
      name: 'content',
      label: 'Content',
      type: 'richText', // A powerful rich text editor
      required: true,
    },
    {
      name: 'video',
      label: 'Video URL',
      type: 'text', // Use 'text' to store a URL (e.g., from YouTube, Vimeo)
      required: false,
    },
    // --- END OF CHANGES ---
    {
      name: 'author',
      label: 'Author',
      type: 'relationship',
      // Relate this post to the 'users' collection
      relationTo: 'users',
      required: true,
      // Don't show this field in the admin UI's list view
      admin: {
        position: 'sidebar',
      },
    },
    {
      name: 'publishedDate',
      label: 'Published Date',
      type: 'date',
      required: true,
      admin: {
        position: 'sidebar',
        date: {
          pickerAppearance: 'dayOnly',
          displayFormat: 'd MMM yyyY',
        },
      },
    },
    {
      name: 'status',
      label: 'Status',
      type: 'select',
      options: [
        { label: 'Draft', value: 'draft' },
        { label: 'Published', value: 'published' },
      ],
      defaultValue: 'draft',
      admin: {
        position: 'sidebar',
      },
    },
    {
      name: 'tags',
      label: 'Tags',
      type: 'relationship',
      // A post can have many tags
      hasMany: true,
      // Relate this post to the 'tags' collection
      relationTo: 'tags',
      admin: {
        position: 'sidebar',
      },
    },
    {
      name: 'featuredImage',
      label: 'Featured Image',
      type: 'upload',
      // Relate this post to the 'media' collection
      relationTo: 'media',
    },
    {
      name: 'slug',
      label: 'Slug',
      type: 'text',
      // Slugs should be unique and indexed for better performance
      unique: true,
      index: true,
      admin: {
        position: 'sidebar',
      },
      hooks: {
        // This hook runs before the document is validated
        beforeValidate: [
          ({ value, data }) => {
            // If there is a title and no slug, create a slug from the title
            if (data?.title && !value) {
              return formatSlug(data.title)
            }
            // Otherwise, use the existing value
            return value
          },
        ],
      },
    },
  ],
}

export default Studies;