#!/bin/bash

echo "🚀 Starting User Service and Testing API"

# Activate virtual environment
source .venv/bin/activate

# Set PYTHONPATH
export PYTHONPATH="${PWD}:${PYTHONPATH}"

# Start user service in background
cd services/user_service
python -m uvicorn main:app --host 0.0.0.0 --port 8001 &
USER_SERVICE_PID=$!

echo "⏳ Waiting for service to start..."
sleep 5

# Go back to project root
cd ../..

# Test registration
echo "🧪 Testing user registration..."
RESPONSE=$(curl -s -X POST "http://localhost:8001/api/v1/register" \
-H "Content-Type: application/json" \
-d '{
  "email": "test@example.com",
  "password": "testpassword123",
  "first_name": "Test",
  "last_name": "User",
  "phone": "+1234567890"
}')

echo "📝 Registration response:"
echo "$RESPONSE" | jq . 2>/dev/null || echo "$RESPONSE"

# Test login
echo -e "\n🧪 Testing user login..."
LOGIN_RESPONSE=$(curl -s -X POST "http://localhost:8001/api/v1/login" \
-H "Content-Type: application/json" \
-d '{
  "email": "test@example.com",
  "password": "testpassword123"
}')

echo "📝 Login response:"
echo "$LOGIN_RESPONSE" | jq . 2>/dev/null || echo "$LOGIN_RESPONSE"

# Stop the service
echo -e "\n🛑 Stopping service..."
kill $USER_SERVICE_PID

echo "✅ Test completed!"