#!/usr/bin/env bash

# Build iOS Unity player (generates Xcode project)

set -euxo pipefail

# Read Unity version from project file
UNITY_VERSION=$(grep "m_EditorVersion:" src/main/csharp/net/eagle0/clients/unity/eagle0/ProjectSettings/ProjectVersion.txt | head -1 | sed 's/m_EditorVersion: //')

# Use runner-specific build directory if EAGLE0_BUILD_DIR is set, otherwise default
BUILD_BASE="${EAGLE0_BUILD_DIR:-/tmp/eagle0}"

WORKSPACE=$(pwd)
BUILD_PATH=${1:-"${BUILD_BASE}/eagle0iOS"}
BUILD_ADDRESSABLES=${2:-true}
ADDRESSABLES_BUILD_LAYOUT=${3:-$BUILD_ADDRESSABLES}
LOG_PATH="${BUILD_BASE}/editor_ios.log"

# Generate unique build number from git commit count
# This ensures each build has a unique number for TestFlight
BUILD_NUMBER=$(git rev-list --count HEAD)
echo "Build number (git commit count): $BUILD_NUMBER"

echo "Building protos"
./scripts/build_protos.sh

UNITY_INSTALL_PATH="/Applications/Unity/Hub/Editor"

echo "Building iOS Unity player to: $BUILD_PATH"
echo "Build Addressables: $BUILD_ADDRESSABLES"
echo "Generate Addressables build layout: $ADDRESSABLES_BUILD_LAYOUT"

mkdir -p "$(dirname "$LOG_PATH")"
mkdir -p "$BUILD_PATH"

# Build iOS player - this generates an Xcode project
# Capture exit code to show log on failure
set +e
${UNITY_INSTALL_PATH}/${UNITY_VERSION}/Unity.app/Contents/MacOS/Unity \
   -nographics \
   -batchmode \
   -quit \
   -executeMethod BuildScript.BuildiOSPlayer \
   -buildPath "$BUILD_PATH" \
   -buildNumber "$BUILD_NUMBER" \
   -buildAddressables "$BUILD_ADDRESSABLES" \
   -addressablesBuildLayout "$ADDRESSABLES_BUILD_LAYOUT" \
   -logFile "$LOG_PATH" \
   -projectPath "$WORKSPACE/src/main/csharp/net/eagle0/clients/unity/eagle0"
UNITY_EXIT_CODE=$?
set -e

if [ $UNITY_EXIT_CODE -ne 0 ]; then
    echo ""
    echo "Unity build failed with exit code $UNITY_EXIT_CODE"
    echo "=== Unity Editor Log (last 200 lines) ==="
    tail -200 "$LOG_PATH" || echo "Could not read log file at $LOG_PATH"
    echo "=== End of Unity Editor Log ==="
    exit $UNITY_EXIT_CODE
fi

if [ ! -f "$LOG_PATH" ]; then
    echo ""
    echo "ERROR: Unity exited successfully but did not write an editor log at $LOG_PATH"
    exit 1
fi

if [ ! -f "$BUILD_PATH/Data/Raw/aa/settings.json" ]; then
    echo ""
    echo "ERROR: iOS player is missing Addressables runtime data at:"
    echo "  $BUILD_PATH/Data/Raw/aa/settings.json"
    echo "Build Addressables before packaging the player so Addressables can initialize at runtime."
    exit 1
fi

# Fail the build if any prefab references are broken — this produces a player
# that launches but has null Inspector fields, which is hard to debug.
if grep -q "Missing Prefab" "$LOG_PATH"; then
    echo ""
    echo "ERROR: Build has missing prefab references (likely stale Library cache):"
    grep "Missing Prefab" "$LOG_PATH"
    exit 1
fi

if grep -q "Build asset version error" "$LOG_PATH"; then
    echo ""
    echo "ERROR: Build has asset version mismatches (likely stale Library cache):"
    grep "Build asset version error" "$LOG_PATH" | head -5
    exit 1
fi

echo "iOS Unity build complete"
echo "Xcode project generated at: $BUILD_PATH"
ls -la "$BUILD_PATH"
