/**
 * Seed Community Rules
 *
 * Erstellt Beispiel-Regeln für das Community Management System.
 *
 * Usage: npx tsx scripts/seed-community-rules.ts
 */

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

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

  const rules = [
    {
      name: 'Medizinische Fragen → Priorität hoch',
      description: 'Erkennt medizinische Fragen anhand von Keywords und setzt hohe Priorität',
      priority: 10,
      isActive: true,
      trigger: {
        type: 'keyword',
        keywords: [
          { keyword: 'arzt', matchType: 'contains' },
          { keyword: 'krankheit', matchType: 'contains' },
          { keyword: 'nebenwirkung', matchType: 'contains' },
          { keyword: 'medikament', matchType: 'contains' },
          { keyword: 'schmerzen', matchType: 'contains' },
          { keyword: 'symptom', matchType: 'contains' },
          { keyword: 'diagnose', matchType: 'contains' },
          { keyword: 'behandlung', matchType: 'contains' },
        ],
      },
      actions: [
        { action: 'set_priority', value: 'high' },
        { action: 'flag_medical' },
      ],
    },
    {
      name: 'Negative Kommentare → Eskalation',
      description: 'Eskaliert negative Kommentare automatisch',
      priority: 20,
      isActive: true,
      trigger: {
        type: 'sentiment',
        sentimentValues: ['negative'],
      },
      actions: [
        { action: 'set_priority', value: 'high' },
        { action: 'escalate' },
      ],
    },
    {
      name: 'Influencer → Priorität urgent',
      description: 'Priorisiert Kommentare von Accounts mit 10k+ Followern',
      priority: 5,
      isActive: true,
      trigger: {
        type: 'influencer',
        influencerMinFollowers: 10000,
      },
      actions: [{ action: 'set_priority', value: 'urgent' }],
    },
    {
      name: 'Spam-Erkennung',
      description: 'Markiert verdächtige Kommentare als Spam',
      priority: 1,
      isActive: true,
      trigger: {
        type: 'keyword',
        keywords: [
          { keyword: 'gratis gewinn', matchType: 'contains' },
          { keyword: 'gewonnen', matchType: 'contains' },
          { keyword: 'link in bio', matchType: 'contains' },
          { keyword: 'check my profile', matchType: 'contains' },
          { keyword: 'dm for', matchType: 'contains' },
          { keyword: 'crypto', matchType: 'contains' },
          { keyword: 'bitcoin', matchType: 'contains' },
          { keyword: 'nft', matchType: 'contains' },
        ],
      },
      actions: [{ action: 'mark_spam' }],
    },
    {
      name: 'Support-Anfragen',
      description: 'Erkennt Support-Anfragen und setzt normale Priorität',
      priority: 50,
      isActive: true,
      trigger: {
        type: 'keyword',
        keywords: [
          { keyword: 'hilfe', matchType: 'contains' },
          { keyword: 'problem', matchType: 'contains' },
          { keyword: 'funktioniert nicht', matchType: 'contains' },
          { keyword: 'fehler', matchType: 'contains' },
          { keyword: 'wie kann ich', matchType: 'contains' },
          { keyword: 'frage', matchType: 'contains' },
        ],
      },
      actions: [{ action: 'set_priority', value: 'normal' }],
    },
    {
      name: 'Positive Feedback',
      description: 'Markiert positives Feedback für spätere Testimonials',
      priority: 100,
      isActive: true,
      trigger: {
        type: 'sentiment',
        sentimentValues: ['positive'],
      },
      actions: [{ action: 'set_priority', value: 'low' }],
    },
  ]

  console.log('🔧 Creating community rules...\n')

  let created = 0
  let failed = 0

  for (const rule of rules) {
    try {
      // Prüfen ob Regel bereits existiert
      const existing = await payload.find({
        collection: 'community-rules',
        where: { name: { equals: rule.name } },
        limit: 1,
      })

      if (existing.docs.length > 0) {
        console.log(`⏭️  Skipped (exists): ${rule.name}`)
        continue
      }

      await payload.create({
        collection: 'community-rules',
        data: rule as any,
      })
      console.log(`✅ Created: ${rule.name}`)
      created++
    } catch (error: unknown) {
      const msg = error instanceof Error ? error.message : 'Unknown error'
      console.error(`❌ Failed: ${rule.name} - ${msg}`)
      failed++
    }
  }

  console.log('\n' + '='.repeat(50))
  console.log(`📊 Summary: ${created} created, ${failed} failed`)
  console.log('='.repeat(50))
  console.log('\n👉 Check: /admin/collections/community-rules\n')

  process.exit(0)
}

seedRules().catch((err) => {
  console.error('Fatal error:', err)
  process.exit(1)
})
