#!/bin/bash

# API Endpoint Test Script
echo "=== RateRight v2 API Endpoint Tests ==="
echo

# Base URL
BASE_URL="https://rivet.rateright.com.au"

# Test CSRF endpoint
echo "1. Testing CSRF endpoint..."
curl -s -X GET "${BASE_URL}/api/auth/csrf" \
  -H "Content-Type: application/json" | jq . 2>/dev/null || echo "CSRF endpoint failed"
echo

# Test without auth (should fail)
echo "2. Testing protected endpoints without auth..."
echo "a) Find matches:"
curl -s -X POST "${BASE_URL}/api/match/find-matches" \
  -H "Content-Type: application/json" \
  -d '{}' | jq . 2>/dev/null || echo "Find matches endpoint failed"
echo

echo "b) Create payment:"
curl -s -X POST "${BASE_URL}/api/payments/create" \
  -H "Content-Type: application/json" \
  -d '{}' | jq . 2>/dev/null || echo "Create payment endpoint failed"
echo

# Test ABN lookup
echo "3. Testing ABN lookup endpoint..."
curl -s -X POST "${BASE_URL}/api/abn-lookup" \
  -H "Content-Type: application/json" \
  -d '{"abn":"74172177831"}' | jq . 2>/dev/null || echo "ABN lookup endpoint failed"
echo

# Test AI endpoints
echo "4. Testing AI endpoints..."
echo "a) Generate job:"
curl -s -X POST "${BASE_URL}/api/ai/generate-job" \
  -H "Content-Type: application/json" \
  -d '{"title":"Electrician needed","trade":"Electrical"}' | jq . 2>/dev/null || echo "Generate job endpoint failed"
echo

# Test Stripe webhook
echo "5. Testing Stripe webhook endpoint..."
curl -s -X POST "${BASE_URL}/api/webhooks/stripe" \
  -H "Content-Type: application/json" \
  -d '{}' | jq . 2>/dev/null || echo "Stripe webhook endpoint failed"
echo

# Test geocode endpoint
echo "6. Testing geocode endpoint..."
curl -s -X POST "${BASE_URL}/api/geocode" \
  -H "Content-Type: application/json" \
  -d '{"address":"Sydney, Australia"}' | jq . 2>/dev/null || echo "Geocode endpoint failed"
echo

echo "=== API Tests Complete ==="