#!/usr/bin/env fish
echo "🔧 COMPLETE IMAGE_PATH FIX (v5 - SUBMISSION + UPDATES)"
echo "====================================================="

# Restore from backups first
if test -f "src/models.rs.backup2"
    cp src/models.rs.backup2 src/models.rs
    cp src/db/events.rs.backup2 src/db/events.rs
    cp src/handlers/events.rs.backup2 src/handlers/events.rs
    echo "✅ Restored from backups"
end

echo "1️⃣ Adding image_path to BOTH CreateEventRequest AND SubmitEventRequest..."

# Add to CreateEventRequest
sed -i '/pub struct CreateEventRequest {/,/^}/ {
    /pub recurring_type: Option<String>,/ a\
    pub image_path: Option<String>,
}' src/models.rs

# Add to SubmitEventRequest  
sed -i '/pub struct SubmitEventRequest {/,/^}/ {
    /pub submitter_email: Option<String>,/ a\
    pub image_path: Option<String>,
}' src/models.rs

echo "2️⃣ Adding image_path to SubmitEventRequest initialization in handlers..."
sed -i '/let mut req = SubmitEventRequest {/,/};/ {
    /submitter_email: None,/ a\
        image_path: None,
}' src/handlers/events.rs

echo "3️⃣ Fixing the submit_for_approval function SQL..."
# Update the INSERT statement to include image_path
sed -i 's|category, is_featured, recurring_type, bulletin_week, submitter_email|category, is_featured, recurring_type, bulletin_week, submitter_email, image_path|' src/db/events.rs
sed -i 's|VALUES (\$1, \$2, \$3, \$4, \$5, \$6, \$7, \$8, \$9, \$10, \$11)|VALUES (\$1, \$2, \$3, \$4, \$5, \$6, \$7, \$8, \$9, \$10, \$11, \$12)|' src/db/events.rs

echo "4️⃣ Fixing the events table update function..."
sed -i 's|recurring_type = \$9, updated_at = NOW()|recurring_type = \$9, image_path = \$10, updated_at = NOW()|' src/db/events.rs
sed -i 's|WHERE id = \$10|WHERE id = \$11|' src/db/events.rs
sed -i '/req\.recurring_type,$/a\        req.image_path,' src/db/events.rs

echo "5️⃣ Building..."
if cargo build
    echo "✅ SUCCESS! Both submission and updates now support image_path."
    echo ""
    echo "=== What was fixed ==="
    echo "✅ Added image_path to CreateEventRequest struct (for updates)"
    echo "✅ Added image_path to SubmitEventRequest struct (for new submissions)"
    echo "✅ Updated handlers to initialize image_path field"
    echo "✅ Fixed submit_for_approval SQL to include image_path column"
    echo "✅ Fixed update SQL to include image_path column"
    echo ""
    echo "🚀 Next steps:"
    echo "1. Restart your API server"
    echo "2. Run your image_path update script"
    echo "3. Both new submissions AND existing event updates will handle image_path!"
else
    echo "❌ Build failed. Let's debug..."
    echo ""
    echo "=== Current structs ==="
    grep -A 15 "pub struct CreateEventRequest" src/models.rs
    echo ""
    grep -A 15 "pub struct SubmitEventRequest" src/models.rs
    echo ""
    echo "=== Current submit_for_approval function ==="
    grep -A 15 "submit_for_approval.*SubmitEventRequest" src/db/events.rs
    
    # Restore
    cp src/models.rs.backup2 src/models.rs
    cp src/db/events.rs.backup2 src/db/events.rs
    cp src/handlers/events.rs.backup2 src/handlers/events.rs
    echo "🔄 Restored backups"
end
