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, // Enables Draft/Published workflows
  },
  access: {
    read: ({ req: { user } }) => {
      // In local development, always show everything (including Drafts and scheduled future posts)
      if (process.env.NODE_ENV === 'development') {
        return true;
      }

      // If an Admin user is logged in, they can view everything
      if (user) {
        return true;
      }

      // For public unauthenticated visitors in PROD/STAGING contexts:
      // Only serve Posts where Status = "published" AND the publish Date is in the past!
      return {
        and: [
          {
            _status: {
              equals: 'published',
            },
          },
          {
            date: {
              less_than_equal: new Date().toISOString(),
            },
          },
        ],
      };
    },
  },
  fields: [
    {
      name: 'title',
      type: 'text',
      required: true,
    },
    {
      name: 'slug',
      type: 'text',
      required: true,
      unique: true,
      admin: {
        position: 'sidebar',
      },
      hooks: {
        beforeValidate: [
          ({ value, data }) => {
            // Auto-generate slug from title if left blank
            if (value || !data?.title) return value;
            return data.title
              .toLowerCase()
              .replace(/ /g, '-')
              .replace(/[^\w-]+/g, '');
          },
        ],
      },
    },
    {
      name: 'excerpt',
      type: 'text',
      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: 'locale',
      type: 'select',
      required: true,
      admin: {
        position: 'sidebar',
      },
      options: [
        { label: 'English', value: 'en' },
        { label: 'German', value: 'de' },
      ],
      defaultValue: 'en',
    },
    {
      name: 'category',
      type: 'text',
      admin: {
        position: 'sidebar',
        description: 'Used for tag bucketing (e.g. "Kabel Technologie").',
      },
    },
    {
      name: 'content',
      type: 'richText',
      editor: lexicalEditor({
        features: ({ defaultFeatures }) => [
          ...defaultFeatures,
          BlocksFeature({
            blocks: [
              StickyNarrative,
              ComparisonGrid,
              VisualLinkPreview,
              TechnicalGrid,
              HighlightBox,
              AnimatedImage,
              ChatBubble,
              PowerCTA,
              Callout,
              Stats,
              SplitHeading,
            ],
          }),
        ],
      }),
    },
  ],
};
