#!/bin/bash

set -euo pipefail

SCRIPT_DIR="$(dirname "$(realpath "$0")")"
REPO_ROOT="$SCRIPT_DIR/.."
cd "$REPO_ROOT"

EXCLUDES_FILE="api/pyrefly-local-excludes.txt"
TEST_CONTAINERS_DIR="tests/test_containers_integration_tests"
TEST_CONTAINERS_CONFIG="$TEST_CONTAINERS_DIR/pyrefly.toml"

target_paths=()
for target_path in "$@"; do
  if [[ "$target_path" == api/* ]]; then
    target_paths+=("${target_path#api/}")
  else
    target_paths+=("$target_path")
  fi
done

pyrefly_args=(
  "--summary=none"
  "--use-ignore-files=false"
  "--disable-project-excludes-heuristics=true"
  "--project-excludes=.venv"
  "--project-excludes=migrations/"
  "--project-excludes=tests/"
)

if [[ "${PYREFLY_OUTPUT_FORMAT:-}" == "github" ]]; then
  pyrefly_args+=("--output-format=github")
fi

if [[ -f "$EXCLUDES_FILE" ]]; then
  while IFS= read -r exclude; do
    [[ -z "$exclude" || "${exclude:0:1}" == "#" ]] && continue
    pyrefly_args+=("--project-excludes=$exclude")
  done < "$EXCLUDES_FILE"
fi

run_pyrefly() {
  if [[ "${PYREFLY_OUTPUT_FORMAT:-}" == "github" ]]; then
    set +e
    "$@"
    local pyrefly_status=$?
    set -e
    return "$pyrefly_status"
  fi

  local tmp_output
  tmp_output="$(mktemp)"

  set +e
  "$@" >"$tmp_output" 2>&1
  local pyrefly_status=$?
  set -e

  uv run --directory api python libs/pyrefly_diagnostics.py --status "$pyrefly_status" < "$tmp_output"
  rm -f "$tmp_output"
  return "$pyrefly_status"
}

status=0

pyrefly_command=(
  uv run --directory api --dev pyrefly check
  "${pyrefly_args[@]}"
)
if (( ${#target_paths[@]} > 0 )); then
  pyrefly_command+=("${target_paths[@]}")
fi

run_pyrefly "${pyrefly_command[@]}" || status=$?

if (( ${#target_paths[@]} == 0 )); then
  test_containers_args=(
    "--summary=none"
    "--use-ignore-files=false"
    "--config=$TEST_CONTAINERS_CONFIG"
  )
  if [[ "${PYREFLY_OUTPUT_FORMAT:-}" == "github" ]]; then
    test_containers_args+=("--output-format=github")
  fi
  run_pyrefly \
    uv run --directory api --dev pyrefly check \
    "${test_containers_args[@]}" \
    || status=$?
fi

exit "$status"
