#!/bin/sh
set -eu

# Execute QA commands for a given profile
# Usage: qa-run <profile> [-- target1 target2 ...]
# Profiles: docs, focused, subsystem, catalog, analysis, exports, runtime, full

if [ $# -lt 1 ]; then
  printf 'usage: qa-run <profile> [-- targets...]\n' >&2
  exit 2
fi

profile="$1"
shift
root=$(git rev-parse --show-toplevel)

# Parse arguments: optional -- is $1, targets are the rest
if [ $# -gt 0 ] && [ "$1" = "--" ]; then
  shift
fi

# Create temp directory for logs (subshell-wrapped for isolation)
(
  logdir=$(mktemp -d)
  cleanup_on_exit=1
  trap '[ "$cleanup_on_exit" = 1 ] && rm -rf "$logdir"' EXIT

  status=0
  cmd=""

  case "$profile" in
    docs)
      # docs profile: git diff --check only
      cmd="git diff --check"
      git diff --check > "$logdir/docs.log" 2>&1 || status=$?
      ;;

    focused)
      # focused profile: run pytest targets or shell scripts passed after --
      if [ $# -eq 0 ]; then
        printf 'qa-run focused: no targets provided after --\n' >&2
        exit 2
      fi

      # Process each target, one log file per target
      i=0
      for target in "$@"; do
        i=$((i + 1))
        if [ -f "$target" ]; then
          case "$target" in
            *.sh)
              # Shell script: run directly
              cmd="sh $target"
              sh "$target" > "$logdir/focused.$i.log" 2>&1 || status=$?
              ;;
            *.py)
              # Python test file: run via pytest through project-env
              cmd="uv run pytest $target"
              "$root/.agents/scripts/project-env" uv run pytest "$target" > "$logdir/focused.$i.log" 2>&1 || status=$?
              ;;
            *)
              printf 'qa-run focused: unknown target type: %s\n' "$target" >&2
              exit 2
              ;;
          esac
        else
          printf 'qa-run focused: target not found: %s\n' "$target" >&2
          exit 2
        fi
        [ "$status" -ne 0 ] && break
      done
      # For failure reporting, point at the last-attempted log
      profile_log="$logdir/focused.$i.log"
      ;;

    subsystem)
      # subsystem profile: relevant pytest + Ruff on changed Python
      cmd="uv run pytest tests/test_subsystem.py && uv run ruff check src/dj_digger"
      "$root/.agents/scripts/project-env" sh -c 'uv run pytest tests/ -k subsystem && uv run ruff check src/dj_digger' > "$logdir/subsystem.log" 2>&1 || status=$?
      ;;

    catalog)
      # catalog profile: catalog/migration tests + package-check
      cmd="uv run pytest tests/test_catalog.py && package-check"
      "$root/.agents/scripts/project-env" sh -c 'uv run pytest tests/ -k catalog' > "$logdir/catalog.log" 2>&1 || status=$?
      if [ "$status" -eq 0 ]; then
        "$root/.agents/scripts/package-check" >> "$logdir/catalog.log" 2>&1 || status=$?
      fi
      ;;

    analysis)
      # analysis profile: worker/pipeline tests + Ruff + mypy
      cmd="uv run pytest tests/test_analysis.py && uv run ruff check src/dj_digger/core/analysis && uv run mypy src/dj_digger/core/analysis"
      "$root/.agents/scripts/project-env" sh -c 'uv run pytest tests/ -k analysis && uv run ruff check src/dj_digger/core/analysis && uv run mypy src/dj_digger/core/analysis' > "$logdir/analysis.log" 2>&1 || status=$?
      ;;

    exports)
      # exports profile: export/schema tests + fixture validation
      cmd="uv run pytest tests/test_exports.py"
      "$root/.agents/scripts/project-env" sh -c 'uv run pytest tests/ -k exports' > "$logdir/exports.log" 2>&1 || status=$?
      ;;

    runtime)
      # runtime profile: CLI/application tests + Ruff + mypy
      cmd="uv run pytest tests/test_cli.py && uv run ruff check src/dj_digger/cli && uv run mypy src/dj_digger"
      "$root/.agents/scripts/project-env" sh -c 'uv run pytest tests/ -k cli && uv run ruff check src/dj_digger && uv run mypy src/dj_digger' > "$logdir/runtime.log" 2>&1 || status=$?
      ;;

    full)
      # full profile: full pytest, Ruff, mypy, fixtures, package-check, diff check
      cmd="uv run pytest + uv run ruff check + uv run mypy + package-check + git diff --check"
      "$root/.agents/scripts/project-env" sh -c 'uv run pytest tests/ && uv run ruff check src/dj_digger && uv run mypy src/dj_digger' > "$logdir/full.log" 2>&1 || status=$?
      if [ "$status" -eq 0 ]; then
        "$root/.agents/scripts/package-check" >> "$logdir/full.log" 2>&1 || status=$?
      fi
      if [ "$status" -eq 0 ]; then
        git diff --check >> "$logdir/full.log" 2>&1 || status=$?
      fi
      ;;

    *)
      printf 'qa-run: unknown profile: %s\n' "$profile" >&2
      exit 2
      ;;
  esac

  # Default profile_log to the standard per-profile log file, unless already set (focused)
  : "${profile_log:=$logdir/$profile.log}"

  # If command failed, print failure details
  if [ "$status" -ne 0 ]; then
    printf '%s FAILED\n' "$cmd" >&2
    if [ -f "$profile_log" ]; then
      tail -10 "$profile_log" >&2
    fi
    printf 'log: %s\n' "$profile_log" >&2
    # Keep the log dir around so the failure message above stays useful.
    cleanup_on_exit=0
    exit 1
  fi

  # Success
  printf 'PASS %s\n' "$profile"
)
