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';

export const Posts: CollectionConfig = {
  slug: 'posts',
  admin: {
    defaultColumns: ['featuredImage', 'title', 'date', '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 {
        and: [
          {
            _status: {
              equals: 'published',
            },
          },
          {
            date: {
              less_than_equal: new Date().toISOString(),
            },
          },
        ],
      };
    },
  },
  fields: [
    {
      name: 'title',
      type: 'text',
      required: true,
      localized: true,
    },
    {
      name: 'slug',
      type: 'text',
      required: true,
      localized: true,
      admin: {
        position: 'sidebar',
        description: 'Unique slug per locale (e.g. same slug can exist in DE and EN).',
      },
      hooks: {
        beforeValidate: [
          ({ value, data }) => {
            if (value || !data?.title) return value;
            return data.title
              .toLowerCase()
              .replace(/ /g, '-')
              .replace(/[^\w-]+/g, '');
          },
        ],
      },
    },
    {
      name: 'excerpt',
      type: 'text',
      localized: true,
      admin: {
        description: 'A short summary for blog feed cards and SEO.',
      },
    },
    {
      name: 'date',
      type: 'date',
      required: true,
      admin: {
        position: 'sidebar',
        description: 'Future dates will schedule the post to publish automatically.',
      },
      defaultValue: () => new Date().toISOString(),
    },
    {
      name: 'featuredImage',
      type: 'upload',
      relationTo: 'media',
      admin: {
        position: 'sidebar',
        description: 'The primary Hero image used for headers and OpenGraph previews.',
      },
    },
    {
      name: 'category',
      type: 'text',
      localized: true,
      admin: {
        position: 'sidebar',
        description: 'Used for tag bucketing (e.g. "Kabel Technologie").',
      },
    },
    {
      name: 'content',
      type: 'richText',
      localized: true,
      editor: lexicalEditor({
        features: ({ defaultFeatures }) => [
          ...defaultFeatures,
          BlocksFeature({
            blocks: [
              StickyNarrative,
              ComparisonGrid,
              VisualLinkPreview,
              TechnicalGrid,
              HighlightBox,
              AnimatedImage,
              ChatBubble,
              PowerCTA,
              Callout,
              Stats,
              SplitHeading,
            ],
          }),
        ],
      }),
    },
  ],
};
