#!/usr/bin/env bash
# ------------------------------------------------------------------------------
# H5-in-App-Integration SDK · Android 命令行构建入口
#
# 用法：
#   bash scripts/build-android-kotlin.sh              # = sdk：构建 webview-container AAR
#   bash scripts/build-android-kotlin.sh sdk          # 构建 webview-container Release AAR
#   bash scripts/build-android-kotlin.sh demo         # 构建 Demo APK (Debug)
#   bash scripts/build-android-kotlin.sh all          # sdk + demo 全量
#   bash scripts/build-android-kotlin.sh --help       # 查看本帮助
#
# 产物布局：
#   android-kotlin/webview-container/build/outputs/aar/webview-container-release.aar
#   android-kotlin/app/build/outputs/apk/debug/app-debug.apk
#
# 设计要点：
# - 使用 Gradle Wrapper（./gradlew），无需全局安装 Gradle
# - 双模块：:webview-container（SDK Library）/ :app（Demo Application）
# - 语言基线：Kotlin 1.9 / JDK 17
# - 无 flavor 区分（单一产物）
# ------------------------------------------------------------------------------
set -euo pipefail

# ---- 路径与常量 --------------------------------------------------------------
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)"
ANDROID_DIR="${REPO_ROOT}/android-kotlin"
GRADLEW="${ANDROID_DIR}/gradlew"

# 从 CHANGELOG 读取版本号
SDK_VERSION="$("${SCRIPT_DIR}/extract-version.sh")"
echo "ℹ️  SDK 版本号（来自 CHANGELOG）：${SDK_VERSION}"

# ---- 提前处理 --help ---------------------------------------------------------
case "${1:-}" in
  -h|--help)
    sed -n '2,22p' "${BASH_SOURCE[0]}"
    exit 0
    ;;
esac

# ---- 工具链就绪检查 ----------------------------------------------------------
if [[ ! -x "${GRADLEW}" ]]; then
  echo "❌ 找不到 gradlew：${GRADLEW}" >&2
  echo "   请确认 android-kotlin/ 目录下存在可执行的 gradlew 文件。" >&2
  exit 1
fi

# 检查 JAVA_HOME
if [[ -z "${JAVA_HOME:-}" ]]; then
  if [[ -x "/usr/libexec/java_home" ]]; then
    export JAVA_HOME="$(/usr/libexec/java_home 2>/dev/null || true)"
  fi
fi

if [[ -z "${JAVA_HOME:-}" ]] || [[ ! -d "${JAVA_HOME}" ]]; then
  echo "⚠️  JAVA_HOME 未设置或无效，Gradle 可能无法正常运行（需要 JDK 17+）。" >&2
fi

echo "ℹ️  Android 项目目录：${ANDROID_DIR}"
echo "ℹ️  Gradle Wrapper：${GRADLEW}"
echo ""

# ---- 构建函数 ----------------------------------------------------------------
build_sdk() {
  echo "▶ 构建 webview-container Release AAR ..."
  "${GRADLEW}" -p "${ANDROID_DIR}" :webview-container:assembleRelease --no-daemon -PsdkVersion="${SDK_VERSION}"
}

build_demo() {
  echo "▶ 构建 Demo App Debug APK ..."
  "${GRADLEW}" -p "${ANDROID_DIR}" :app:assembleDebug --no-daemon -PsdkVersion="${SDK_VERSION}"
}

# ---- 构建前清理上次产物（先清理再构建） --------------------------------------
echo "▶ 清理上次构建产物：webview-container/build、app/build"
rm -rf "${ANDROID_DIR}/webview-container/build" "${ANDROID_DIR}/app/build"
echo ""

# ---- 入口分发 ----------------------------------------------------------------
target="${1:-sdk}"
case "${target}" in
  sdk|"")
    build_sdk
    ;;
  demo)
    build_demo
    ;;
  all)
    build_sdk
    build_demo
    ;;
  *)
    echo "❌ 未知参数：${target}" >&2
    echo "用法：bash scripts/build-android-kotlin.sh [sdk|demo|all]" >&2
    exit 1
    ;;
esac

echo ""
echo "🎉 Android 构建完成！"
echo "   SDK 产物目录：${ANDROID_DIR}/webview-container/build/outputs/aar/"
find "${ANDROID_DIR}/webview-container/build/outputs/aar/" -name "*.aar" -exec ls -lh {} \; 2>/dev/null || true
