#!/usr/bin/env node

const API_URL = 'http://localhost:8000/createtransaksi'
const API_KEY = 'dev-key'

const orderId = `SNAPTEST-${Date.now()}`

const payload = {
  mercant_id: 'TESTMERCHANT',
  timestamp: Date.now(),
  deskripsi: 'Testing Snap Payment Mode',
  nominal: 150000,
  nama: 'Test Snap User',
  no_telepon: '081234567890',
  email: 'test@snap.com',
  item: [
    {
      item_id: orderId,
      nama: 'Test Product Snap',
      harga: 150000,
      qty: 1
    }
  ]
}

try {
  const response = await fetch(API_URL, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'x-api-key': API_KEY
    },
    body: JSON.stringify(payload)
  })

  if (!response.ok) {
    const error = await response.text()
    console.error('❌ Error:', response.status, error)
    process.exit(1)
  }

  const data = await response.json()
  
  const paymentUrl = data.data?.url || data.payment_url
  const token = paymentUrl ? paymentUrl.split('/pay/')[1] : null
  
  console.log('✅ Payment link created successfully!')
  console.log('\n🔗 Snap Mode Payment Link:')
  console.log(paymentUrl.replace('https://midtrans-cifo.winteraccess.id', 'http://localhost:5173'))
  console.log('\n📋 Order ID:', orderId)
  console.log('💰 Amount: Rp 150,000')
  console.log('🔑 Mode: SNAP (Hosted UI)')
  
  if (token) {
    console.log('\n📄 Token:', token.substring(0, 50) + '...')
  }
  
} catch (error) {
  console.error('❌ Failed to create payment link:', error.message)
  process.exit(1)
}
