import type { CollectionConfig } from 'payload';
import { lexicalEditor, BlocksFeature } from '@payloadcms/richtext-lexical';

import { StickyNarrative } from '../blocks/StickyNarrative';
import { ComparisonGrid } from '../blocks/ComparisonGrid';
import { VisualLinkPreview } from '../blocks/VisualLinkPreview';
import { TechnicalGrid } from '../blocks/TechnicalGrid';
import { HighlightBox } from '../blocks/HighlightBox';
import { AnimatedImage } from '../blocks/AnimatedImage';
import { ChatBubble } from '../blocks/ChatBubble';
import { PowerCTA } from '../blocks/PowerCTA';
import { Callout } from '../blocks/Callout';
import { Stats } from '../blocks/Stats';
import { SplitHeading } from '../blocks/SplitHeading';
import { ProductTabs } from '../blocks/ProductTabs';

export const Products: CollectionConfig = {
  slug: 'products',
  admin: {
    defaultColumns: ['featuredImage', 'title', 'sku', 'updatedAt', '_status'],
  },
  versions: {
    drafts: true,
  },
  access: {
    read: ({ req: { user } }) => {
      if (process.env.NODE_ENV === 'development' || process.env.TARGET === 'staging') {
        return true;
      }
      if (user) {
        return true;
      }
      return {
        _status: {
          equals: 'published',
        },
      };
    },
  },
  fields: [
    {
      name: 'title',
      type: 'text',
      required: true,
      localized: true,
    },
    {
      name: 'sku',
      type: 'text',
      required: true,
      admin: {
        position: 'sidebar',
      },
    },
    {
      // slug is shared: the cable name (e.g. "n2xy") is the same in DE and EN
      name: 'slug',
      type: 'text',
      required: true,
      admin: {
        position: 'sidebar',
      },
    },
    {
      name: 'description',
      type: 'textarea',
      required: true,
      localized: true,
    },
    {
      name: 'categories',
      type: 'array',
      required: true,
      fields: [
        {
          name: 'category',
          type: 'text',
        },
      ],
      admin: {
        position: 'sidebar',
      },
    },
    {
      name: 'featuredImage',
      type: 'upload',
      relationTo: 'media',
      admin: {
        position: 'sidebar',
        description: 'The primary thumbnail used in list views.',
      },
    },
    {
      name: 'images',
      type: 'upload',
      relationTo: 'media',
      hasMany: true,
      admin: {
        position: 'sidebar',
      },
    },
    {
      name: 'application',
      type: 'richText',
      localized: true,
      editor: lexicalEditor({}),
    },
    {
      name: 'content',
      type: 'richText',
      localized: true,
      editor: lexicalEditor({
        features: ({ defaultFeatures }) => [
          ...defaultFeatures,
          BlocksFeature({
            blocks: [
              StickyNarrative,
              ComparisonGrid,
              VisualLinkPreview,
              TechnicalGrid,
              HighlightBox,
              AnimatedImage,
              ChatBubble,
              PowerCTA,
              Callout,
              Stats,
              SplitHeading,
              ProductTabs,
            ],
          }),
        ],
      }),
    },
  ],
};
