#!/bin/bash

# Fix iOS Xcode Project
# This script helps set up the iOS example project for Xcode

set -e

echo "🔧 Fixing iOS Xcode Project"
echo "==========================="

PROJECT_DIR="ios-example"
XCFRAMEWORK_NAME="ProofModeRust.xcframework"

cd "$PROJECT_DIR"

# Check if XCFramework exists
if [ -d "$XCFRAMEWORK_NAME" ]; then
    echo "✅ $XCFRAMEWORK_NAME found"
    
    # Validate framework structure
    if [ -f "$XCFRAMEWORK_NAME/Info.plist" ]; then
        echo "✅ XCFramework Info.plist present"
    else
        echo "❌ XCFramework Info.plist missing"
        exit 1
    fi
    
    # Check for arm64 framework
    if [ -d "$XCFRAMEWORK_NAME/ios-arm64/ProofModeRust.framework" ]; then
        echo "✅ Device framework present"
    else
        echo "❌ Device framework missing"
        exit 1
    fi
    
    # Check for simulator framework
    if [ -d "$XCFRAMEWORK_NAME/ios-arm64_x86_64-simulator/ProofModeRust.framework" ]; then
        echo "✅ Simulator framework present"
    else
        echo "❌ Simulator framework missing"
        exit 1
    fi
else
    echo "❌ $XCFRAMEWORK_NAME not found"
    exit 1
fi

echo ""
echo "📱 Framework Structure:"
echo "======================"
find "$XCFRAMEWORK_NAME" -type f -name "*.plist" -o -name "*.h" -o -name "*.swiftinterface" | sort

echo ""
echo "🔍 Checking Xcode project references..."

# The framework should be referenced in the project file
if grep -q "ProofModeRust.xcframework" "ProofModeExample.xcodeproj/project.pbxproj"; then
    echo "✅ Framework reference found in Xcode project"
else
    echo "❌ Framework reference missing in Xcode project"
fi

echo ""
echo "📋 iOS Setup Instructions:"
echo "=========================="
echo "1. Open ProofModeExample.xcodeproj in Xcode"
echo "2. The ProofModeRust framework should appear in the project navigator"
echo "3. If the framework appears red (missing):"
echo "   a. Right-click on ProofModeRust.xcframework in Xcode"
echo "   b. Select 'Show in Finder'"
echo "   c. Navigate to: $(pwd)/$XCFRAMEWORK_NAME"
echo "   d. Drag the framework back into Xcode"
echo ""
echo "4. Build Settings to verify:"
echo "   - Framework Search Paths: should include $(PROJECT_DIR)"
echo "   - Validate Workspace: should be 'Yes'"
echo "   - Build Active Architecture Only: 'No' for Release"
echo ""
echo "5. To build:"
echo "   - Select a simulator or device"
echo "   - Press Cmd+B or click Build"
echo ""
echo "6. If you still have issues:"
echo "   - Clean build folder (Cmd+Shift+K)"
echo "   - Delete DerivedData"
echo "   - Restart Xcode"
echo ""
echo "🎯 Current framework location:"
echo "$(pwd)/$XCFRAMEWORK_NAME"
echo ""
echo "✨ Mock framework is ready for development!"
echo "Note: This is a mock framework for development."
echo "To build the real framework, you'll need:"
echo "- macOS with Xcode"
echo "- Rust toolchain with iOS targets"
echo "- Run: cargo make build-ios"