#!/usr/bin/env bash
# F13 + C3 — geçerli müşteri kimliği ile (iyzico sandbox)
set -euo pipefail
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
LOG="$ROOT/frontend/qa/logs"
API="${FAUNAMIX_QA_API:-https://faunamix-production.up.railway.app}"
TS=$(date +%s)

CUST_BASE='{"full_name":"QA F13","email":"qa-f13-'$TS'@faunamix.test","phone":"5058224348","identity_no":"42049828030","city":"Muğla","district":"Fethiye","address_line":"QA adres","postal_code":"48300"}'
LEGAL='{"mesafeli":true,"on_bilgilendirme":true,"iade":true,"gizlilik":true}'

checkout() {
  local idem="$1" body="$2" out="$3"
  curl -sS -X POST "$API/api/checkout_create.php" \
    -H "Content-Type: application/json" \
    -H "Idempotency-Key: $idem" \
    -d "$body" | tee "$out"
}

# S2 coupon
BODY=$(php -r "echo json_encode(['items'=>[['product_id'=>17,'quantity'=>1,'variant_id'=>0]],'customer'=>array_merge(json_decode('$CUST_BASE',true),['coupon_code'=>'TESTKUPON','shipping_method'=>'standard']),'legal_acceptances'=>json_decode('$LEGAL',true)]);")
checkout "f13-s2-$TS" "$BODY" "$LOG/f13-s2-v2.json"
echo

# S3 multi
BODY=$(php -r "echo json_encode(['items'=>[['product_id'=>16,'quantity'=>1,'variant_id'=>0],['product_id'=>17,'quantity'=>1,'variant_id'=>0]],'customer'=>array_merge(json_decode('$CUST_BASE',true),['shipping_method'=>'standard']),'legal_acceptances'=>json_decode('$LEGAL',true)]);")
checkout "f13-s3-$TS" "$BODY" "$LOG/f13-s3-v2.json"
echo

# S4 express
BODY=$(php -r "echo json_encode(['items'=>[['product_id'=>17,'quantity'=>1,'variant_id'=>0]],'customer'=>array_merge(json_decode('$CUST_BASE',true),['shipping_method'=>'express']),'legal_acceptances'=>json_decode('$LEGAL',true)]);")
checkout "f13-s4-$TS" "$BODY" "$LOG/f13-s4-v2.json"
echo

# S5 variant
VID=$(mysql -N -e "SELECT id FROM product_variants WHERE sku='QA-VAR-17' LIMIT 1" 2>/dev/null || echo 1)
BODY=$(php -r "echo json_encode(['items'=>[['product_id'=>17,'quantity'=>1,'variant_id'=>(int)'$VID']],'customer'=>array_merge(json_decode('$CUST_BASE',true),['shipping_method'=>'standard']),'legal_acceptances'=>json_decode('$LEGAL',true)]);")
checkout "f13-s5-$TS" "$BODY" "$LOG/f13-s5-v2.json"
echo

# S6 retry path
BODY=$(php -r "echo json_encode(['items'=>[['product_id'=>17,'quantity'=>1,'variant_id'=>0]],'customer'=>array_merge(json_decode('$CUST_BASE',true),['shipping_method'=>'standard']),'legal_acceptances'=>json_decode('$LEGAL',true)]);")
RAW=$(checkout "f13-s6-create-$TS" "$BODY" "$LOG/f13-s6-create-v2.json")
TOKEN=$(echo "$RAW" | php -r '$j=json_decode(stream_get_contents(STDIN),true);echo $j["order_token"]??"";')
if [[ -n "$TOKEN" ]]; then
  curl -sS -X POST "$API/api/order_retry_payment.php" -H "Content-Type: application/json" \
    -d "{\"token\":\"$TOKEN\"}" | tee "$LOG/f13-s6-retry-v2.json"
fi
echo

# C3 parallel product 16 stock=1
mysql -e "UPDATE products SET stock_quantity=1 WHERE id=16" 2>/dev/null || true
BODY_A=$(php -r "echo json_encode(['items'=>[['product_id'=>16,'quantity'=>1,'variant_id'=>0]],'customer'=>array_merge(json_decode('$CUST_BASE',true),['email'=>'qa-c3a-$TS@faunamix.test','shipping_method'=>'standard']),'legal_acceptances'=>json_decode('$LEGAL',true)]);")
BODY_B=$(php -r "echo json_encode(['items'=>[['product_id'=>16,'quantity'=>1,'variant_id'=>0]],'customer'=>array_merge(json_decode('$CUST_BASE',true),['email'=>'qa-c3b-$TS@faunamix.test','shipping_method'=>'standard']),'legal_acceptances'=>json_decode('$LEGAL',true)]);")
checkout "c3-a-$TS" "$BODY_A" "$LOG/c3-a-v2.json" &
checkout "c3-b-$TS" "$BODY_B" "$LOG/c3-b-v2.json" &
wait
echo done
