import type { GlobalConfig } from 'payload'
import { anyone, isEditorOrAdmin } from '../access/roles'
import { loc } from '../fields/localized'

// One product (EPCO X1) => a Global. The 8 views + 6 details keep their existing
// asset `file` path AND an optional `image` upload override; the content endpoint
// emits image?.url || file so the bike studio shots stay intact until replaced.
export const Product: GlobalConfig = {
  slug: 'product',
  admin: { group: 'Rental' },
  access: { read: anyone, update: isEditorOrAdmin },
  versions: { drafts: true },
  fields: [
    { name: 'name', type: 'text', required: true },
    { name: 'tagline', type: 'text', ...loc },
    { name: 'pitch', type: 'text', ...loc },
    {
      name: 'specs', type: 'array', admin: { description: 'Technical spec cells.' },
      fields: [
        { name: 'key', type: 'text', required: true, admin: { description: 'Stable id, e.g. motor' } },
        { name: 'label', type: 'text', required: true, ...loc },
        { name: 'value', type: 'text', required: true },
        { name: 'sub', type: 'text', ...loc },
        { name: 'icon', type: 'text' },
      ],
    },
    {
      name: 'features', type: 'array',
      fields: [
        { name: 'tag', type: 'text' },
        { name: 'label', type: 'text', ...loc },
      ],
    },
    {
      name: 'views', type: 'array', admin: { description: '360° studio angles.' },
      fields: [
        { name: 'viewId', type: 'text', required: true },
        { name: 'label', type: 'text', required: true, ...loc },
        { name: 'en', type: 'text' },
        { name: 'file', type: 'text', admin: { description: 'Built-in asset path (do not delete).' } },
        { name: 'image', type: 'upload', relationTo: 'media', admin: { description: 'Optional override.' } },
      ],
    },
    {
      name: 'details', type: 'array', admin: { description: 'Manufacturing detail shots.' },
      fields: [
        { name: 'detailId', type: 'text', required: true },
        { name: 'label', type: 'text', required: true, ...loc },
        { name: 'en', type: 'text' },
        { name: 'desc', type: 'textarea', ...loc },
        { name: 'file', type: 'text' },
        { name: 'image', type: 'upload', relationTo: 'media' },
      ],
    },
    {
      name: 'colorways', type: 'array',
      fields: [
        { name: 'colorId', type: 'text', required: true },
        { name: 'name', type: 'text', required: true, ...loc },
        { name: 'hex', type: 'text' },
        { name: 'file', type: 'text' },
        { name: 'image', type: 'upload', relationTo: 'media' },
      ],
    },
  ],
}
