import TransactionController from '../src/controllers/transactionController.js';

// Verification test suite for 9PSB Deposit Webhook Payload Formats

const samplePayloads = [
  {
    name: "Standard 9PSB Payload (Nested)",
    payload: {
      code: "00",
      customer: {
        account: { number: "1234567890", name: "Jane Doe" }
      },
      order: { amount: "5000", currency: "NGN" },
      transaction: { sessionid: "SESS_9PSB_001", reference: "REF_001" }
    }
  },
  {
    name: "9PSB Variation (Numeric Code & Top-level Account Number)",
    payload: {
      code: 0,
      accountNumber: "1234567890",
      amount: 10000,
      currency: "NGN",
      sessionid: "SESS_9PSB_002"
    }
  },
  {
    name: "9PSB Variation (responseCode & status: SUCCESS & destinationAccount)",
    payload: {
      responseCode: "200",
      status: "SUCCESS",
      destinationAccount: "1234567890",
      transactionAmount: "25000",
      reference: "REF_003"
    }
  }
];

console.log("=== Testing 9PSB Deposit Webhook Extractor ===");

for (const item of samplePayloads) {
  console.log(`\nTesting: ${item.name}`);
  const payload = item.payload;
  
  const rawCode = payload.code ?? payload.responseCode ?? payload.statusCode ?? payload.status ?? "";
  const codeString = rawCode.toString().trim().toUpperCase();
  const isSuccessCode = ["00", "0", "200", "SUCCESS", "SUCCESSFUL", "APPROVED"].includes(codeString);

  const accountNumber =
    payload.customer?.account?.number ||
    payload.customer?.accountNumber ||
    payload.accountNumber ||
    payload.destinationAccount ||
    payload.account_number ||
    payload.destinationAccountNumber ||
    payload.virtualAccountNumber;

  const rawAmount = payload.order?.amount ?? payload.amount ?? payload.transactionAmount ?? payload.orderAmount;
  const amount = parseFloat(rawAmount);

  const sessionId =
    payload.transaction?.sessionid ||
    payload.transaction?.id ||
    payload.sessionid ||
    payload.sessionId ||
    payload.reference ||
    payload.transactionReference ||
    payload.order?.reference;

  console.log(` - Is Success Code: ${isSuccessCode} (raw: ${rawCode})`);
  console.log(` - Extracted Account: ${accountNumber}`);
  console.log(` - Extracted Amount: ${amount}`);
  console.log(` - Extracted SessionID: ${sessionId}`);
  
  if (isSuccessCode && accountNumber && !isNaN(amount) && amount > 0 && sessionId) {
    console.log(" ✅ PARSE PASSED");
  } else {
    console.log(" ❌ PARSE FAILED");
  }
}
