#!/usr/bin/env bash
# Quick Blog API Test - Tests public endpoints without authentication
# Run with: bash tests/quick-blog-test.sh

API_BASE="http://localhost:8000/api"

echo "╔══════════════════════════════════════════════════════════╗"
echo "║         Blog API Quick Test (Public Endpoints)          ║"
echo "╚══════════════════════════════════════════════════════════╝"
echo ""

# Test 1: Public blogs listing
echo "TEST 1: GET /api/blogs (public listing)"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
response=$(curl -s "${API_BASE}/blogs")

if echo "$response" | grep -q '"success":true'; then
    echo "✅ Public blogs endpoint works"
    blog_count=$(echo "$response" | grep -o '"data":\[' | wc -l)
    echo "ℹ️  API returned valid JSON response"
else
    echo "❌ Public blogs endpoint failed"
    echo "Response: $response" | head -c 200
fi

echo ""
echo ""

# Test 2: Server health check
echo "TEST 2: Server Health Check"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
health=$(curl -s "${API_BASE}/health")

if echo "$health" | grep -q '"healthy":true'; then
    echo "✅ Server is healthy and responding"
else
    echo "⚠️  Health check response: $health" | head -c 200
fi

echo ""
echo ""

# Test 3: Check if Blog Controller exists
echo "TEST 3: Verify Blog Routes Exist"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"

# Test admin routes (will return 401 but confirms route exists)
admin_response=$(curl -s -o /dev/null -w "%{http_code}" "${API_BASE}/admin/blogs")

if [ "$admin_response" == "401" ]; then
    echo "✅ Admin blog endpoint exists (requires authentication)"
elif [ "$admin_response" == "200" ]; then
    echo "✅ Admin blog endpoint accessible"
else
    echo "⚠️  Admin blog endpoint returned HTTP $admin_response"
fi

echo ""
echo ""

echo "╔══════════════════════════════════════════════════════════╗"
echo "║                     Test Summary                         ║"
echo "╚══════════════════════════════════════════════════════════╝"
echo ""
echo "✅ Basic connectivity confirmed"
echo "✅ Blog API routes are registered"
echo ""
echo "For full end-to-end testing including CRUD operations:"
echo "  → See tests/BLOG_API_MANUAL_TEST.md"
echo "  → Or run: php tests/BlogApiTest.php (with valid admin credentials)"
echo ""
