#!/bin/bash

# CMS File Upload Validation Test Script
# Tests error handling and validation responses

API_URL="http://localhost:8000/api"
ADMIN_EMAIL="admin@showprima.com"
ADMIN_PASSWORD="password"

echo "========================================="
echo "CMS File Upload Validation Tests"
echo "========================================="
echo ""

# Step 1: Get auth token
echo "Step 1: Authenticating..."
TOKEN_RESPONSE=$(curl -s -X POST "$API_URL/admin/login" \
  -H "Content-Type: application/json" \
  -d "{\"email\":\"$ADMIN_EMAIL\",\"password\":\"$ADMIN_PASSWORD\"}")

TOKEN=$(echo $TOKEN_RESPONSE | jq -r '.data.token // .token // empty')

if [ -z "$TOKEN" ]; then
  echo "❌ Failed to get auth token"
  echo "Response: $TOKEN_RESPONSE"
  exit 1
fi

echo "✅ Authenticated successfully"
echo "Token: ${TOKEN:0:20}..."
echo ""

# Step 2: Test file too large (create a 15MB file)
echo "Step 2: Testing file too large (>10MB)..."
echo "Creating 15MB test file..."
dd if=/dev/zero of=/tmp/cms_test_large.jpg bs=1M count=15 2>/dev/null

RESPONSE=$(curl -s -X POST "$API_URL/admin/cms/files" \
  -H "Authorization: Bearer $TOKEN" \
  -F "files[]=@/tmp/cms_test_large.jpg" \
  -F "category=general")

echo "Response:"
echo "$RESPONSE" | jq .

# Check if error message is correct
HAS_ERROR=$(echo "$RESPONSE" | jq -r '.success')
if [ "$HAS_ERROR" = "false" ]; then
  echo "✅ Correctly rejected oversized file"
else
  echo "❌ Should have rejected oversized file"
fi

rm /tmp/cms_test_large.jpg
echo ""

# Step 3: Test invalid file type (create a .exe file)
echo "Step 3: Testing invalid file type (.exe)..."
echo "fake executable" > /tmp/cms_test.exe

RESPONSE=$(curl -s -X POST "$API_URL/admin/cms/files" \
  -H "Authorization: Bearer $TOKEN" \
  -F "files[]=@/tmp/cms_test.exe" \
  -F "category=general")

echo "Response:"
echo "$RESPONSE" | jq .

HAS_ERROR=$(echo "$RESPONSE" | jq -r '.success')
if [ "$HAS_ERROR" = "false" ]; then
  echo "✅ Correctly rejected invalid file type"
else
  echo "❌ Should have rejected .exe file"
fi

rm /tmp/cms_test.exe
echo ""

# Step 4: Test missing files parameter
echo "Step 4: Testing missing files parameter..."

RESPONSE=$(curl -s -X POST "$API_URL/admin/cms/files" \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"category":"general"}')

echo "Response:"
echo "$RESPONSE" | jq .

HAS_ERROR=$(echo "$RESPONSE" | jq -r '.success')
if [ "$HAS_ERROR" = "false" ]; then
  echo "✅ Correctly rejected request without files"
else
  echo "❌ Should have rejected request without files"
fi

echo ""

# Step 5: Test valid upload (create a small valid image)
echo "Step 5: Testing valid upload (1KB PNG)..."

# Create a minimal PNG file (1x1 pixel transparent PNG)
echo -ne '\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52\x00\x00\x00\x01\x00\x00\x00\x01\x08\x06\x00\x00\x00\x1f\x15\xc4\x89\x00\x00\x00\x0a\x49\x44\x41\x54\x78\x9c\x63\x00\x01\x00\x00\x05\x00\x01\x0d\x0a\x2d\xb4\x00\x00\x00\x00\x49\x45\x4e\x44\xae\x42\x60\x82' > /tmp/cms_test_valid.png

RESPONSE=$(curl -s -X POST "$API_URL/admin/cms/files" \
  -H "Authorization: Bearer $TOKEN" \
  -F "files[]=@/tmp/cms_test_valid.png" \
  -F "category=general" \
  -F "tags[]=test" \
  -F "tags[]=validation" \
  -F "description=Validation test file")

echo "Response:"
echo "$RESPONSE" | jq .

SUCCESS=$(echo "$RESPONSE" | jq -r '.success')
if [ "$SUCCESS" = "true" ]; then
  echo "✅ Successfully uploaded valid file"

  # Get file ID for cleanup
  FILE_ID=$(echo "$RESPONSE" | jq -r '.data[0].id')

  # Clean up uploaded file
  if [ ! -z "$FILE_ID" ]; then
    echo "Cleaning up uploaded file (ID: $FILE_ID)..."
    curl -s -X DELETE "$API_URL/admin/cms/files/$FILE_ID" \
      -H "Authorization: Bearer $TOKEN" > /dev/null
    echo "✅ Cleanup complete"
  fi
else
  echo "❌ Valid file upload failed"
fi

rm /tmp/cms_test_valid.png
echo ""

# Step 6: Test tag autocomplete endpoint
echo "Step 6: Testing tag autocomplete endpoint..."

RESPONSE=$(curl -s -X GET "$API_URL/admin/cms/files/tags" \
  -H "Authorization: Bearer $TOKEN")

echo "Response:"
echo "$RESPONSE" | jq .

SUCCESS=$(echo "$RESPONSE" | jq -r '.success')
if [ "$SUCCESS" = "true" ]; then
  TAG_COUNT=$(echo "$RESPONSE" | jq -r '.data.count')
  echo "✅ Tag autocomplete endpoint working (found $TAG_COUNT unique tags)"
else
  echo "❌ Tag autocomplete endpoint failed"
fi

echo ""
echo "========================================="
echo "Test Summary"
echo "========================================="
echo "All validation tests complete!"
echo ""
echo "Expected Results:"
echo "✅ Oversized file rejected (>10MB)"
echo "✅ Invalid file type rejected (.exe)"
echo "✅ Missing files rejected"
echo "✅ Valid file uploaded successfully"
echo "✅ Tag autocomplete working"
