#!/bin/bash
# Test script to verify registration fix works correctly
# This should be GREEN after the fix

set -euo pipefail

# Source test environment
cd "$(dirname "${BASH_SOURCE[0]}")"
source .env
source http/http_assert.sh

echo "🔍 Testing registration fix..."
echo "================================"

# Test 1: Valid registration should return 201
echo -e "\n1. Testing valid registration..."
TEST_EMAIL="user+fixed$(date +%s)@lab.veza" 
TEST_PASSWORD="V3za!test-$(date +%s)"
USERNAME="testuser$(date +%s)"

response=$(curl -skw '\n%{http_code}\n%{time_total}\n' \
  -H 'Content-Type: application/json' \
  -d "{\"username\":\"$USERNAME\",\"email\":\"$TEST_EMAIL\",\"password\":\"$TEST_PASSWORD\"}" \
  "$API_ORIGIN/api/auth/register")

body=$(echo "$response" | sed -n '1,/^$/p' | head -n -3)
code=$(echo "$response" | tail -n 2 | head -n 1)
time_ms=$(echo "$response" | tail -n 1 | awk '{print int($1*1000)}')

echo "Response code: $code"
echo "Response time: ${time_ms}ms"

if [ "$code" -eq 201 ]; then
    echo "✅ Valid registration returns 201 (FIXED!)"
    # Check response structure
    echo "$body" | jq -e '.access_token' >/dev/null && echo "✅ Has access_token"
    echo "$body" | jq -e '.refresh_token' >/dev/null && echo "✅ Has refresh_token"
    echo "$body" | jq -e '.user.email' >/dev/null && echo "✅ Has user object"
else
    echo "❌ Registration returned $code instead of 201"
    echo "Response: $body"
    exit 1
fi

# Test 2: Invalid email should return 400
echo -e "\n2. Testing invalid email..."
response=$(curl -skw '\n%{http_code}\n' \
  -H 'Content-Type: application/json' \
  -d '{"username":"test","email":"not-an-email","password":"ValidPass123!"}' \
  "$API_ORIGIN/api/auth/register")

body=$(echo "$response" | sed '$d')
code=$(echo "$response" | tail -1)

if [ "$code" -eq 400 ]; then
    echo "✅ Invalid email returns 400"
else
    echo "❌ Invalid email returned $code instead of 400"
    echo "Response: $body"
fi

# Test 3: Short password should return 400
echo -e "\n3. Testing short password..."
response=$(curl -skw '\n%{http_code}\n' \
  -H 'Content-Type: application/json' \
  -d '{"username":"test","email":"test@valid.com","password":"123"}' \
  "$API_ORIGIN/api/auth/register")

code=$(echo "$response" | tail -1)

if [ "$code" -eq 400 ]; then
    echo "✅ Short password returns 400"
else
    echo "❌ Short password returned $code instead of 400"
fi

# Test 4: Duplicate registration should return 409
echo -e "\n4. Testing duplicate registration..."
response=$(curl -skw '\n%{http_code}\n' \
  -H 'Content-Type: application/json' \
  -d "{\"username\":\"$USERNAME\",\"email\":\"$TEST_EMAIL\",\"password\":\"$TEST_PASSWORD\"}" \
  "$API_ORIGIN/api/auth/register")

body=$(echo "$response" | sed '$d')
code=$(echo "$response" | tail -1)

if [ "$code" -eq 409 ]; then
    echo "✅ Duplicate registration returns 409"
    echo "$body" | jq -r '.error' | grep -i "already exists" && echo "✅ Error message is correct"
else
    echo "❌ Duplicate registration returned $code instead of 409"
    echo "Response: $body"
fi

# Test 5: Missing fields should return 400
echo -e "\n5. Testing missing fields..."
response=$(curl -skw '\n%{http_code}\n' \
  -H 'Content-Type: application/json' \
  -d '{"email":"test@example.com"}' \
  "$API_ORIGIN/api/auth/register")

code=$(echo "$response" | tail -1)

if [ "$code" -eq 400 ]; then
    echo "✅ Missing fields returns 400"
else
    echo "❌ Missing fields returned $code instead of 400"
fi

echo -e "\n================================"
echo "✅ All registration tests passed!"
echo "🎉 Registration bug is FIXED!"