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',
    livePreview: {
      url: ({ data }) =>
        `${process.env.NEXT_PUBLIC_SERVER_URL ?? 'http://localhost:3000'}/collection/${data?.slug}?preview=true`,
    },
  },
  access: {
    read: () => true,
  },
  versions: {
    drafts: {
      autosave: { interval: 800 },
    },
  },
  fields: [
    {
      type: 'tabs',
      tabs: [
        {
          label: 'Identité',
          fields: [
            {
              name: 'productDisplayName',
              label: 'Nom affiché',
              type: 'text',
              required: true,
              admin: { description: 'Le nom visible par le public (ex: Solar Altar)' },
            },
            {
              name: 'name',
              label: 'Nom technique',
              type: 'text',
              required: true,
              admin: { description: 'Nom interne sans espaces (ex: Solar_Altar)' },
            },
            {
              name: 'slug',
              label: 'URL (slug)',
              type: 'text',
              required: true,
              unique: true,
              index: true,
              admin: { description: 'Ex: solar-altar → /collection/solar-altar' },
            },
            {
              name: 'index',
              label: 'Index projet',
              type: 'text',
              required: true,
              admin: { description: 'Ex: PROJET_001' },
            },
            {
              name: 'type',
              label: 'Type',
              type: 'text',
              required: true,
              admin: { description: 'Ex: LAMPE DE TABLE' },
            },
            {
              name: 'materials',
              label: 'Matériaux',
              type: 'text',
              required: true,
            },
            {
              name: 'year',
              label: 'Année',
              type: 'text',
              required: true,
              defaultValue: '2026',
            },
            {
              name: 'status',
              label: 'Statut',
              type: 'text',
              required: true,
              admin: { description: 'Ex: PROTOTYPE [80%]' },
            },
            {
              name: 'sortOrder',
              label: 'Ordre d\u2019affichage',
              type: 'number',
              defaultValue: 0,
            },
            {
              name: 'isPublished',
              label: 'Publié',
              type: 'checkbox',
              defaultValue: true,
            },
          ],
        },
        {
          label: 'Contenu',
          fields: [
            {
              name: 'description',
              label: 'Description',
              type: 'textarea',
              required: true,
            },
            {
              name: 'specs',
              label: 'Spécifications',
              type: 'textarea',
            },
            {
              name: 'notes',
              label: 'Notes',
              type: 'textarea',
            },
          ],
        },
        {
          label: 'Images',
          fields: [
            {
              name: 'images',
              label: 'Images',
              type: 'array',
              minRows: 1,
              required: true,
              fields: [
                {
                  name: 'image',
                  type: 'upload',
                  relationTo: 'media',
                  required: true,
                },
              ],
            },
          ],
        },
        {
          label: 'Commerce',
          fields: [
            {
              name: 'price',
              label: 'Prix (en centimes)',
              type: 'number',
              admin: {
                description: '180000 = 1 800 EUR. Vide = non disponible à la vente.',
              },
            },
            {
              name: 'currency',
              label: 'Devise',
              type: 'select',
              defaultValue: 'EUR',
              options: [
                { label: 'EUR', value: 'EUR' },
                { label: 'USD', value: 'USD' },
              ],
            },
            {
              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' },
              ],
            },
            {
              name: 'stripeProductID',
              type: 'text',
              admin: {
                readOnly: true,
                description: 'Synchronisé automatiquement avec Stripe',
              },
            },
          ],
        },
        {
          label: 'SEO',
          fields: [
            {
              name: 'seoTitle',
              label: 'Titre SEO',
              type: 'text',
              admin: { description: 'Laissez vide pour utiliser le nom du produit' },
            },
            {
              name: 'seoDescription',
              label: 'Description SEO',
              type: 'textarea',
            },
          ],
        },
      ],
    },
  ],
}
