#!/usr/bin/env fish

echo "🔧 ACTUALLY FIXING THE ROUTES (NO BULLSHIT)"
echo "============================================"

# Backup first
cp src/main.rs src/main.rs.backup

# Fix admin routes: move pending routes before generic :id routes
sed -i '' '
/\.route("\/events\/:id", put(handlers::events::update))/i\
        .route("/events/pending", get(handlers::events::list_pending))\
        .route("/events/pending/:id/approve", post(handlers::events::approve))\
        .route("/events/pending/:id/reject", post(handlers::events::reject))\
        .route("/events/pending/:id", delete(handlers::events::delete_pending))
' src/main.rs

# Remove the old pending routes that are now duplicated
sed -i '' '/\.route("\/events\/pending", get(handlers::events::list_pending))/d' src/main.rs
sed -i '' '/\.route("\/events\/pending\/:id\/approve", post(handlers::events::approve))/d' src/main.rs
sed -i '' '/\.route("\/events\/pending\/:id\/reject", post(handlers::events::reject))/d' src/main.rs
sed -i '' '/\.route("\/events\/pending\/:id", delete(handlers::events::delete_pending))/d' src/main.rs

# Fix public routes: move submit before :id
sed -i '' '
/\.route("\/api\/events\/:id", get(handlers::events::get))/i\
        .route("/api/events/submit", post(handlers::events::submit))
' src/main.rs

# Remove the old submit route
sed -i '' '/\.route("\/api\/events\/submit", post(handlers::events::submit))/d' src/main.rs

echo "✅ Routes reordered"

# Build and test
if cargo build
    echo "✅ Build successful!"
    
    # Restart server
    sudo systemctl restart church-api
    sleep 3
    
    # Test it works
    set AUTH_RESPONSE (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"}')
    
    set JWT_TOKEN (echo $AUTH_RESPONSE | jq -r '.data.token')
    
    echo "🧪 Testing pending events endpoint..."
    set PENDING_TEST (curl -s -H "Authorization: Bearer $JWT_TOKEN" \
        "https://api.rockvilletollandsda.church/api/admin/events/pending")
    
    if echo $PENDING_TEST | grep -q success
        echo "✅ PENDING EVENTS WORKING!"
    else
        echo "❌ Still broken: $PENDING_TEST"
    end
    
    echo "🧪 Testing submit endpoint..."
    echo "test" > test.txt
    set SUBMIT_TEST (curl -s -X POST https://api.rockvilletollandsda.church/api/events/submit \
        -H "Authorization: Bearer $JWT_TOKEN" \
        -F "title=Route Test" \
        -F "description=Testing" \
        -F "start_time=2025-07-01T18:00" \
        -F "end_time=2025-07-01T19:00" \
        -F "location=Test" \
        -F "category=Other" \
        -F "bulletin_week=current" \
        -F "image=@test.txt")
    
    if echo $SUBMIT_TEST | grep -q success
        echo "✅ SUBMIT WORKING!"
        echo "🎉 ALL ROUTES FIXED!"
    else
        echo "❌ Submit still broken: $SUBMIT_TEST"
    end
    
    rm -f test.txt
else
    echo "❌ Build failed, restoring backup"
    cp src/main.rs.backup src/main.rs
end
