#!/bin/bash

echo "🧪 Testing About Page CMS Functionality"
echo "========================================"
echo ""

# Get admin token
cd /media/pts/Website/PromptTech_Solution_Site/backend
source venv/bin/activate

TOKEN=$(python -c "
import asyncio
from database import AsyncSessionLocal
from models import User
from sqlalchemy import select
import jwt
from datetime import datetime, timedelta

async def get_admin_token():
    async with AsyncSessionLocal() as db:
        result = await db.execute(select(User).where(User.role == 'admin').limit(1))
        admin = result.scalar_one_or_none()
        if admin:
            token = jwt.encode(
                {'sub': admin.email, 'exp': datetime.utcnow() + timedelta(hours=24)},
                'your-secret-key-change-in-production',
                algorithm='HS256'
            )
            print(token)

asyncio.run(get_admin_token())
" 2>/dev/null)

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

echo "✅ Got admin token"
echo ""

# Test 1: Get all content
echo "📄 Test 1: Fetching About Content..."
CONTENT=$(curl -s -H "Authorization: Bearer $TOKEN" http://localhost:8181/api/admin/about/content)
CONTENT_COUNT=$(echo "$CONTENT" | python3 -c "import sys, json; print(len(json.load(sys.stdin)))")
echo "   Found $CONTENT_COUNT content sections"
echo "$CONTENT" | python3 -m json.tool | head -30
echo ""

# Test 2: Get all team members
echo "👥 Test 2: Fetching Team Members..."
TEAM=$(curl -s -H "Authorization: Bearer $TOKEN" http://localhost:8181/api/admin/about/team)
TEAM_COUNT=$(echo "$TEAM" | python3 -c "import sys, json; print(len(json.load(sys.stdin)))")
echo "   Found $TEAM_COUNT team members"
echo "$TEAM" | python3 -m json.tool | head -30
echo ""

# Test 3: Get all company values
echo "💎 Test 3: Fetching Company Values..."
VALUES=$(curl -s -H "Authorization: Bearer $TOKEN" http://localhost:8181/api/admin/about/values)
VALUES_COUNT=$(echo "$VALUES" | python3 -c "import sys, json; print(len(json.load(sys.stdin)))")
echo "   Found $VALUES_COUNT company values"
echo "$VALUES" | python3 -m json.tool | head -30
echo ""

# Test 4: Update a team member
echo "✏️  Test 4: Testing Update Functionality..."
FIRST_MEMBER_ID=$(echo "$TEAM" | python3 -c "import sys, json; data=json.load(sys.stdin); print(data[0]['id'] if data else '')")

if [ ! -z "$FIRST_MEMBER_ID" ]; then
    echo "   Updating team member: $FIRST_MEMBER_ID"
    UPDATE_RESULT=$(curl -s -X PUT \
        -H "Authorization: Bearer $TOKEN" \
        -H "Content-Type: application/json" \
        -d '{"name":"Alex Johnson","role":"Founder & CEO (Updated)","is_active":true}' \
        http://localhost:8181/api/admin/about/team/$FIRST_MEMBER_ID)
    
    echo "$UPDATE_RESULT" | python3 -m json.tool
    echo ""
    echo "   Reverting update..."
    curl -s -X PUT \
        -H "Authorization: Bearer $TOKEN" \
        -H "Content-Type: application/json" \
        -d '{"name":"Alex Johnson","role":"Founder & CEO","is_active":true}' \
        http://localhost:8181/api/admin/about/team/$FIRST_MEMBER_ID > /dev/null
    echo "   ✅ Update test completed"
else
    echo "   ⚠️  No team members found to test update"
fi

echo ""
echo "========================================"
echo "✅ All tests completed!"
