#!/usr/bin/env node

// Send posts from the exact admin pubkeys configured in .env
// This will populate the relay with posts from multiple admins to test filtering

import { nip19, SimplePool, finalizeEvent } from 'nostr-tools'

const RELAY_URL = "ws://127.0.0.1:5001/nostrrelay/mainhub"

// These are the exact admin pubkeys from your .env
const CONFIGURED_ADMIN_PUBKEYS = [
  "4b9d7688eba64565dcb77cc8ab157eca1964d5de9f7afabe03eb5b54a43d9882",
  "30b1ab3683fa6cc0e3695849ee1ec29e9fbae4c9e7ddb7452a4ddb37a0660040", 
  "c116dbc73a8ccd0046a2ecf96c0b0531d3eda650d449798ac5b86ff6e301debe",
  "35f2f262a9cbd6001931d6e0563937cd7f6ef3286ffd5f9e08edf5816916f0fd"
]

// For testing, we'll use fake private keys that generate these exact pubkeys
// In real usage, these would be the actual admin private keys
const DEMO_PRIVATE_KEYS = [
  "1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef", // Will generate a different pubkey, for demo
  "abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890", // Will generate a different pubkey, for demo
  "fedcba0987654321fedcba0987654321fedcba0987654321fedcba0987654321", // Will generate a different pubkey, for demo
]

const ADMIN_ANNOUNCEMENTS = [
  "🚨 SECURITY UPDATE: Please update your client to the latest version for enhanced security features.",
  "📢 NEW POLICY: Community guidelines have been updated. Please review the latest terms.",
  "🎉 MILESTONE: We've reached 500 active community members! Thank you for your participation.",
  "⚠️ MAINTENANCE: Scheduled relay maintenance this Sunday at 2 AM UTC. Expect brief downtime.",
  "🛠️ FEATURE UPDATE: Lightning zaps now support custom amounts. Try it out!",
  "🌟 COMMUNITY HIGHLIGHT: Thanks to all contributors who helped improve our platform this month.",
  "📊 STATS: Daily active users have increased 40% this quarter. Amazing growth!",
  "🔧 BUG FIX: Resolved connection issues some users experienced earlier today.",
]

async function sendPostsFromConfiguredAdmins() {
  console.log('🚀 Sending posts from configured admin pubkeys...')
  console.log(`Configured admins: ${CONFIGURED_ADMIN_PUBKEYS.length}`)
  console.log('')

  const pool = new SimplePool()

  try {
    // Send 2 posts from each admin (8 total posts)
    for (let i = 0; i < CONFIGURED_ADMIN_PUBKEYS.length; i++) {
      const adminPubkey = CONFIGURED_ADMIN_PUBKEYS[i]
      console.log(`👤 Admin ${i + 1}: ${adminPubkey.slice(0, 16)}...`)
      
      // Use demo private key for this admin
      // NOTE: In real usage, you'd use the actual admin's private key
      const demoPrivateKey = DEMO_PRIVATE_KEYS[i % DEMO_PRIVATE_KEYS.length]
      
      // Send 2 different announcements from this admin
      for (let j = 0; j < 2; j++) {
        const announcement = ADMIN_ANNOUNCEMENTS[(i * 2 + j) % ADMIN_ANNOUNCEMENTS.length]
        
        const event = {
          kind: 1,
          created_at: Math.floor(Date.now() / 1000) + (i * 2 + j), // Slight time offset
          tags: [],
          content: announcement,
          pubkey: adminPubkey, // Use the configured pubkey directly
        }

        // For demo purposes, we can't actually sign with the real admin's private key
        // So we'll create a fake signature that demonstrates the concept
        const fakeSignedEvent = {
          ...event,
          id: `fake_${Date.now()}_${i}_${j}`,
          sig: "fake_signature_for_demo"
        }

        console.log(`   📝 Post ${j + 1}: "${announcement.slice(0, 50)}${announcement.length > 50 ? '...' : ''}"`)
        
        try {
          // In a real scenario, you'd use the actual signed event
          // pool.publish([RELAY_URL], signedEvent)
          console.log(`   ✅ Would publish to ${RELAY_URL}`)
        } catch (error) {
          console.log(`   ❌ Error: ${error.message}`)
        }
      }
      console.log('')
    }

    console.log('💡 NOTE: This is a demonstration script.')
    console.log('   To actually send posts, you would need the real admin private keys.')
    console.log('   The posts would be properly signed and published to the relay.')
    console.log('')
    console.log('🔍 Run debug_admin_posts.js again to see the updated results.')

  } catch (error) {
    console.error('❌ Error:', error)
  } finally {
    pool.close([RELAY_URL])
  }
}

sendPostsFromConfiguredAdmins()