#!/bin/bash

# Simple test script to verify the backend is working

echo "======================================"
echo "🧪 Backend API Test Script"
echo "======================================"
echo ""

# Test 1: Health check
echo "1️⃣  Testing health endpoint..."
HEALTH=$(curl -s http://localhost:3000/api/health)
echo "Response: $HEALTH"
echo ""

# Test 2: Preflight request
echo "2️⃣  Testing CORS preflight..."
curl -i -X OPTIONS http://localhost:3000/api/admin/login \
  -H "Origin: http://localhost:3000" \
  -H "Access-Control-Request-Method: POST" 2>/dev/null | head -20
echo ""
echo ""

# Test 3: Login request
echo "3️⃣  Testing login endpoint..."
LOGIN=$(curl -s -X POST http://localhost:3000/api/admin/login \
  -H "Content-Type: application/json" \
  -d '{"email":"admin@numerology.com","password":"admin123"}')
echo "Response: $LOGIN"
echo ""

# Extract token for next test
TOKEN=$(echo $LOGIN | grep -o '"accessToken":"[^"]*' | cut -d'"' -f4)
if [ -z "$TOKEN" ]; then
  TOKEN="YOUR_TOKEN_HERE"
fi

echo "======================================"
echo "✅ Test Complete!"
echo "======================================"
echo ""
echo "If you see:"
echo "  - Health: ✅ server is running"
echo "  - CORS: ✅ 200 OK (not 204)"
echo "  - Login: ✅ status: true with tokens"
echo ""
echo "Then your backend is working perfectly! 🎉"
echo ""
echo "Access Token: $TOKEN"
echo ""
echo "Next: Use this token in Authorization header:"
echo "  Authorization: Bearer $TOKEN"
