#!/bin/bash

# Test image upload endpoint
# First, login as admin to get token

echo "=== Testing Image Upload ==="

# Login as admin
TOKEN=$(curl -s -X POST http://localhost:8181/api/auth/login \
  -H "Content-Type: application/json" \
  -d '{"email":"admin@techzone.com","password":"admin123"}' | python3 -c "import sys, json; print(json.load(sys.stdin)['access_token'])" 2>/dev/null)

if [ -z "$TOKEN" ]; then
  echo "❌ Failed to get admin token"
  exit 1
fi

echo "✓ Got admin token"

# Create a test image file
echo "Creating test image..."
convert -size 100x100 xc:blue /tmp/test_product_image.jpg 2>/dev/null || {
  # Fallback if imagemagick not available
  echo "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==" | base64 -d > /tmp/test_product_image.png
}

# Try to upload the image
echo "Uploading image..."
RESPONSE=$(curl -s -X POST http://localhost:8181/api/upload/image \
  -H "Authorization: Bearer $TOKEN" \
  -F "file=@/tmp/test_product_image.jpg" 2>&1)

echo "Response: $RESPONSE"

if echo "$RESPONSE" | grep -q "url"; then
  echo "✓ Image upload successful!"
else
  echo "❌ Image upload failed"
  echo "Full response: $RESPONSE"
fi

# Cleanup
rm -f /tmp/test_product_image.jpg /tmp/test_product_image.png
