#!/bin/bash
#
# Manual test script for TO-003-S3: Deposit & Reserve APIs
# Tests deposit payments and seat reservations
#

BASE_URL="http://localhost:8000"
EVENT_ID=1

echo "========================================"
echo "TO-003-S3: Deposit & Reserve APIs Test"
echo "========================================" echo ""

# Colors for output
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# Test 1: Deposit at Minimum (10%)
echo -e "${YELLOW}Test 1: Deposit at Minimum (10%)${NC}"
echo "-------------------"
echo "Order total: $150, Minimum: $15 (10%), Deposit: $15"
IDEMPOTENCY_KEY=$(uuidgen)
curl -X POST "${BASE_URL}/api/orders/deposit" \
  -H "Content-Type: application/json" \
  -H "Payment-Idempotency-Key: ${IDEMPOTENCY_KEY}" \
  -d '{
    "hold_token": "test-hold-token-deposit-' + $(date +%s) + '",
    "deposit_amount": 15.00,
    "payment_method": "cash",
    "payment_verified": true,
    "customer_name": "John Doe (Deposit 10%)",
    "customer_email": "john@test.com",
    "customer_phone": "+1234567890",
    "staff_id": 1
  }' | jq .

echo ""
echo ""

# Test 2: Deposit Below Minimum (Should Fail)
echo -e "${RED}Test 2: Deposit Below Minimum (Should Fail)${NC}"
echo "-------------------"
echo "Order total: $150, Minimum: $15, Deposit: $10 (should fail)"
IDEMPOTENCY_KEY=$(uuidgen)
curl -X POST "${BASE_URL}/api/orders/deposit" \
  -H "Content-Type: application/json" \
  -H "Payment-Idempotency-Key: ${IDEMPOTENCY_KEY}" \
  -d '{
    "hold_token": "test-hold-token-deposit-' + $(date +%s) + '",
    "deposit_amount": 10.00,
    "payment_method": "cash",
    "payment_verified": true,
    "customer_name": "Jane Doe (Below Min)",
    "customer_email": "jane@test.com",
    "customer_phone": "+1234567891",
    "staff_id": 1
  }' | jq .

echo ""
echo ""

# Test 3: Deposit Above Maximum (Should Fail)
echo -e "${RED}Test 3: Deposit Above Maximum (Should Fail)${NC}"
echo "-------------------"
echo "Order total: $150, Maximum: $149, Deposit: $150 (should fail)"
IDEMPOTENCY_KEY=$(uuidgen)
curl -X POST "${BASE_URL}/api/orders/deposit" \
  -H "Content-Type: application/json" \
  -H "Payment-Idempotency-Key: ${IDEMPOTENCY_KEY}" \
  -d '{
    "hold_token": "test-hold-token-deposit-' + $(date +%s) + '",
    "deposit_amount": 150.00,
    "payment_method": "cash",
    "payment_verified": true,
    "customer_name": "Bob Smith (Above Max)",
    "customer_email": "bob@test.com",
    "customer_phone": "+1234567892",
    "staff_id": 1
  }' | jq .

echo ""
echo ""

# Test 4: Deposit Mid-Range (50%)
echo -e "${GREEN}Test 4: Deposit Mid-Range (50%)${NC}"
echo "-------------------"
echo "Order total: $150, Deposit: $75 (50%)"
IDEMPOTENCY_KEY=$(uuidgen)
curl -X POST "${BASE_URL}/api/orders/deposit" \
  -H "Content-Type: application/json" \
  -H "Payment-Idempotency-Key: ${IDEMPOTENCY_KEY}" \
  -d '{
    "hold_token": "test-hold-token-deposit-' + $(date +%s) + '",
    "deposit_amount": 75.00,
    "payment_method": "credit_card",
    "payment_verified": true,
    "customer_name": "Alice Johnson (50%)",
    "customer_email": "alice@test.com",
    "customer_phone": "+1234567893",
    "staff_id": 1
  }' | jq .

