#!/usr/bin/env bash
# Shared configuration and helpers for flexui build scripts.
# Source this at the top of every script:  source "$REPO_ROOT/scripts/_config.sh"
set -euo pipefail

# === Repository root (rely on REPO_ROOT being set by build.sh, or detect) ===
if [ -z "${REPO_ROOT:-}" ]; then
  REPO_ROOT="$(cd "$(dirname "$0")/.." && pwd)"
fi

# === Paths ===
ENTRY_DIR="$REPO_ROOT/flexui-engine/entry"
AGENT_DIR="$REPO_ROOT/ai-agent"
HAP="$ENTRY_DIR/build/default/outputs/default/entry-default-signed.hap"
AGENT_HAP="$AGENT_DIR/entry/build/default/outputs/default/entry-default-signed.hap"
# === DevEco Studio detection (cross-platform) ===
if [ -z "${DEVECO_SDK_HOME:-}" ]; then
  if [ -d "/Applications/DevEco-Studio.app" ]; then
    export DEVECO_SDK_HOME="/Applications/DevEco-Studio.app/Contents/sdk"
  elif [ -d "$PROGRAMFILES/Huawei/DevEco Studio" ]; then
    export DEVECO_SDK_HOME="$PROGRAMFILES/Huawei/DevEco Studio/sdk"
  elif [ -d "D:/Program Files/Huawei/DevEco Studio" ]; then
    export DEVECO_SDK_HOME="D:/Program Files/Huawei/DevEco Studio/sdk"
  elif [ -d "C:/Program Files/Huawei/DevEco Studio" ]; then
    export DEVECO_SDK_HOME="C:/Program Files/Huawei/DevEco Studio/sdk"
  fi
fi

# Find hvigorw (DevEco Studio bundles hvigor; probe common per-platform locations)
if [ -z "${HVIGORW_PATH:-}" ]; then
  for cand in \
    "/Applications/DevEco-Studio.app/Contents/tools/hvigor/bin/hvigorw" \
    "/Applications/DevEco Studio.app/Contents/tools/hvigor/bin/hvigorw" \
    "${DEVECO_SDK_HOME%/sdk}/tools/hvigor/bin/hvigorw" \
    "/c/Program Files/Huawei/DevEco Studio/tools/hvigor/bin/hvigorw" \
    "/d/Program Files/Huawei/DevEco Studio/tools/hvigor/bin/hvigorw"; do
    if [ -n "$cand" ] && [ -f "$cand" ]; then
      HVIGORW="$cand"
      break
    fi
  done
  HVIGORW="${HVIGORW:-hvigorw}"  # final PATH fallback
fi
HVIGORW_ARGS="--mode module -p module=entry@default -p product=default"
DEVICE="${DEVICE:-$(hdc list targets 2>/dev/null | head -1)}"
BUNDLE="${BUNDLE:-com.enjoy.now.hmos}"
WEBPACK_DIR="$REPO_ROOT/demos/ascf-demo"
RAW_FILE="$ENTRY_DIR/src/main/resources/rawfile/ascf/index.js"
ASCF_DEMO_DIR="$REPO_ROOT/demos/ascf-ai-demo"
ASCF_DEMO_RAW="$ASCF_DEMO_DIR/entry/src/main/resources/rawfile"
ASCF_AGENT_RAW="$AGENT_DIR/entry/src/main/resources/rawfile/ascf-ai-demo"
ASCF_CLI="$REPO_ROOT/toolkit/ascf-toolkit/lib/bin/ascf.js"

# === Shared Functions ===

# Sync compiled ASCF AI-demo rawfile → flexui-engine entry rawfile
# Usage: sync_ascf_rawfile
sync_ascf_rawfile() {
  local engine_ascf_dir="$ENTRY_DIR/src/main/resources/rawfile/ascf-ai-demo"
  if [ ! -d "$ASCF_DEMO_RAW" ]; then
    err "ASCF demo rawfile not found at: $ASCF_DEMO_RAW"
    return 1
  fi
  mkdir -p "$engine_ascf_dir"
  cp -r "$ASCF_DEMO_RAW/"* "$engine_ascf_dir/"
  log "Synced to $engine_ascf_dir/"
}

# Compile ASCF AI-demo using ASCF CLI.
# Usage: compile_ascf_ai_demo [mode]
#   mode: development (default) or release
#
# NOTE: ASCF CLI --mode is a boolean flag (no value).
#   --mode      → development
#   (no flag)   → production
# So passing --mode release is WRONG and produces debug builds.
compile_ascf_ai_demo() {
  local mode="${1:-development}"

  # ASCF CLI --mode is a boolean flag:
  #   --mode  → development
  #   (omit)  → production
  local mode_flag=""
  [ "$mode" = "development" ] && mode_flag="--mode"

  log "Compiling ASCF AI-demo (mode=$mode)..."

  node "$ASCF_CLI" compile "$ASCF_DEMO_DIR" $mode_flag 2>&1
  local exit_code=$?
  if [ "$exit_code" -eq 0 ]; then
    log "ASCF compile done (mode=$mode)"
  else
    err "ASCF compile failed (exit=$exit_code)"
    return "$exit_code"
  fi
}

# === Colors ===
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'

# === Helpers ===
log()  { echo -e "${GREEN}[$(date +%H:%M:%S)]${NC} $*"; }
warn() { echo -e "${YELLOW}[$(date +%H:%M:%S)] WARN${NC} $*"; }
err()  { echo -e "${RED}[$(date +%H:%M:%S)] ERROR${NC} $*"; }