import type { CollectionConfig } from 'payload'

export const Products: CollectionConfig = {
  slug: 'products',
  labels: {
    singular: 'Produit',
    plural: 'Produits',
  },
  admin: {
    useAsTitle: 'productDisplayName',
    defaultColumns: ['productDisplayName', 'index', 'type', 'price', 'isPublished'],
    group: 'Contenu',
  },
  access: {
    read: () => true,
  },
  hooks: {
    beforeValidate: [
      ({ data }) => {
        if (!data?.productDisplayName) {
          return { ...data, skipSync: true }
        }
        return data
      },
    ],
  },
  versions: {
    drafts: {
      autosave: { interval: 800 },
    },
  },
  fields: [
    {
      name: 'previewPanel',
      type: 'ui',
      admin: {
        components: {
          Field: '@/components/admin/ProductPreviewEditor#default',
        },
      },
    },
    {
      name: 'productDisplayName',
      label: 'Nom affiché',
      type: 'text',
      required: true,
      admin: { hidden: true },
    },
    {
      name: 'name',
      label: 'Nom technique',
      type: 'text',
      required: true,
      admin: { hidden: true },
    },
    {
      name: 'slug',
      label: 'URL (slug)',
      type: 'text',
      required: true,
      unique: true,
      index: true,
      admin: { hidden: true },
    },
    {
      name: 'index',
      label: 'Index projet',
      type: 'text',
      required: true,
      admin: { hidden: true },
    },
    {
      name: 'type',
      label: 'Type',
      type: 'text',
      required: true,
      admin: { hidden: true },
    },
    {
      name: 'materials',
      label: 'Matériaux',
      type: 'text',
      required: true,
      admin: { hidden: true },
    },
    {
      name: 'year',
      label: 'Année',
      type: 'text',
      required: true,
      defaultValue: '2026',
      admin: { hidden: true },
    },
    {
      name: 'status',
      label: 'Statut',
      type: 'text',
      required: true,
      admin: { hidden: true },
    },
    {
      name: 'sortOrder',
      label: 'Ordre d\u2019affichage',
      type: 'number',
      defaultValue: 0,
      admin: { hidden: true },
    },
    {
      name: 'isPublished',
      label: 'Publié',
      type: 'checkbox',
      defaultValue: true,
      admin: { hidden: true },
    },
    {
      name: 'description',
      label: 'Description',
      type: 'textarea',
      required: true,
      admin: { hidden: true },
    },
    {
      name: 'specs',
      label: 'Spécifications',
      type: 'textarea',
      admin: { hidden: true },
    },
    {
      name: 'notes',
      label: 'Notes',
      type: 'textarea',
      admin: { hidden: true },
    },
    {
      name: 'images',
      label: 'Images',
      type: 'array',
      minRows: 1,
      required: true,
      admin: { hidden: true },
      fields: [
        {
          name: 'image',
          type: 'upload',
          relationTo: 'media',
          required: true,
        },
      ],
    },
    {
      name: 'price',
      label: 'Prix (en centimes)',
      type: 'number',
      admin: { hidden: true },
    },
    {
      name: 'currency',
      label: 'Devise',
      type: 'select',
      defaultValue: 'EUR',
      options: [
        { label: 'EUR', value: 'EUR' },
        { label: 'USD', value: 'USD' },
      ],
      admin: { hidden: true },
    },
    {
      name: 'availability',
      label: 'Disponibilité (schema.org)',
      type: 'select',
      defaultValue: 'https://schema.org/InStock',
      options: [
        { label: 'En stock', value: 'https://schema.org/InStock' },
        { label: 'Disponibilité limitée', value: 'https://schema.org/LimitedAvailability' },
        { label: 'Sur commande', value: 'https://schema.org/PreOrder' },
        { label: 'Indisponible', value: 'https://schema.org/OutOfStock' },
      ],
      admin: { hidden: true },
    },
    {
      name: 'stripeProductID',
      type: 'text',
      admin: { hidden: true, readOnly: true },
    },
    {
      name: 'seoTitle',
      label: 'Titre SEO',
      type: 'text',
      admin: { hidden: true },
    },
    {
      name: 'seoDescription',
      label: 'Description SEO',
      type: 'textarea',
      admin: { hidden: true },
    },
  ],
}
