#!/bin/bash

echo "🔍 Xcode 安裝驗證工具"
echo "===================="

# Check if Xcode is installed
if [ -d "/Applications/Xcode.app" ]; then
    echo "✅ Xcode 應用程式已安裝"
    
    # Get Xcode version
    XCODE_VERSION=$(/Applications/Xcode.app/Contents/Developer/usr/bin/xcodebuild -version 2>/dev/null | head -1)
    if [ ! -z "$XCODE_VERSION" ]; then
        echo "📱 Xcode 版本: $XCODE_VERSION"
    fi
else
    echo "❌ Xcode 應用程式未安裝"
    echo "請使用以下方法安裝："
    echo "1. 打開 Mac App Store 搜尋 Xcode"
    echo "2. 或執行: mas install 497799835"
    exit 1
fi

# Check Xcode developer path
XCODE_PATH=$(xcode-select --print-path 2>/dev/null)
if [[ "$XCODE_PATH" == "/Applications/Xcode.app/Contents/Developer" ]]; then
    echo "✅ Xcode 開發者路徑設定正確"
else
    echo "⚠️  Xcode 開發者路徑需要設定"
    echo "請執行: sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer"
fi

# Check if first launch has been run
if xcodebuild -checkFirstLaunchStatus 2>/dev/null; then
    echo "✅ Xcode 首次啟動已完成"
else
    echo "⚠️  需要執行首次啟動設定"
    echo "請執行: sudo xcodebuild -runFirstLaunch"
fi

# Check license agreement
if xcodebuild -license check 2>/dev/null; then
    echo "✅ Xcode 授權條款已接受"
else
    echo "⚠️  需要接受 Xcode 授權條款"
    echo "請執行: sudo xcodebuild -license accept"
fi

# Check iOS simulators
SIMULATORS=$(xcrun simctl list devices available 2>/dev/null | grep "iOS" | wc -l)
if [ "$SIMULATORS" -gt 0 ]; then
    echo "✅ iOS 模擬器可用 ($SIMULATORS 個)"
else
    echo "⚠️  未找到 iOS 模擬器，請透過 Xcode 下載"
fi

# Check CocoaPods
if command -v pod &> /dev/null; then
    POD_VERSION=$(pod --version)
    echo "✅ CocoaPods 已安裝: v$POD_VERSION"
else
    echo "⚠️  CocoaPods 未安裝"
    echo "請執行: sudo gem install cocoapods"
fi

echo ""
echo "🔄 Flutter iOS 開發環境檢查:"
export PATH="/Users/jettcheng1018/flutter/bin:$PATH"
flutter doctor --doctor-check-ios