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

repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"

dry_run_default_dev="$(
  make --dry-run --always-make -C "$repo_root" dev 2>&1
)"

default_app_up_line="$(
  grep -E "docker compose .* -f docker-compose.yaml up .*backend.*adminfront.*devfront.*orgfront.*userfront" <<<"$dry_run_default_dev" | tail -1
)"

if [[ -z "$default_app_up_line" ]]; then
  echo "make dev must include orgfront in the default development app services." >&2
  exit 1
fi

dry_run_dev="$(
  make --dry-run --always-make -C "$repo_root" dev DEV_SERVICES="backend adminfront" 2>&1
)"

if ! grep -q "Ensuring Infra stack" <<<"$dry_run_dev"; then
  echo "make dev must ensure the infra stack first." >&2
  exit 1
fi

if ! grep -q "Ensuring Ory stack" <<<"$dry_run_dev"; then
  echo "make dev must ensure the Ory stack first." >&2
  exit 1
fi

if ! grep -q "Rendering Ory config" <<<"$dry_run_dev"; then
  echo "make dev must render Ory config before starting services." >&2
  exit 1
fi

app_up_line="$(
  grep -E "docker compose .* -f docker-compose.yaml up .*backend.*adminfront" <<<"$dry_run_dev" | tail -1
)"

if [[ -z "$app_up_line" ]]; then
  echo "make dev must run docker compose up for development app services." >&2
  exit 1
fi

if grep -q -- " -d" <<<"$app_up_line"; then
  echo "make dev must run app services in foreground attach mode without -d." >&2
  exit 1
fi

if ! grep -q -- " --build" <<<"$app_up_line"; then
  echo "make dev must rebuild app service images before starting development containers." >&2
  exit 1
fi

if ! grep -q -- "BACKEND_LOG_LEVEL=info" <<<"$app_up_line"; then
  echo "make dev must run backend at info log level." >&2
  exit 1
fi

if ! grep -q -- "CLIENT_LOG_DEBUG=false" <<<"$app_up_line"; then
  echo "make dev must disable verbose client debug log ingestion." >&2
  exit 1
fi

if ! grep -q -- "VITE_CLIENT_LOG_DEBUG=false" <<<"$app_up_line"; then
  echo "make dev must disable React client debug console logs." >&2
  exit 1
fi

if grep -q -- "BACKEND_LOG_LEVEL=debug" <<<"$app_up_line"; then
  echo "make dev must not run backend at debug log level." >&2
  exit 1
fi

if grep -q -- "USERFRONT_FLUTTER_RUN_FLAGS=--debug" <<<"$app_up_line"; then
  echo "make dev must not run userfront with explicit Flutter debug flags." >&2
  exit 1
fi

dry_run_dev_debug="$(
  make --dry-run --always-make -C "$repo_root" dev-debug DEV_SERVICES="backend userfront" 2>&1
)"

if ! grep -q "Ensuring Infra stack" <<<"$dry_run_dev_debug"; then
  echo "make dev-debug must ensure the infra stack first." >&2
  exit 1
fi

if ! grep -q "Ensuring Ory stack" <<<"$dry_run_dev_debug"; then
  echo "make dev-debug must ensure the Ory stack first." >&2
  exit 1
fi

dev_debug_app_up_line="$(
  grep -E "BACKEND_LOG_LEVEL=debug CLIENT_LOG_DEBUG=true VITE_CLIENT_LOG_DEBUG=true USERFRONT_FLUTTER_RUN_FLAGS=--debug docker compose .* -f docker-compose.yaml up .*backend.*userfront" <<<"$dry_run_dev_debug" | tail -1
)"

if [[ -z "$dev_debug_app_up_line" ]]; then
  echo "make dev-debug must run app services with explicit backend, client log, and userfront debug flags." >&2
  exit 1
fi

dry_run_up_dev="$(
  make --dry-run --always-make -C "$repo_root" up-dev 2>&1
)"

if ! grep -q "Ensuring Infra stack" <<<"$dry_run_up_dev"; then
  echo "make up-dev must ensure the infra stack." >&2
  exit 1
fi

if ! grep -q "Ensuring Ory stack" <<<"$dry_run_up_dev"; then
  echo "make up-dev must ensure the Ory stack." >&2
  exit 1
fi

dry_run_up_app="$(
  make --dry-run --always-make -C "$repo_root" up-app 2>&1
)"

if ! grep -q "Starting App stack (backend/userfront/adminfront/devfront/orgfront)" <<<"$dry_run_up_app"; then
  echo "make up-app must announce orgfront as part of the app stack." >&2
  exit 1
fi

if ! grep -q "Rendering Ory config" <<<"$dry_run_up_app"; then
  echo "make up-app must render Ory config before starting services." >&2
  exit 1
fi

up_app_line="$(
  grep -E "docker compose .* -f docker-compose.yaml up .*backend.*adminfront.*devfront.*orgfront.*userfront|docker compose .* -f docker-compose.yaml up " <<<"$dry_run_up_app" | tail -1
)"

if ! grep -q -- " --build" <<<"$up_app_line"; then
  echo "make up-app must rebuild app service images before starting containers." >&2
  exit 1
fi

dry_run_up_all="$(
  make --dry-run --always-make -C "$repo_root" up-all 2>&1
)"

if ! dry_run_up="$(
  make --dry-run --always-make -C "$repo_root" up 2>&1
)"; then
  echo "make up must be available as the default full-stack startup target." >&2
  echo "$dry_run_up" >&2
  exit 1
fi

if ! grep -q "Starting ALL stacks (infra + ory + app)" <<<"$dry_run_up"; then
  echo "make up must delegate to the full-stack startup flow." >&2
  exit 1
fi

if ! grep -q "config/.generated/auth-config.env" <<<"$dry_run_up"; then
  echo "make up must use generated env from config/.generated." >&2
  exit 1
fi

if ! grep -q "Rendering Ory config" <<<"$dry_run_up"; then
  echo "make up must render Ory config before compose up." >&2
  exit 1
fi

if ! grep -q "Ensuring Docker networks" <<<"$dry_run_up_all"; then
  echo "make up-all must ensure external Docker networks before compose up." >&2
  exit 1
fi

if ! grep -q 'docker network create "$network"' <<<"$dry_run_up_all"; then
  echo "make up-all must create missing external Docker networks." >&2
  exit 1
fi

dry_run_drop="$(
  make --dry-run --always-make -C "$repo_root" drop 2>&1
)"

if ! grep -q "Dropping Baron SSO local Docker stack" <<<"$dry_run_drop"; then
  echo "make drop must announce that it is dropping the local stack." >&2
  exit 1
fi

if ! grep -q -- "down -v --rmi local" <<<"$dry_run_drop"; then
  echo "make drop must remove containers, volumes, and local compose images." >&2
  exit 1
fi

if ! grep -q "docker rm -f" <<<"$dry_run_drop"; then
  echo "make drop must force-remove known fixed-name stack containers." >&2
  exit 1
fi
