#!/usr/bin/env bash

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
APP_DIR="$REPO_ROOT/apps/chanora_flutter"
OUTPUT_DIR="$APP_DIR/build/app/outputs/flutter-apk"
DIST_DIR="$APP_DIR/build/app/outputs/release-dist"
PUBSPEC_PATH="$APP_DIR/pubspec.yaml"
ANDROID_SDK_ROOT_DEFAULT="$HOME/Library/Android/sdk"

resolve_apksigner() {
  local sdk_root="${ANDROID_SDK_ROOT:-${ANDROID_HOME:-$ANDROID_SDK_ROOT_DEFAULT}}"
  local build_tools_dir="$sdk_root/build-tools"
  if [[ ! -d "$build_tools_dir" ]]; then
    echo "Android build-tools directory not found: $build_tools_dir" >&2
    exit 1
  fi

  local latest_build_tools
  latest_build_tools="$(find "$build_tools_dir" -mindepth 1 -maxdepth 1 -type d | sort -V | tail -n 1)"
  if [[ -z "$latest_build_tools" ]]; then
    echo "No Android build-tools versions found under: $build_tools_dir" >&2
    exit 1
  fi

  local apksigner_path="$latest_build_tools/apksigner"
  if [[ ! -x "$apksigner_path" ]]; then
    echo "apksigner not found or not executable: $apksigner_path" >&2
    exit 1
  fi

  echo "$apksigner_path"
}

if ! command -v flutter >/dev/null 2>&1; then
  echo "flutter not found on PATH" >&2
  exit 1
fi

if [[ ! -f "$PUBSPEC_PATH" ]]; then
  echo "pubspec.yaml not found at $PUBSPEC_PATH" >&2
  exit 1
fi

if [[ ! -f "$APP_DIR/android/key.properties" ]]; then
  echo "Missing signing config: $APP_DIR/android/key.properties" >&2
  exit 1
fi

APKSIGNER_BIN="$(resolve_apksigner)"

VERSION_LINE="$(grep '^version:' "$PUBSPEC_PATH" | head -n 1 | awk '{print $2}')"
if [[ -z "${VERSION_LINE:-}" ]]; then
  echo "Could not parse version from $PUBSPEC_PATH" >&2
  exit 1
fi

VERSION_NAME="${VERSION_LINE%%+*}"
BUILD_NUMBER="${VERSION_LINE##*+}"
APP_NAME="chanora"

mkdir -p "$DIST_DIR"

build_one() {
  local target_platform="$1"
  local abi_label="$2"
  local release_path_flutter="$OUTPUT_DIR/app-release.apk"
  local release_path_gradle="$APP_DIR/build/app/outputs/apk/release/app-release.apk"
  local named_apk="$DIST_DIR/${APP_NAME}-${VERSION_NAME}+${BUILD_NUMBER}-${abi_label}-release.apk"

  rm -f "$release_path_flutter" "$release_path_gradle"
  (
    cd "$APP_DIR"
    flutter build apk --release --target-platform "$target_platform" --tree-shake-icons >&2
  )

  local built_apk=""
  if [[ -f "$release_path_flutter" ]]; then
    built_apk="$release_path_flutter"
  elif [[ -f "$release_path_gradle" ]]; then
    built_apk="$release_path_gradle"
  fi

  if [[ -z "$built_apk" ]]; then
    echo "Expected release APK missing: $release_path_flutter" >&2
    exit 1
  fi

  cp "$built_apk" "$named_apk"
  "$APKSIGNER_BIN" verify --print-certs "$named_apk" >/dev/null
  echo "$named_apk"
}

ARM64_APK="$(build_one android-arm64 arm64-v8a)"
echo "Built: $ARM64_APK"

if rustup target list --installed | grep -qx 'x86_64-linux-android'; then
  X64_APK="$(build_one android-x64 x86_64)"
  echo "Built: $X64_APK"
else
  echo "Skipped x86_64 build: rustup target x86_64-linux-android is not installed"
fi
