#!/bin/bash

echo "🧪 Testing NPCI QR Validation Request (ReqValQr) Compliance"
echo "=========================================================="

# Test 1: Valid NPCI ReqValQr with all mandatory fields
echo "📋 Test 1: Valid NPCI ReqValQr Request"
echo "--------------------------------------"

valid_request='<?xml version="1.0" encoding="UTF-8"?>
<upi:ReqValQr xmlns:upi="http://npci.org/upi/schema/">
  <Head ver="2.0" ts="2025-01-15T10:30:00+05:30" orgId="NPCI001" msgId="MSG123456789ABCDEF01234"/>
  <Txn id="TXN987654321" note="QR Validation" refId="REF001" refUrl="" ts="2025-01-15T10:30:00+05:30" type="IntlQr" initiationMode="QR" purpose="00" custRef="123456789012"/>
  <Payer addr="customer@paytm" name="John Doe" seqNum="1" type="PERSON" code="0000">
    <Institution QrPayLoad="upi://pay?pa=merchant123@mercury&pn=Test%20Merchant&am=100.00&cu=INR&mc=5411" netInstId="MERCURY001" baseCurr="SGD"/>
  </Payer>
</upi:ReqValQr>'

echo "Request XML:"
echo "$valid_request"
echo ""

response=$(curl -s -X POST "http://localhost:4000/api/v1/upi/validate-qr" \
  -H "Content-Type: application/xml" \
  -d "$valid_request")

echo "Response:"
echo "$response"
echo ""

# Test 2: Invalid request - missing mandatory fields
echo "📋 Test 2: Invalid Request - Missing QrPayLoad"
echo "----------------------------------------------"

invalid_request='<?xml version="1.0" encoding="UTF-8"?>
<upi:ReqValQr xmlns:upi="http://npci.org/upi/schema/">
  <Head ver="2.0" ts="2025-01-15T10:30:00+05:30" orgId="NPCI001" msgId="MSG123456789ABCDEF01234"/>
  <Txn id="TXN987654321" note="QR Validation" type="IntlQr" initiationMode="QR" purpose="00" custRef="123456789012"/>
  <Payer addr="customer@paytm" name="John Doe" seqNum="1" type="PERSON" code="0000">
    <Institution netInstId="MERCURY001" baseCurr="SGD"/>
  </Payer>
</upi:ReqValQr>'

echo "Request XML:"
echo "$invalid_request"
echo ""

response=$(curl -s -X POST "http://localhost:4000/api/v1/upi/validate-qr" \
  -H "Content-Type: application/xml" \
  -d "$invalid_request")

echo "Response:"
echo "$response"
echo ""

# Test 3: Invalid transaction type
echo "📋 Test 3: Invalid Transaction Type"
echo "-----------------------------------"

wrong_type_request='<?xml version="1.0" encoding="UTF-8"?>
<upi:ReqValQr xmlns:upi="http://npci.org/upi/schema/">
  <Head ver="2.0" ts="2025-01-15T10:30:00+05:30" orgId="NPCI001" msgId="MSG123456789ABCDEF01234"/>
  <Txn id="TXN987654321" note="QR Validation" type="PAY" initiationMode="QR" purpose="00" custRef="123456789012"/>
  <Payer addr="customer@paytm" name="John Doe" seqNum="1" type="PERSON" code="0000">
    <Institution QrPayLoad="upi://pay?pa=merchant123@mercury&pn=Test%20Merchant&am=100.00&cu=INR&mc=5411" netInstId="MERCURY001" baseCurr="SGD"/>
  </Payer>
</upi:ReqValQr>'

echo "Request XML:"
echo "$wrong_type_request"
echo ""

response=$(curl -s -X POST "http://localhost:4000/api/v1/upi/validate-qr" \
  -H "Content-Type: application/xml" \
  -d "$wrong_type_request")

echo "Response:"
echo "$response"
echo ""

# Test 4: International QR with different currencies
echo "📋 Test 4: SGD to INR International QR"
echo "--------------------------------------"

sgd_request='<?xml version="1.0" encoding="UTF-8"?>
<upi:ReqValQr xmlns:upi="http://npci.org/upi/schema/">
  <Head ver="2.0" ts="2025-01-15T10:30:00+05:30" orgId="NPCI001" msgId="MSG123456789ABCDEF01234"/>
  <Txn id="TXN987654321" note="International QR Validation" refId="REF002" refUrl="" ts="2025-01-15T10:30:00+05:30" type="IntlQr" initiationMode="QR" purpose="00" custRef="123456789012"/>
  <Payer addr="customer@dbs" name="Jane Smith" seqNum="1" type="PERSON" code="0000">
    <Institution QrPayLoad="upi://pay?pa=merchant456@mercury&pn=Singapore%20Restaurant&am=50.00&cu=SGD&mc=5812" netInstId="MERCURY001" baseCurr="SGD"/>
  </Payer>
</upi:ReqValQr>'

echo "Request XML:"
echo "$sgd_request"
echo ""

response=$(curl -s -X POST "http://localhost:4000/api/v1/upi/validate-qr" \
  -H "Content-Type: application/xml" \
  -d "$sgd_request")

echo "Response:"
echo "$response"
echo ""

echo "✅ NPCI QR Validation Tests Completed"
echo "====================================="
