import dotenv from "dotenv";
import path from "path";
import { fileURLToPath } from "url";
import { dirname } from "path";

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

// Load .env from project root
dotenv.config({ path: path.join(__dirname, "../.env") });

import PsbService from "../src/thirdParties/psbService.js";

async function test() {
  try {
    // 1. Get banks to find OPay's bank code
    console.log("Fetching banks...");
    const banks = await PsbService.getBanks("AIRPERO");
    const opayBank = banks?.data?.banks?.find(b => b.name.toLowerCase().includes("opay") || b.name.toLowerCase().includes("paycom"));
    
    if (!opayBank) {
      console.log("OPay bank not found in list:", banks?.data?.banks?.map(b => b.name));
      // Fallback OPay bank code is usually 999992
    }
    
    const opayBankCode = opayBank ? opayBank.code : "999992";
    console.log(`Found OPay bank code: ${opayBankCode}`);

    const payload = {
      transaction: { reference: "TEST-PAYOUT-" + Date.now() },
      order: { amount: 100, currency: "NGN", description: "Test Payout to Sammy", country: "NG" },
      customer: {
        account: {
          number: "8140023959",
          bank: opayBankCode,
          name: "Samuel Ogunnaike",
          senderaccountnumber: "1100691259",
          sendername: "Airpero Test",
        }
      },
      merchant: { isFee: false, merchantFeeAccount: "", merchantFeeAmount: "" },
      transactionType: "OTHER_BANKS",
      narration: "Test Payout to Sammy",
    };
    
    console.log("Calling walletOtherBanks with payload:", JSON.stringify(payload, null, 2));
    const response = await PsbService.walletOtherBanks(payload, "AIRPERO");
    console.log("Response SUCCESS:", JSON.stringify(response, null, 2));
  } catch (err) {
    console.error("Response ERROR:", JSON.stringify(err.response?.data || err.message, null, 2));
  }
}

test();
