#!/bin/bash
echo "=== TEST ENDPOINTS PROTÉGÉS ==="

# 1. Créer un utilisateur et récupérer le token
TIMESTAMP=$(date +%s)
echo "1. Register..."
REGISTER=$(curl -s -X POST "http://localhost:8080/api/v1/auth/register" \
  -H "Content-Type: application/json" \
  -d "{\"email\":\"test${TIMESTAMP}@test.com\",\"username\":\"test${TIMESTAMP}\",\"password\":\"Xk9\$mP2#vL7@nQ4!wR8\",\"password_confirm\":\"Xk9\$mP2#vL7@nQ4!wR8\"}")

TOKEN=$(echo "$REGISTER" | jq -r '.data.token.access_token')
REFRESH_TOKEN=$(echo "$REGISTER" | jq -r '.data.token.refresh_token')

if [ "$TOKEN" == "null" ] || [ -z "$TOKEN" ]; then
  echo "❌ Register failed - no token"
  echo "$REGISTER" | jq .
  exit 1
fi

echo "✅ Token: ${TOKEN:0:50}..."

# 2. Get Me (déjà validé)
echo -e "\n2. Get Me..."
ME=$(curl -s -X GET "http://localhost:8080/api/v1/auth/me" \
  -H "Authorization: Bearer $TOKEN")
if echo "$ME" | jq -e '.success == true' > /dev/null 2>&1; then
  echo "✅ Get Me: SUCCESS"
else
  echo "❌ Get Me: FAILED"
  echo "$ME" | jq .
fi

# 3. Create Track
echo -e "\n3. Create Track..."
CREATE_TRACK=$(curl -s -X POST "http://localhost:8080/api/v1/tracks" \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"title":"Test Track MVP","genre":"Electronic","description":"Test"}')
if echo "$CREATE_TRACK" | jq -e '.success == true or .id != null' > /dev/null 2>&1; then
  echo "✅ Create Track: SUCCESS"
  TRACK_ID=$(echo "$CREATE_TRACK" | jq -r '.id // .data.id // empty')
  echo "Track ID: $TRACK_ID"
else
  echo "❌ Create Track: FAILED"
  echo "$CREATE_TRACK" | jq .
fi

# 4. List Tracks
echo -e "\n4. List Tracks..."
LIST_TRACKS=$(curl -s -X GET "http://localhost:8080/api/v1/tracks" \
  -H "Authorization: Bearer $TOKEN")
if echo "$LIST_TRACKS" | jq -e '.success == true or .data != null or type == "array"' > /dev/null 2>&1; then
  echo "✅ List Tracks: SUCCESS"
  TRACK_COUNT=$(echo "$LIST_TRACKS" | jq '.data | length // . | length // 0' 2>/dev/null || echo "0")
  echo "Tracks count: $TRACK_COUNT"
else
  echo "❌ List Tracks: FAILED"
  echo "$LIST_TRACKS" | jq .
fi

# 5. Create Playlist
echo -e "\n5. Create Playlist..."
CREATE_PLAYLIST=$(curl -s -X POST "http://localhost:8080/api/v1/playlists" \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"title":"Test Playlist MVP","description":"Test","is_public":false}')
if echo "$CREATE_PLAYLIST" | jq -e '.success == true or .id != null' > /dev/null 2>&1; then
  echo "✅ Create Playlist: SUCCESS"
  PLAYLIST_ID=$(echo "$CREATE_PLAYLIST" | jq -r '.id // .data.id // empty')
  echo "Playlist ID: $PLAYLIST_ID"
else
  echo "❌ Create Playlist: FAILED"
  echo "$CREATE_PLAYLIST" | jq .
fi

# 6. List Playlists
echo -e "\n6. List Playlists..."
LIST_PLAYLISTS=$(curl -s -X GET "http://localhost:8080/api/v1/playlists" \
  -H "Authorization: Bearer $TOKEN")
if echo "$LIST_PLAYLISTS" | jq -e '.success == true or .data != null or type == "array"' > /dev/null 2>&1; then
  echo "✅ List Playlists: SUCCESS"
  PLAYLIST_COUNT=$(echo "$LIST_PLAYLISTS" | jq '.data | length // . | length // 0' 2>/dev/null || echo "0")
  echo "Playlists count: $PLAYLIST_COUNT"
else
  echo "❌ List Playlists: FAILED"
  echo "$LIST_PLAYLISTS" | jq .
fi

# 7. Search Playlists
echo -e "\n7. Search Playlists..."
SEARCH_PLAYLISTS=$(curl -s -X GET "http://localhost:8080/api/v1/playlists/search?q=test" \
  -H "Authorization: Bearer $TOKEN")
if echo "$SEARCH_PLAYLISTS" | jq -e '.success == true or .data != null or type == "array"' > /dev/null 2>&1; then
  echo "✅ Search Playlists: SUCCESS"
  SEARCH_COUNT=$(echo "$SEARCH_PLAYLISTS" | jq '.data | length // . | length // 0' 2>/dev/null || echo "0")
  echo "Search results: $SEARCH_COUNT"
else
  echo "❌ Search Playlists: FAILED"
  echo "$SEARCH_PLAYLISTS" | jq .
fi

# 8. Sessions (avec trailing slash pour éviter 301)
echo -e "\n8. Sessions..."
SESSIONS=$(curl -s -X GET "http://localhost:8080/api/v1/sessions/" \
  -H "Authorization: Bearer $TOKEN")
if echo "$SESSIONS" | jq -e '.success == true or .data != null or type == "array"' > /dev/null 2>&1; then
  echo "✅ Sessions: SUCCESS"
  SESSION_COUNT=$(echo "$SESSIONS" | jq '.data | length // . | length // 0' 2>/dev/null || echo "0")
  echo "Sessions count: $SESSION_COUNT"
else
  echo "❌ Sessions: FAILED"
  echo "$SESSIONS" | jq .
fi

# 9. Refresh Token
echo -e "\n9. Refresh Token..."
REFRESH=$(curl -s -X POST "http://localhost:8080/api/v1/auth/refresh" \
  -H "Content-Type: application/json" \
  -d "{\"refresh_token\":\"$REFRESH_TOKEN\"}")
if echo "$REFRESH" | jq -e '.success == true or .data.token.access_token != null' > /dev/null 2>&1; then
  echo "✅ Refresh Token: SUCCESS"
  NEW_TOKEN=$(echo "$REFRESH" | jq -r '.data.token.access_token // .token.access_token // empty')
  if [ -n "$NEW_TOKEN" ]; then
    echo "New token: ${NEW_TOKEN:0:50}..."
  fi
else
  echo "❌ Refresh Token: FAILED"
  echo "$REFRESH" | jq .
fi

# 10. Logout
echo -e "\n10. Logout..."
LOGOUT=$(curl -s -X POST "http://localhost:8080/api/v1/auth/logout" \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d "{\"refresh_token\":\"$REFRESH_TOKEN\"}")
if echo "$LOGOUT" | jq -e '.success == true' > /dev/null 2>&1; then
  echo "✅ Logout: SUCCESS"
else
  echo "❌ Logout: FAILED (non-blocking)"
  echo "$LOGOUT" | jq .
fi

echo -e "\n=== TESTS TERMINÉS ==="

