#!/bin/bash

echo "🔍 SERVER-SIDE DEBUG (Run this on the actual server)"
echo "=================================================="

# Check if we're on the server
if [ ! -f "/opt/rtsda/church-api/Cargo.toml" ]; then
    echo "❌ This script must be run on the server (rockvilleavdesktop)"
    echo "   SSH to the server and run this script there"
    exit 1
fi

echo "✅ Running on server"

# Check uploads directory
echo "📁 Checking uploads directory..."
if [ -d "/opt/rtsda/church-api/uploads/events" ]; then
    echo "✅ uploads/events exists"
    echo "Files:"
    ls -la /opt/rtsda/church-api/uploads/events/
else
    echo "❌ uploads/events directory not found"
    echo "Creating it..."
    mkdir -p /opt/rtsda/church-api/uploads/events
    chown rockvilleav:rockvilleav /opt/rtsda/church-api/uploads/events
    echo "✅ Created uploads/events directory"
fi

# Check server logs
echo ""
echo "📜 Recent server logs..."
journalctl -u church-api --since "5 minutes ago" --no-pager | tail -20

# Check the pending events endpoint issue
echo ""
echo "🔍 Testing pending events endpoint..."
AUTH_TOKEN=$(curl -s -X POST https://api.rockvilletollandsda.church/api/auth/login \
    -H "Content-Type: application/json" \
    -d '{"username": "admin", "password": "Alright8-Reapply-Shrewdly-Platter-Important-Keenness-Banking-Streak-Tactile"}' \
    | jq -r '.data.token')

echo "Testing: https://api.rockvilletollandsda.church/api/events/pending"
curl -v -H "Authorization: Bearer $AUTH_TOKEN" \
    "https://api.rockvilletollandsda.church/api/events/pending" 2>&1

echo ""
echo "🎯 What to check:"
echo "1. Are WebP files being created in uploads/events/?"
echo "2. What's the UUID parsing error in pending events?"
echo "3. Are there any crash logs in journalctl?"
