#!/bin/zsh
set -o pipefail

# --- Configuration ---
SCHEME="BeMyEars"
DEVICE_NAME="iPhone 17 Pro" 
# Falling back to 16 Pro as 17 might not be in all sim runtimes yet, 
# but easy to change if needed. 
BUILD_PATH="./build"

echo "🔍 Checking compilation for $SCHEME..."

# Ensure xcbeautify is installed or fallback to cat
if ! command -v xcbeautify &> /dev/null; then
    echo "⚠️ xcbeautify not found, using plain output."
    FORMATTER="cat"
else
    FORMATTER="xcbeautify"
fi

# Build Only
env -u CC -u CXX -u LIBCLANG_PATH xcodebuild \
  -workspace "BeMyEars.xcworkspace" \
  -scheme "$SCHEME" \
  -destination "platform=iOS Simulator,name=$DEVICE_NAME" \
  -configuration Debug \
  -derivedDataPath "$BUILD_PATH" \
  build | $FORMATTER

# Check exit code
if [ $? -eq 0 ]; then
    echo "✅ Build Succeeded. No errors found."
else
    echo "❌ Build Failed."
    exit 1
fi
