#!/usr/bin/env bash
set -euo pipefail

# ====== CONFIG ======
VSDC_URL="http://devesdc.sandbox.vms.frcs.org.fj:8888/20f351f3-9b39-4c63-b9e0-d8a00b6e93fb/api/v3/invoices"        # V-SDC API URL from your cert/env
CERT_PFX="./LK2VRSH4-DeveloperAuthenticationCertificate.pfx"
PFX_PASSWORD="2CBP6HMW"

CASHIER="Software QA Engineer"
BUYER_ID="DT12345"
BUYER_CC="Data Tech"
POS_NO_PREFIX="DEMO-2025"
TZ_OFFSET="+12:00"   # Fiji time; adjust if needed

# Create log folder with timestamp
RUN_ID=$(date +"%Y%m%d-%H%M%S")
LOG_DIR="./logs/$RUN_ID"
mkdir -p "$LOG_DIR"

hdr=(-H "Accept: application/json" -H "Content-Type: application/json")

log_and_sign() {
  local name="$1"
  local json="$2"

  echo "$json" > "$LOG_DIR/${name}_request.json"

  curl -sS -X POST "$VSDC_URL" \
    --cert-type P12 --cert "${CERT_PFX}:${PFX_PASSWORD}" \
    "${hdr[@]}" \
    --data "$json" | tee "$LOG_DIR/${name}_response.json" >/dev/null
}

now() { date -u +"%Y-%m-%dT%H:%M:%S${TZ_OFFSET}"; }

pay() { printf '[{"amount": %s, "paymentType": "Cash"}]' "$1"; }
item() { # name qty unitPrice labels total
  printf '[{"Name":"%s","Quantity":%s,"UnitPrice":%s,"Labels":%s,"TotalAmount":%s}]' \
    "$1" "$2" "$3" "$4" "$5"
}

# 1) ADVANCE SALE #1 ($400)
AS1_JSON=$(cat <<JSON
{
  "Cashier": "$CASHIER",
  "buyerId": "$BUYER_ID",
  "BuyerCostCenterId": "$BUYER_CC",
  "InvoiceNumber": "${POS_NO_PREFIX}-AS1",
  "DateAndTimeOfIssue": "$(now)",
  "invoiceType": "Advance",
  "transactionType": "Sale",
  "payment": $(pay 400),
  "Options": { "omitTextualRepresentation": 1, "omitQRCodeGen": 1 },
  "Items": $(item "Advance payment 1" 1 400 '["A"]' 400)
}
JSON
)
AS1_RESP=$(log_and_sign "AS1" "$AS1_JSON")
AS1_SDC=$(jq -r '.invoiceNumber' "$LOG_DIR/AS1_response.json")

# 2) ADVANCE SALE #2 ($400)
AS2_JSON=$(cat <<JSON
{
  "Cashier": "$CASHIER",
  "buyerId": "$BUYER_ID",
  "BuyerCostCenterId": "$BUYER_CC",
  "InvoiceNumber": "${POS_NO_PREFIX}-AS2",
  "DateAndTimeOfIssue": "$(now)",
  "invoiceType": "Advance",
  "transactionType": "Sale",
  "payment": $(pay 400),
  "Options": { "omitTextualRepresentation": 1, "omitQRCodeGen": 1 },
  "Items": $(item "Advance payment 2" 1 400 '["A"]' 400)
}
JSON
)
AS2_RESP=$(log_and_sign "AS2" "$AS2_JSON")
AS2_SDC=$(jq -r '.invoiceNumber' "$LOG_DIR/AS2_response.json")

# 3) ADVANCE SALE #3 ($300)
AS3_JSON=$(cat <<JSON
{
  "Cashier": "$CASHIER",
  "buyerId": "$BUYER_ID",
  "BuyerCostCenterId": "$BUYER_CC",
  "InvoiceNumber": "${POS_NO_PREFIX}-AS3",
  "DateAndTimeOfIssue": "$(now)",
  "invoiceType": "Advance",
  "transactionType": "Sale",
  "payment": $(pay 300),
  "Options": { "omitTextualRepresentation": 1, "omitQRCodeGen": 1 },
  "Items": $(item "Advance payment 3" 1 300 '["A"]' 300)
}
JSON
)
AS3_RESP=$(log_and_sign "AS3" "$AS3_JSON")
AS3_SDC=$(jq -r '.invoiceNumber' "$LOG_DIR/AS3_response.json")

# 4) ADVANCE REFUND ($100) referencing AS3
AR_JSON=$(cat <<JSON
{
  "Cashier": "$CASHIER",
  "buyerId": "$BUYER_ID",
  "BuyerCostCenterId": "$BUYER_CC",
  "InvoiceNumber": "${POS_NO_PREFIX}-AR1",
  "DateAndTimeOfIssue": "$(now)",
  "invoiceType": "Advance",
  "transactionType": "Refund",
  "referentDocumentNumber": "$AS3_SDC",
  "payment": $(pay 100),
  "Options": { "omitTextualRepresentation": 1, "omitQRCodeGen": 1 },
  "Items": $(item "Advance refund" 1 100 '["A"]' 100)
}
JSON
)
AR1_RESP=$(log_and_sign "AR1" "$AR_JSON")
AR1_SDC=$(jq -r '.invoiceNumber' "$LOG_DIR/AR1_response.json")

# 5) NORMAL SALE ($1000) referencing AR1
NS_JSON=$(cat <<JSON
{
  "Cashier": "$CASHIER",
  "buyerId": "$BUYER_ID",
  "BuyerCostCenterId": "$BUYER_CC",
  "InvoiceNumber": "${POS_NO_PREFIX}-NS1",
  "DateAndTimeOfIssue": "$(now)",
  "invoiceType": "Normal",
  "transactionType": "Sale",
  "referentDocumentNumber": "$AR1_SDC",
  "payment": $(pay 1000),
  "Options": { "omitTextualRepresentation": 0, "omitQRCodeGen": 1 },
  "Items": [
    { "Name": "Final goods/service", "Quantity": 1, "UnitPrice": 1000, "Labels": ["A"], "TotalAmount": 1000 }
  ]
}
JSON
)
NS_RESP=$(log_and_sign "NS1" "$NS_JSON")
NS_SDC=$(jq -r '.invoiceNumber' "$LOG_DIR/NS1_response.json")

echo "=== DONE ==="
echo "Log directory: $LOG_DIR"
echo "AS1 SDC Invoice No: $AS1_SDC"
echo "AS2 SDC Invoice No: $AS2_SDC"
echo "AS3 SDC Invoice No: $AS3_SDC"
echo "AR1 SDC Invoice No: $AR1_SDC"
echo "NS1 SDC Invoice No: $NS_SDC"

