#!/usr/bin/env tsx
/**
 * Test Payload Post Creation with Lexical Content
 */

import { getPayload } from 'payload'
import config from '@payload-config'

async function testPostCreation() {
  const payload = await getPayload({ config })

  // Test 1: Simple string content
  console.log('\n🧪 Test 1: String content')
  try {
    const result1 = await payload.create({
      collection: 'posts',
      data: {
        title: 'Test String Content',
        slug: 'test-string-' + Date.now(),
        content: '<p>Simple HTML content</p>',
        publishedAt: new Date(),
        status: 'draft',
      },
    })
    console.log('✓ String content worked:', result1.id)
    await payload.delete({
      collection: 'posts',
      id: result1.id,
    })
  } catch (error: any) {
    console.log('✗ String content failed:', error.message)
  }

  // Test 2: JSON string content
  console.log('\n🧪 Test 2: JSON string content')
  try {
    const result2 = await payload.create({
      collection: 'posts',
      data: {
        title: 'Test JSON Content',
        slug: 'test-json-' + Date.now(),
        content: JSON.stringify({
          type: 'root',
          version: 1,
          children: [{
            type: 'paragraph',
            version: 1,
            children: [{
              type: 'text',
              version: 1,
              text: 'This is a test paragraph.'
            }]
          }],
          direction: null
        }),
        publishedAt: new Date(),
        status: 'draft',
      },
    })
    console.log('✓ JSON string content worked:', result2.id)
    await payload.delete({
      collection: 'posts',
      id: result2.id,
    })
  } catch (error: any) {
    console.log('✗ JSON string content failed:', error.message)
    if (error.data) {
      console.error('  Validation errors:', JSON.stringify(error.data, null, 2))
    }
  }

  // Test 3: Object content
  console.log('\n🧪 Test 3: Object content')
  try {
    const result3 = await payload.create({
      collection: 'posts',
      data: {
        title: 'Test Object Content',
        slug: 'test-object-' + Date.now(),
        content: {
          type: 'root',
          version: 1,
          children: [{
            type: 'paragraph',
            version: 1,
            children: [{
              type: 'text',
              version: 1,
              text: 'This is a test paragraph.'
            }]
          }],
          direction: null
        },
        publishedAt: new Date(),
        status: 'draft',
      },
    })
    console.log('✓ Object content worked:', result3.id)
    await payload.delete({
      collection: 'posts',
      id: result3.id,
    })
  } catch (error: any) {
    console.log('✗ Object content failed:', error.message)
    if (error.data) {
      console.error('  Validation errors:', JSON.stringify(error.data, null, 2))
    }
  }
}

testPostCreation().catch(console.error)
