/**
 * BlogWoman Coming Soon Page Seed Script
 *
 * Creates a Coming Soon page for the BlogWoman tenant
 *
 * Run with: npx tsx scripts/seed-blogwoman-coming-soon.ts
 */

import { getPayload } from 'payload'
import config from '../src/payload.config'

// Helper to create Lexical Rich Text content
function createRichText(content: string | string[]): object {
  const paragraphs = Array.isArray(content) ? content : [content]
  return {
    root: {
      type: 'root',
      children: paragraphs.map((text) => ({
        type: 'paragraph',
        children: [{ type: 'text', text }],
      })),
      direction: 'ltr',
      format: '',
      indent: 0,
      version: 1,
    },
  }
}

async function seedComingSoonPage() {
  console.log('🚀 Starting BlogWoman Coming Soon Page Seed...\n')

  const payload = await getPayload({ config })

  // Find BlogWoman tenant
  const tenantResult = await payload.find({
    collection: 'tenants',
    where: { slug: { equals: 'blogwoman' } },
  })

  if (tenantResult.docs.length === 0) {
    console.error('❌ BlogWoman tenant not found! Run seed-blogwoman.ts first.')
    process.exit(1)
  }

  const tenantId = tenantResult.docs[0].id as number
  console.log(`✓ Found BlogWoman tenant (ID: ${tenantId})`)

  // Coming Soon Page Definition
  const comingSoonPage = {
    title: 'Coming Soon',
    slug: 'coming-soon',
    status: 'published',
    hero: {
      headline: 'BlogWoman kommt bald',
      subline: 'Für Frauen, die Karriere, Familie & Stil ernst nehmen.',
    },
    layout: [
      // Hero Block - Main Coming Soon Message
      {
        blockType: 'hero-block',
        headline: 'BlogWoman',
        subline: 'Kommt bald.',
        alignment: 'center',
        overlay: true,
        overlayOpacity: 0.5,
        textColor: 'light',
      },
      // Text Block - Teaser
      {
        blockType: 'text-block',
        width: 'medium',
        content: createRichText([
          'Systeme statt Motivation.',
          'Für Frauen, die Karriere, Familie und Stil ernst nehmen.',
          '',
          'BlogWoman ist das Projekt von Dr. Caroline Porwoll – Unternehmerin, Mutter und Systemdenkerin.',
          '',
          'Hier entstehen praktische Systeme für alle, die mehr wollen als Work-Life-Balance-Sprüche.',
        ]),
      },
      // Stats Block - Launch Date / Countdown Placeholder
      {
        blockType: 'stats-block',
        headline: 'Was dich erwartet',
        stats: [
          {
            value: '9',
            label: 'YouTube-Serien',
            description: 'GRFI, Investment-Piece, Regeneration & mehr',
          },
          {
            value: '4',
            label: 'Content-Säulen',
            description: 'Stil, Systeme, Energie, Karriere×Familie',
          },
          {
            value: '∞',
            label: 'Praktische Tipps',
            description: 'Keine Theorie. Nur was funktioniert.',
          },
        ],
        columns: 3,
        backgroundColor: 'light',
      },
      // Text Block - What's Coming
      {
        blockType: 'text-block',
        width: 'medium',
        content: {
          root: {
            type: 'root',
            children: [
              {
                type: 'heading',
                tag: 'h2',
                children: [{ type: 'text', text: 'Die BlogWoman Serien' }],
              },
              {
                type: 'list',
                listType: 'bullet',
                children: [
                  {
                    type: 'listitem',
                    children: [
                      { type: 'text', text: 'GRFI – Get Ready For Impact', format: 1 },
                      { type: 'text', text: ' – In 7-10 Minuten vom Alltag zur Präsenz' },
                    ],
                  },
                  {
                    type: 'listitem',
                    children: [
                      { type: 'text', text: 'Investment-Piece', format: 1 },
                      { type: 'text', text: ' – 1 Teil, 3 Looks, 1 ehrliche Rechnung' },
                    ],
                  },
                  {
                    type: 'listitem',
                    children: [
                      { type: 'text', text: 'Pleasure P&L', format: 1 },
                      { type: 'text', text: ' – Gönnen mit ROI' },
                    ],
                  },
                  {
                    type: 'listitem',
                    children: [
                      { type: 'text', text: 'Regeneration', format: 1 },
                      { type: 'text', text: ' – 20-Minuten-Resets für Körper und Kopf' },
                    ],
                  },
                  {
                    type: 'listitem',
                    children: [
                      { type: 'text', text: 'Backstage', format: 1 },
                      { type: 'text', text: ' – Ein Tag, real. Keine Filter.' },
                    ],
                  },
                ],
              },
            ],
            direction: 'ltr',
            format: '',
            indent: 0,
            version: 1,
          },
        },
      },
      // Newsletter Block - Stay Updated
      {
        blockType: 'newsletter-block',
        headline: 'Sei dabei, wenn es losgeht',
        description:
          'Trag dich in den Newsletter ein und erfahre als Erste, wenn BlogWoman startet. Plus: Exklusive Launch-Inhalte nur für Abonnentinnen.',
        buttonText: 'BENACHRICHTIGEN',
        privacyText: 'Kein Spam. Jederzeit abmelden. Datenschutz beachten.',
        source: 'coming-soon',
      },
      // Divider
      {
        blockType: 'divider-block',
        style: 'line',
        spacing: 'medium',
      },
      // Text Block - About Caroline (Teaser)
      {
        blockType: 'text-block',
        width: 'narrow',
        content: {
          root: {
            type: 'root',
            children: [
              {
                type: 'heading',
                tag: 'h3',
                children: [{ type: 'text', text: 'Die Frau hinter BlogWoman' }],
              },
              {
                type: 'paragraph',
                children: [
                  {
                    type: 'text',
                    text: 'Dr. Caroline Porwoll ist promovierte Wirtschaftswissenschaftlerin, Geschäftsführerin der Complex Care Solutions GmbH und Mutter.',
                  },
                ],
              },
              {
                type: 'paragraph',
                children: [
                  {
                    type: 'text',
                    text: '„Ich glaube nicht an Work-Life-Balance – ich glaube an Systeme, die beides möglich machen."',
                    format: 2, // italic
                  },
                ],
              },
            ],
            direction: 'ltr',
            format: '',
            indent: 0,
            version: 1,
          },
        },
      },
      // CTA Block - Social Links
      {
        blockType: 'cta-block',
        headline: 'Folge BlogWoman',
        description: 'Auf YouTube und Instagram startet der Content noch vor dem Website-Launch.',
        backgroundColor: 'dark',
        buttons: [
          {
            text: 'YouTube abonnieren',
            link: 'https://youtube.com/@blogwoman',
            style: 'primary',
          },
          {
            text: 'Instagram folgen',
            link: 'https://instagram.com/blogwoman.de',
            style: 'secondary',
          },
        ],
      },
    ],
    seo: {
      metaTitle: 'BlogWoman – Coming Soon | Karriere, Familie & Stil mit System',
      metaDescription:
        'BlogWoman kommt bald. Für Frauen, die Karriere, Familie und Stil ernst nehmen. Systeme statt Motivation. Von Dr. Caroline Porwoll.',
    },
  }

  // Check if page already exists
  const existingPage = await payload.find({
    collection: 'pages',
    where: {
      and: [{ slug: { equals: 'coming-soon' } }, { tenant: { equals: tenantId } }],
    },
  })

  if (existingPage.docs.length > 0) {
    // Update existing page
    await payload.update({
      collection: 'pages',
      id: existingPage.docs[0].id,
      data: {
        ...comingSoonPage,
        tenant: tenantId,
      } as any,
    })
    console.log(`✓ Updated Coming Soon page (ID: ${existingPage.docs[0].id})`)
  } else {
    // Create new page
    const createdPage = await payload.create({
      collection: 'pages',
      data: {
        ...comingSoonPage,
        tenant: tenantId,
      } as any,
    })
    console.log(`✓ Created Coming Soon page (ID: ${createdPage.id})`)
  }

  console.log('\n========================================')
  console.log('✅ Coming Soon Page Seed completed!')
  console.log('========================================')
  console.log(`
Page URL: /coming-soon
Tenant: BlogWoman (ID: ${tenantId})

Blocks used:
- HeroBlock (main coming soon message)
- TextBlock (teaser content)
- StatsBlock (what's coming preview)
- TextBlock (series preview list)
- NewsletterBlock (email signup)
- DividerBlock
- TextBlock (about Caroline teaser)
- CTABlock (social media links)
`)

  process.exit(0)
}

seedComingSoonPage().catch((error) => {
  console.error('❌ Seed failed:', error)
  process.exit(1)
})
