import axios from "axios";
import { v4 as uuidv4 } from "uuid";

/**
 * MANUAL MERCHANT WEBHOOK SENDER
 * Use this script to manually test a merchant's webhook endpoint.
 * 
 * Usage:
 * 1. Update the configuration below.
 * 2. Run: node tests/manual_merchant_webhook.js
 */

const CONFIG = {
  // --- Targeted Webhook URL ---
  webhookUrl: "https://digitalpivotltd.onrender.com/waas/airpero/webhook", 

  // --- Event Type ---
  event: "PAYIN_SUCCESS",

  // --- The Data Payload ---
  data: {
    transactionId: "100004260402085201156174739776", // Using SessionID as requested in earlier logic
    customerId: "CUST-VA-17be8eaf", // Using reference as ID for test
    customerName: "DESTINY MOMODU",
    amount: 10000.00,
    fee: 50.00,
    total_amount: 10000.00,
    currency: "NGN",
    reference: "CUST-VA-17be8eaf",
    timestamp: "2026-04-02T09:52:15"
  }
};

async function sendManualWebhook() {
  const { webhookUrl, event, data } = CONFIG;

  const payload = {
    event,
    merchantId: "manual-test-merchant", // Placeholder or real ID
    timestamp: new Date().toISOString(),
    data,
  };

  try {
    console.log(`🚀 Sending [${event}] notification to: ${webhookUrl}`);
    console.log(`📦 Payload:`, JSON.stringify(payload, null, 2));

    const response = await axios.post(webhookUrl, payload, {
      headers: {
        "Content-Type": "application/json",
        "X-Airpero-Event": event,
      },
      timeout: 15000, // 15s timeout for manual tests
    });

    console.log("-----------------------------------------");
    console.log(`✅ SUCCESS: Webhook delivered!`);
    console.log(`📄 Response Status: HTTP ${response.status}`);
    console.log(`📄 Response Body:`, response.data);
    console.log("-----------------------------------------");

  } catch (error) {
    console.error("-----------------------------------------");
    console.error(`❌ FAILED: Webhook delivery failed!`);
    if (error.response) {
      console.error(`Status: HTTP ${error.response.status}`);
      console.error(`Data:`, error.response.data);
    } else {
      console.error(`Error Message: ${error.message}`);
    }
    console.error("-----------------------------------------");
  }
}

// Execute
sendManualWebhook();
