#!/usr/bin/env sh
set -eu

assert_mode() {
  script_path="$1"
  app_env="$2"
  expected="$3"
  actual="$(APP_ENV="$app_env" sh "$script_path" --print-mode)"
  if [ "$actual" != "$expected" ]; then
    echo "script=$script_path APP_ENV=$app_env expected mode=$expected got=$actual" >&2
    exit 1
  fi
}

for script in \
  "./adminfront/scripts/runtime-mode.sh" \
  "./devfront/scripts/runtime-mode.sh" \
  "./orgfront/scripts/runtime-mode.sh"
do
  if ! grep -Fq "ensure_frontend_dependencies" "$script"; then
    echo "script=$script must sync frontend dependencies before start" >&2
    exit 1
  fi
  if ! grep -Fq "package-lock.json" "$script"; then
    echo "script=$script must use package-lock.json for dependency sync" >&2
    exit 1
  fi
  assert_mode "$script" "production" "production"
  assert_mode "$script" "prod" "production"
  assert_mode "$script" "stage" "production"
  assert_mode "$script" "staging" "production"
  assert_mode "$script" "development" "development"
  assert_mode "$script" "dev" "development"
done

echo "frontend runtime mode checks passed"
