#!/bin/bash

# Script de diagnostic pour le problème de register

TIMESTAMP=$(date +%s)
TEST_EMAIL="diagnose-${TIMESTAMP}@example.com"
TEST_USERNAME="user${TIMESTAMP}"
TEST_PASSWORD="MySecurePass2024!@#"

echo "=== DIAGNOSTIC REGISTER ==="
echo ""
echo "1. Testing register endpoint..."
REGISTER_RESPONSE=$(curl -s -X POST "http://localhost:8080/api/v1/auth/register" \
  -H "Content-Type: application/json" \
  -d "{\"email\":\"$TEST_EMAIL\",\"username\":\"$TEST_USERNAME\",\"password\":\"$TEST_PASSWORD\",\"password_confirm\":\"$TEST_PASSWORD\"}")

echo "$REGISTER_RESPONSE" | jq .

echo ""
echo "2. Checking HTTP status code..."
HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" -X POST "http://localhost:8080/api/v1/auth/register" \
  -H "Content-Type: application/json" \
  -d "{\"email\":\"$TEST_EMAIL\",\"username\":\"$TEST_USERNAME\",\"password\":\"$TEST_PASSWORD\",\"password_confirm\":\"$TEST_PASSWORD\"}")
echo "HTTP Code: $HTTP_CODE"

echo ""
echo "3. Testing with different password (no common words)..."
TEST_PASSWORD2="XyZ987AbC!@#"
REGISTER_RESPONSE2=$(curl -s -X POST "http://localhost:8080/api/v1/auth/register" \
  -H "Content-Type: application/json" \
  -d "{\"email\":\"diagnose2-${TIMESTAMP}@example.com\",\"username\":\"user2${TIMESTAMP}\",\"password\":\"$TEST_PASSWORD2\",\"password_confirm\":\"$TEST_PASSWORD2\"}")
echo "$REGISTER_RESPONSE2" | jq .

echo ""
echo "=== RECOMMENDATIONS ==="
echo "1. Vérifier que le backend a été redémarré avec le nouveau code"
echo "2. Vérifier les logs du backend pour voir l'erreur exacte"
echo "3. Vérifier la connexion à la base de données"
echo "4. Vérifier que les migrations ont été exécutées"

