#!/bin/bash

echo "🍎 完整的 Xcode 設定腳本"
echo "======================="

# Function to check if Xcode is installed
check_xcode_installation() {
    if [ -d "/Applications/Xcode.app" ]; then
        echo "✅ Xcode 已安裝"
        return 0
    else
        echo "❌ Xcode 尚未安裝"
        echo "請先從 Mac App Store 安裝 Xcode"
        return 1
    fi
}

# Function to configure Xcode developer tools
configure_xcode_tools() {
    echo "🔧 設定 Xcode 開發者工具..."
    
    # Set Xcode developer directory
    echo "設定 Xcode 開發者路徑..."
    sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer
    
    if [ $? -eq 0 ]; then
        echo "✅ Xcode 開發者路徑設定成功"
    else
        echo "❌ Xcode 開發者路徑設定失敗"
        return 1
    fi
    
    # Run first launch
    echo "執行 Xcode 首次啟動設定..."
    sudo xcodebuild -runFirstLaunch
    
    if [ $? -eq 0 ]; then
        echo "✅ Xcode 首次啟動設定完成"
    else
        echo "❌ Xcode 首次啟動設定失敗"
        return 1
    fi
    
    # Accept license
    echo "接受 Xcode 授權條款..."
    sudo xcodebuild -license accept
    
    if [ $? -eq 0 ]; then
        echo "✅ Xcode 授權條款已接受"
    else
        echo "❌ Xcode 授權條款接受失敗"
        return 1
    fi
}

# Function to install CocoaPods if not present
install_cocoapods() {
    if command -v pod &> /dev/null; then
        echo "✅ CocoaPods 已存在"
    else
        echo "🔧 安裝 CocoaPods..."
        sudo gem install cocoapods
        
        if [ $? -eq 0 ]; then
            echo "✅ CocoaPods 安裝成功"
        else
            echo "❌ CocoaPods 安裝失敗"
            return 1
        fi
    fi
}

# Function to configure Flutter for iOS
configure_flutter_ios() {
    echo "🐦 設定 Flutter iOS 開發環境..."
    
    # Add Flutter to PATH for this session
    export PATH="/Users/jettcheng1018/flutter/bin:$PATH"
    
    # Run Flutter doctor to check iOS setup
    flutter doctor --doctor-ios-only 2>/dev/null || flutter doctor
    
    echo "✅ Flutter iOS 設定檢查完成"
}

# Function to verify the complete setup
verify_setup() {
    echo "🔍 驗證完整設定..."
    
    # Run the verification script
    if [ -f "./tools/environment/xcode/xcode_verification.sh" ]; then
        ./tools/environment/xcode/xcode_verification.sh
    else
        echo "⚠️  驗證腳本不存在，執行基本檢查..."
        
        # Basic verification
        if [ -d "/Applications/Xcode.app" ]; then
            echo "✅ Xcode 已安裝"
            
            XCODE_PATH=$(xcode-select --print-path)
            if [[ "$XCODE_PATH" == "/Applications/Xcode.app/Contents/Developer" ]]; then
                echo "✅ Xcode 開發者路徑正確"
            else
                echo "❌ Xcode 開發者路徑不正確: $XCODE_PATH"
            fi
        else
            echo "❌ Xcode 未安裝"
        fi
    fi
}

# Function to update project status
update_project_status() {
    echo "📝 更新專案狀態..."
    
    # Update PROJECTS.md
    if [ -f "PROJECTS.md" ]; then
        # Replace the status line for Xcode installation
        if sed -i.bak 's/⏳ `ENV` Xcode 安裝配置 (iOS支援)/✅ `ENV` Xcode 安裝配置 (iOS支援) (2025-09-08)/' PROJECTS.md; then
            echo "✅ 專案狀態已更新"
        else
            echo "⚠️  專案狀態更新可能失敗"
        fi
    else
        echo "⚠️  PROJECTS.md 檔案不存在"
    fi
}

# Main execution flow
main() {
    echo "開始 Xcode 完整設定流程..."
    echo ""
    
    # Check if Xcode is installed
    if ! check_xcode_installation; then
        echo "請先安裝 Xcode 然後再次執行此腳本"
        exit 1
    fi
    
    # Configure Xcode tools
    if ! configure_xcode_tools; then
        echo "Xcode 工具設定失敗"
        exit 1
    fi
    
    # Install CocoaPods
    if ! install_cocoapods; then
        echo "CocoaPods 安裝失敗"
        exit 1
    fi
    
    # Configure Flutter for iOS
    configure_flutter_ios
    
    # Verify setup
    verify_setup
    
    # Update project status
    update_project_status
    
    echo ""
    echo "🎉 Xcode 設定完成！"
    echo "現在您可以開始 iOS 開發了"
}

# Run main function
main