#!/usr/bin/env bash
set -euo pipefail

ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
COMPOSE_FILE="$ROOT_DIR/docker-compose.yaml"
USERFRONT_DOCKERFILE="$ROOT_DIR/userfront/Dockerfile"
USERFRONT_DEV_SERVER="$ROOT_DIR/userfront/scripts/dev-server.sh"

fail() {
  echo "ERROR: $*" >&2
  exit 1
}

assert_contains() {
  local pattern="$1"
  grep -Fq -- "$pattern" "$COMPOSE_FILE" || fail "docker-compose.yaml must contain: $pattern"
}

assert_not_contains() {
  local pattern="$1"
  if grep -Fq -- "$pattern" "$COMPOSE_FILE"; then
    fail "docker-compose.yaml must not contain stale frontend mount: $pattern"
  fi
}

for app in adminfront devfront orgfront; do
  assert_contains "./$app:/workspace/$app"
  assert_contains "/workspace/$app/node_modules"
  assert_not_contains "./$app:/app"
done

assert_contains 'target: ${USERFRONT_BUILD_TARGET:-dev}'
assert_contains "./userfront/lib:/workspace/userfront/lib"
assert_contains "./userfront/assets:/workspace/userfront/assets"
assert_contains "./userfront/web:/workspace/userfront/web"
assert_contains "./userfront/scripts:/workspace/userfront/scripts:ro"
assert_contains "./scripts:/workspace/scripts:ro"
assert_contains "./locales:/workspace/locales:ro"
grep -Fq -- "AS dev" "$USERFRONT_DOCKERFILE" || fail "userfront Dockerfile must define a dev build target"
grep -Fq -- "AS production" "$USERFRONT_DOCKERFILE" || fail "userfront Dockerfile must keep an explicit production target"
grep -Fq -- "flutter run" "$USERFRONT_DEV_SERVER" || fail "userfront dev server must use flutter run"
grep -Fq -- "--wasm" "$USERFRONT_DEV_SERVER" || fail "userfront dev server must keep WebAssembly enabled"
grep -Fq -- "--dart-define=CLIENT_LOG_DEBUG=" "$USERFRONT_DEV_SERVER" || fail "userfront dev server must pass client log debug mode through dart-define"
grep -Fq -- "--dart-define=APP_ENV=" "$USERFRONT_DEV_SERVER" || fail "userfront dev server must pass app env through dart-define"
grep -Fq -- 'USERFRONT_FLUTTER_RUN_FLAGS' "$USERFRONT_DEV_SERVER" || fail "userfront dev server must accept optional Flutter run flags"
assert_contains 'CLIENT_LOG_DEBUG=${CLIENT_LOG_DEBUG:-false}'
assert_contains 'USERFRONT_FLUTTER_RUN_FLAGS=${USERFRONT_FLUTTER_RUN_FLAGS:-}'
if grep -Fq -- "--debug" "$USERFRONT_DEV_SERVER"; then
  fail "make dev must not hard-code Flutter debug mode in the userfront dev server"
fi
if grep -Fq -- "--release" "$USERFRONT_DEV_SERVER"; then
  fail "userfront dev server must not run Flutter in release mode"
fi

assert_contains "./common:/workspace/common"
assert_contains "/workspace/common/node_modules"
assert_contains "./locales:/workspace/locales"

for runtime in \
  "$ROOT_DIR/adminfront/scripts/runtime-mode.sh" \
  "$ROOT_DIR/devfront/scripts/runtime-mode.sh" \
  "$ROOT_DIR/orgfront/scripts/runtime-mode.sh"
do
  grep -Fq -- "/workspace/common" "$runtime" || fail "$runtime must install dependencies from /workspace/common"
  grep -Fq -- "pnpm install --filter" "$runtime" || fail "$runtime must install only its workspace slice"
  grep -Fq -- "--frozen-lockfile --ignore-scripts" "$runtime" || fail "$runtime must preserve the workspace lockfile with pnpm"
  if grep -Fq -- "npm install --no-workspaces" "$runtime"; then
    fail "$runtime must not install common dependencies outside the workspace graph"
  fi
done

echo "OK: frontend dev containers bind-mount source into Dockerfile WORKDIR paths"