echo ""
echo ""

# Test 5: Reserve with Default Expiry (24h)
echo -e "${GREEN}Test 5: Reserve with Default Expiry (24h)${NC}"
echo "-------------------"
IDEMPOTENCY_KEY=$(uuidgen)
curl -X POST "${BASE_URL}/api/reservations/create" \
  -H "Content-Type: application/json" \
  -H "Payment-Idempotency-Key: ${IDEMPOTENCY_KEY}" \
  -d '{
    "hold_token": "test-hold-token-reserve-' + $(date +%s) + '",
    "customer_name": "Charlie Brown (Reserve)",
    "customer_email": "charlie@test.com",
    "customer_phone": "+1234567894",
    "staff_id": 1,
    "notes": "Customer will pay tomorrow morning"
  }' | jq .

echo ""
echo ""

# Test 6: Reserve with Custom Expiry (48h)
echo -e "${GREEN}Test 6: Reserve with Custom Expiry (48h)${NC}"
echo "-------------------"
IDEMPOTENCY_KEY=$(uuidgen)
curl -X POST "${BASE_URL}/api/reservations/create" \
  -H "Content-Type: application/json" \
  -H "Payment-Idempotency-Key: ${IDEMPOTENCY_KEY}" \
  -d '{
    "hold_token": "test-hold-token-reserve-' + $(date +%s) + '",
    "customer_name": "Diana Prince (Reserve 48h)",
    "customer_email": "diana@test.com",
    "customer_phone": "+1234567895",
    "expiry_hours": 48,
    "staff_id": 1,
    "notes": "Customer needs extra time to arrange payment"
  }' | jq .

echo ""
echo ""

# Test 7: Reserve with Invalid Expiry (Should Fail)
echo -e "${RED}Test 7: Reserve with Invalid Expiry (Should Fail)${NC}"
echo "-------------------"
echo "Expiry: 200 hours (exceeds 168 hour max)"
IDEMPOTENCY_KEY=$(uuidgen)
curl -X POST "${BASE_URL}/api/reservations/create" \
  -H "Content-Type: application/json" \
  -H "Payment-Idempotency-Key: ${IDEMPOTENCY_KEY}" \
  -d '{
    "hold_token": "test-hold-token-reserve-' + $(date +%s) + '",
    "customer_name": "Eve Taylor (Invalid Expiry)",
    "customer_email": "eve@test.com",
    "customer_phone": "+1234567896",
    "expiry_hours": 200,
    "staff_id": 1
  }' | jq .

echo ""
echo "========================================"
echo "Tests Complete!"
echo "========================================"
echo ""
echo "Expected Results:"
echo "- Test 1 (Deposit 10%): ${GREEN}✓ SUCCESS${NC} (201 with balance_due = $135)"
echo "- Test 2 (Below Min): ${RED}✗ FAIL${NC} (422 validation error)"
echo "- Test 3 (Above Max): ${RED}✗ FAIL${NC} (422 validation error)"
echo "- Test 4 (Deposit 50%): ${GREEN}✓ SUCCESS${NC} (201 with balance_due = $75)"
echo "- Test 5 (Reserve 24h): ${GREEN}✓ SUCCESS${NC} (201 with reserved_until)"
echo "- Test 6 (Reserve 48h): ${GREEN}✓ SUCCESS${NC} (201 with reserved_until +48h)"
echo "- Test 7 (Invalid Expiry): ${RED}✗ FAIL${NC} (422 validation error)"
echo ""
echo "All tests will return 404 (hold not found) unless you create real holds first."
echo ""
echo "To test with real holds:"
echo "1. Create seat hold via /api/seats/hold"
echo "2. Use the returned hold_token in deposit/reserve requests"
echo "3. Check response for receipt_pdf_url / confirmation_pdf_url field"
echo ""
