#!/bin/bash

echo "🔧 Fixing Firebase configuration for iOS..."

# Create directories if they don't exist
mkdir -p platforms/ios/Todo/Resources

# Copy GoogleService-Info.plist if it exists in root
if [ -f "GoogleService-Info.plist" ]; then
    echo "📋 Copying GoogleService-Info.plist to iOS Resources..."
    cp GoogleService-Info.plist platforms/ios/Todo/Resources/
    echo "✅ GoogleService-Info.plist copied successfully"
else
    echo "⚠️  GoogleService-Info.plist not found in root directory"
fi

# Fix CocoaPods deployment target issues
if [ -d "platforms/ios" ]; then
    echo "🔧 Fixing CocoaPods deployment target..."
    cd platforms/ios
    
    # Update Podfile to use minimum deployment target 17.0
    if [ -f "Podfile" ]; then
        echo "📝 Updating Podfile deployment target to 17.0..."
        sed -i '' "s/platform :ios, '[0-9]*\.[0-9]*'/platform :ios, '17.0'/g" Podfile 2>/dev/null || true
        
        # Clean and reinstall pods
        echo "🧹 Cleaning CocoaPods cache..."
        rm -rf Pods Podfile.lock 2>/dev/null || true
        
        echo "📦 Installing CocoaPods with updated deployment target..."
        pod install --repo-update 2>/dev/null || pod install || true
    fi
    
    cd ../..
fi

# Remove any conflicting files that might cause plugin installation issues
echo "🧹 Cleaning up potential conflicts..."
rm -f platforms/ios/Todo/Resources/GoogleService-Info.plist.backup 2>/dev/null || true

# Check if the file was copied correctly
if [ -f "platforms/ios/Todo/Resources/GoogleService-Info.plist" ]; then
    echo "✅ Firebase configuration fixed successfully!"
    echo "📍 File location: platforms/ios/Todo/Resources/GoogleService-Info.plist"
else
    echo "❌ Failed to copy GoogleService-Info.plist"
    exit 1
fi
