#!/bin/bash
# Script de test MVP - À exécuter après redémarrage du backend

set -e

echo "=== MVP ENDPOINTS TEST ==="
echo ""

# 1. Register
TIMESTAMP=$(date +%s)
echo "1. Testing Register..."
REGISTER=$(curl -s -X POST "http://localhost:8080/api/v1/auth/register" \
  -H "Content-Type: application/json" \
  -d "{\"email\":\"mvp${TIMESTAMP}@test.com\",\"username\":\"mvp${TIMESTAMP}\",\"password\":\"Test123!Password\",\"password_confirm\":\"Test123!Password\"}")

HTTP_CODE=$(echo "$REGISTER" | jq -r '.success // false')
TOKEN=$(echo "$REGISTER" | jq -r '.data.token.access_token // .data.access_token // .access_token // empty')

if [ "$HTTP_CODE" = "true" ] && [ -n "$TOKEN" ] && [ "$TOKEN" != "null" ] && [ "$TOKEN" != "" ]; then
  echo "✅ Register: Token reçu"
  ACCESS_TOKEN="$TOKEN"
else
  echo "❌ Register: Échec"
  echo "$REGISTER" | jq .
  exit 1
fi

# 2. Login
echo ""
echo "2. Testing Login..."
LOGIN=$(curl -s -X POST "http://localhost:8080/api/v1/auth/login" \
  -H "Content-Type: application/json" \
  -d "{\"email\":\"mvp${TIMESTAMP}@test.com\",\"password\":\"Test123!Password\"}")

LOGIN_HTTP=$(echo "$LOGIN" | jq -r '.success // false')
LOGIN_TOKEN=$(echo "$LOGIN" | jq -r '.data.token.access_token // .data.access_token // .access_token // empty')

if [ "$LOGIN_HTTP" = "true" ] && [ -n "$LOGIN_TOKEN" ] && [ "$LOGIN_TOKEN" != "null" ]; then
  echo "✅ Login: OK"
  ACCESS_TOKEN="$LOGIN_TOKEN"
else
  echo "❌ Login: Échec"
  echo "$LOGIN" | jq .
  exit 1
fi

# 3. Get Me
echo ""
echo "3. Testing Get Me..."
ME=$(curl -s -X GET "http://localhost:8080/api/v1/auth/me" \
  -H "Authorization: Bearer $ACCESS_TOKEN")

ME_SUCCESS=$(echo "$ME" | jq -r '.success // false')
if [ "$ME_SUCCESS" = "true" ]; then
  echo "✅ Get Me: OK"
else
  echo "❌ Get Me: Échec"
  echo "$ME" | jq .
  exit 1
fi

