#!/usr/bin/env fish

echo "🔧 POPULATING IMAGE_PATH FIELD"
echo "=============================="

set API_BASE "https://api.rockvilletollandsda.church/api"
set UPLOAD_DIR "/opt/rtsda/church-api/uploads/events"

# Get token
set AUTH_RESPONSE (curl -s -X POST $API_BASE/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 "✅ Got token"

# Get all events
set EVENTS_RESPONSE (curl -s -H "Authorization: Bearer $JWT_TOKEN" "$API_BASE/events?perPage=500")
echo $EVENTS_RESPONSE | jq '.data.items' > events.json

set updated 0
set failed 0

echo "🔍 Updating events with proper image_path..."

for event in (cat events.json | jq -c '.[]')
    set event_id (echo $event | jq -r '.id')
    set title (echo $event | jq -r '.title')
    set current_image (echo $event | jq -r '.image // empty')
    set current_image_path (echo $event | jq -r '.image_path // empty')
    
    if test -z "$current_image"
        continue
    end
    
    # Look for the actual uploaded file
    set actual_file (find "$UPLOAD_DIR" -name "$event_id-*" -type f | head -1)
    
    if test -n "$actual_file"
        set correct_path (echo $actual_file | sed "s|$UPLOAD_DIR/|uploads/events/|")
        
        echo "📤 $title"
        echo "   Current image: $current_image"
        echo "   Setting image_path: $correct_path"
        
        # Get current event data
        set current_event (curl -s -H "Authorization: Bearer $JWT_TOKEN" "$API_BASE/events/$event_id")
        
        # Update with both image and image_path
        set event_data (echo $current_event | jq --arg img "$current_image" --arg imgpath "$correct_path" \
            '.data | {
                title: .title,
                description: .description,
                start_time: .start_time,
                end_time: .end_time,
                location: .location,
                location_url: .location_url,
                category: .category,
                recurring_type: .recurring_type,
                is_featured: .is_featured,
                image: $img,
                image_path: $imgpath
            }')
        
        set update_response (curl -s -X PUT \
            -H "Authorization: Bearer $JWT_TOKEN" \
            -H "Content-Type: application/json" \
            -d "$event_data" \
            "$API_BASE/admin/events/$event_id")
        
        set success (echo $update_response | jq -r '.success // false')
        
        if test "$success" = "true"
            echo "   ✅ SUCCESS"
            set updated (math $updated + 1)
        else
            echo "   ❌ FAILED"
            set failed (math $failed + 1)
        end
    else
        echo "❌ $title - no uploaded file found"
        set failed (math $failed + 1)
    end
    
    echo "---"
    sleep 0.1
end

rm -f events.json

echo ""
echo "🎉 RESULTS!"
echo "==========="
echo "✅ Updated image_path: $updated events"
echo "❌ Failed: $failed events"
echo ""
echo "Now the admin dashboard should use the proper image_path field!"
