const BASE_URL = 'http://localhost:3010'

async function testPrayerAPI() {
  console.log('🧪 Testing Prayer API Endpoints...\n')

  // Test 1: Get all prayers
  console.log('📋 Test 1: Fetching all prayers...')
  try {
    const response = await fetch(`${BASE_URL}/api/prayers?limit=10`)
    const data = await response.json()
    console.log(`✅ Success: Retrieved ${data.prayers?.length || 0} prayers`)
    console.log(`   First prayer: ${data.prayers?.[0]?.title || 'N/A'}\n`)
  } catch (error) {
    console.log(`❌ Error fetching prayers: ${error}\n`)
  }

  // Test 2: Get prayers by category
  console.log('📋 Test 2: Fetching prayers by category (health)...')
  try {
    const response = await fetch(`${BASE_URL}/api/prayers?category=health&limit=5`)
    const data = await response.json()
    console.log(`✅ Success: Retrieved ${data.prayers?.length || 0} health prayers\n`)
  } catch (error) {
    console.log(`❌ Error fetching category prayers: ${error}\n`)
  }

  // Test 3: Create a new prayer
  console.log('📋 Test 3: Creating a new prayer request...')
  try {
    const newPrayer = {
      title: 'Test Prayer Request',
      description: 'This is a test prayer request created by the testing script.',
      category: 'personal',
      isAnonymous: false
    }

    const response = await fetch(`${BASE_URL}/api/prayers`, {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify(newPrayer)
    })

    const data = await response.json()
    if (response.ok) {
      console.log(`✅ Success: Created prayer with ID: ${data.prayer?.id}`)
      console.log(`   Title: ${data.prayer?.title}\n`)
      return data.prayer?.id // Return ID for next test
    } else {
      console.log(`❌ Error: ${data.error}\n`)
    }
  } catch (error) {
    console.log(`❌ Error creating prayer: ${error}\n`)
  }

  // Test 4: Update prayer count (pray for a prayer)
  console.log('📋 Test 4: Testing prayer count update...')
  try {
    // Get first prayer ID
    const getResponse = await fetch(`${BASE_URL}/api/prayers?limit=1`)
    const getData = await getResponse.json()
    const prayerId = getData.prayers?.[0]?.id

    if (prayerId) {
      const response = await fetch(`${BASE_URL}/api/prayers/${prayerId}/pray`, {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' }
      })

      const data = await response.json()
      if (response.ok) {
        console.log(`✅ Success: Updated prayer count`)
        console.log(`   New count: ${data.prayerCount}\n`)
      } else {
        console.log(`❌ Error: ${data.error}\n`)
      }
    } else {
      console.log('❌ No prayer found to test with\n')
    }
  } catch (error) {
    console.log(`❌ Error updating prayer count: ${error}\n`)
  }

  // Test 5: Generate AI prayer
  console.log('📋 Test 5: Testing AI prayer generation...')
  try {
    const aiRequest = {
      prompt: 'I need strength to overcome my anxiety about the future',
      category: 'personal',
      locale: 'en'
    }

    const response = await fetch(`${BASE_URL}/api/prayers/generate`, {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify(aiRequest)
    })

    const data = await response.json()
    if (response.ok) {
      console.log(`✅ Success: Generated AI prayer`)
      console.log(`   Title: ${data.title}`)
      console.log(`   Prayer preview: ${data.prayer?.substring(0, 100)}...\n`)
    } else {
      console.log(`❌ Error: ${data.error}\n`)
    }
  } catch (error) {
    console.log(`❌ Error generating AI prayer: ${error}\n`)
  }

  // Test 6: Generate Romanian AI prayer
  console.log('📋 Test 6: Testing Romanian AI prayer generation...')
  try {
    const aiRequest = {
      prompt: 'Am nevoie de înțelepciune pentru o decizie importantă',
      category: 'personal',
      locale: 'ro'
    }

    const response = await fetch(`${BASE_URL}/api/prayers/generate`, {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify(aiRequest)
    })

    const data = await response.json()
    if (response.ok) {
      console.log(`✅ Success: Generated Romanian AI prayer`)
      console.log(`   Title: ${data.title}`)
      console.log(`   Prayer preview: ${data.prayer?.substring(0, 100)}...\n`)
    } else {
      console.log(`❌ Error: ${data.error}\n`)
    }
  } catch (error) {
    console.log(`❌ Error generating Romanian AI prayer: ${error}\n`)
  }

  console.log('✨ Prayer API testing completed!')
}

// Run tests
testPrayerAPI().catch(console.error)