# 4. Create Track (ISSUE-003)
echo ""
echo "4. Testing Create Track (ISSUE-003)..."
TRACK=$(curl -s -w "\nHTTP_CODE:%{http_code}" -X POST "http://localhost:8080/api/v1/tracks" \
  -H "Authorization: Bearer $ACCESS_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"title":"MVP Test Track","genre":"Electronic","artist":"Test Artist"}')

HTTP_CODE=$(echo "$TRACK" | grep "HTTP_CODE" | cut -d: -f2)
TRACK_BODY=$(echo "$TRACK" | grep -v "HTTP_CODE")
TRACK_SUCCESS=$(echo "$TRACK_BODY" | jq -r '.success // false')

if [ "$HTTP_CODE" = "201" ] || [ "$HTTP_CODE" = "200" ]; then
  if [ "$TRACK_SUCCESS" = "true" ]; then
    echo "✅ Create Track: OK"
    TRACK_ID=$(echo "$TRACK_BODY" | jq -r '.data.track.id // .data.id // empty')
    echo "   Track ID: $TRACK_ID"
  else
    echo "⚠️ Create Track: HTTP $HTTP_CODE mais success=false"
    echo "$TRACK_BODY" | jq .
  fi
else
  echo "❌ Create Track: Échec (HTTP $HTTP_CODE)"
  echo "$TRACK_BODY" | jq .
fi

# 5. Create Playlist (ISSUE-005)
echo ""
echo "5. Testing Create Playlist (ISSUE-005)..."
PLAYLIST=$(curl -s -w "\nHTTP_CODE:%{http_code}" -X POST "http://localhost:8080/api/v1/playlists" \
  -H "Authorization: Bearer $ACCESS_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"name":"MVP Test Playlist","description":"Test","visibility":"private"}')

HTTP_CODE=$(echo "$PLAYLIST" | grep "HTTP_CODE" | cut -d: -f2)
PLAYLIST_BODY=$(echo "$PLAYLIST" | grep -v "HTTP_CODE")
PLAYLIST_SUCCESS=$(echo "$PLAYLIST_BODY" | jq -r '.success // false')

if [ "$HTTP_CODE" = "201" ] || [ "$HTTP_CODE" = "200" ]; then
  if [ "$PLAYLIST_SUCCESS" = "true" ]; then
    echo "✅ Create Playlist: OK"
  else
    echo "⚠️ Create Playlist: HTTP $HTTP_CODE mais success=false"
    echo "$PLAYLIST_BODY" | jq .
  fi
else
  echo "❌ Create Playlist: Échec (HTTP $HTTP_CODE)"
  echo "$PLAYLIST_BODY" | jq .
fi

# 6. List Playlists (ISSUE-004)
echo ""
echo "6. Testing List Playlists (ISSUE-004)..."
PLAYLISTS=$(curl -s -w "\nHTTP_CODE:%{http_code}" -X GET "http://localhost:8080/api/v1/playlists" \
  -H "Authorization: Bearer $ACCESS_TOKEN")

HTTP_CODE=$(echo "$PLAYLISTS" | grep "HTTP_CODE" | cut -d: -f2)
PLAYLISTS_BODY=$(echo "$PLAYLISTS" | grep -v "HTTP_CODE")
PLAYLISTS_SUCCESS=$(echo "$PLAYLISTS_BODY" | jq -r '.success // false')

if [ "$HTTP_CODE" = "200" ]; then
  if [ "$PLAYLISTS_SUCCESS" = "true" ]; then
    echo "✅ List Playlists: OK"
  else
    echo "⚠️ List Playlists: HTTP $HTTP_CODE mais success=false"
    echo "$PLAYLISTS_BODY" | jq .
  fi
else
  echo "❌ List Playlists: Échec (HTTP $HTTP_CODE)"
  echo "$PLAYLISTS_BODY" | jq .
fi

# 7. Search Playlists (ISSUE-006)
echo ""
echo "7. Testing Search Playlists (ISSUE-006)..."
SEARCH=$(curl -s -w "\nHTTP_CODE:%{http_code}" -X GET "http://localhost:8080/api/v1/playlists/search?q=test" \
  -H "Authorization: Bearer $ACCESS_TOKEN")

HTTP_CODE=$(echo "$SEARCH" | grep "HTTP_CODE" | cut -d: -f2)
SEARCH_BODY=$(echo "$SEARCH" | grep -v "HTTP_CODE")
SEARCH_SUCCESS=$(echo "$SEARCH_BODY" | jq -r '.success // false')

if [ "$HTTP_CODE" = "200" ]; then
  if [ "$SEARCH_SUCCESS" = "true" ]; then
    echo "✅ Search Playlists: OK"
  else
    echo "⚠️ Search Playlists: HTTP $HTTP_CODE mais success=false"
    echo "$SEARCH_BODY" | jq .
  fi
else
  echo "❌ Search Playlists: Échec (HTTP $HTTP_CODE)"
  echo "$SEARCH_BODY" | jq .
fi

# 8. Logout
echo ""
echo "8. Testing Logout..."
REFRESH_TOKEN=$(echo "$LOGIN" | jq -r '.data.token.refresh_token // .data.refresh_token // .refresh_token // empty')
if [ -n "$REFRESH_TOKEN" ] && [ "$REFRESH_TOKEN" != "null" ]; then
  LOGOUT=$(curl -s -X POST "http://localhost:8080/api/v1/auth/logout" \
    -H "Authorization: Bearer $ACCESS_TOKEN" \
    -H "Content-Type: application/json" \
    -d "{\"refresh_token\":\"$REFRESH_TOKEN\"}")
  
  LOGOUT_SUCCESS=$(echo "$LOGOUT" | jq -r '.success // false')
  if [ "$LOGOUT_SUCCESS" = "true" ]; then
    echo "✅ Logout: OK"
  else
    echo "⚠️ Logout: Échec"
    echo "$LOGOUT" | jq .
  fi
else
  echo "⏭️ Logout: Skip (pas de refresh token)"
fi

echo ""
echo "=== MVP TEST COMPLETE ==="

