import type { CollectionConfig } from 'payload'

import { anyone } from '../access/anyone'
import { authenticated } from '../access/authenticated'
import { adminOrEditor } from '../access/adminOrEditor'
import { auditChange } from './Audit/hooks/auditHooks'
import { slugField } from '@/fields/slug'

export const Categories: CollectionConfig = {
  slug: 'categories',
  access: {
    create: adminOrEditor,
    delete: adminOrEditor,
    read: anyone,
    update: adminOrEditor,
  },
  admin: {
    useAsTitle: 'title',
  },
  hooks: {
    afterChange: [auditChange('categories')],
    afterDelete: [auditChange('categories')],
  },
  fields: [
    {
      name: 'title',
      type: 'text',
      required: true,
      label: '分類名稱（中文）',
    },
    {
      name: 'nameEn',
      type: 'text',
      label: '英文名稱',
      admin: {
        description: '用於 URL 或國際化',
      },
    },
    {
      name: 'order',
      type: 'number',
      label: '排序順序',
      defaultValue: 0,
      admin: {
        position: 'sidebar',
        description: '數字越小越靠前',
      },
    },
    {
      name: 'textColor',
      type: 'text',
      label: '文字顏色',
      defaultValue: '#000000',
      admin: {
        description: '十六進制顏色碼，例如 #000000',
      },
    },
    {
      name: 'backgroundColor',
      type: 'text',
      label: '背景顏色',
      defaultValue: '#ffffff',
      admin: {
        description: '十六進制顏色碼，例如 #ffffff',
      },
    },
    ...slugField(),
  ],
}
