#!/usr/bin/env bash
# Copyright (c) 2025 Tylt LLC. All rights reserved.
# Derivative works may be released by researchers,
# but original files may not be redistributed or used beyond research purposes.
#
# DO NOT EDIT THIS FILE - Generated by cudag framework

set -euo pipefail

mode="${1:-staged}"

if [ "$mode" = "--help" ] || [ "$mode" = "-h" ]; then
  echo "Usage: $0 [--all]" >&2
  echo "  --all   Run checks against all tracked Python files instead of staged ones." >&2
  exit 0
fi

repo_root="$(git rev-parse --show-toplevel 2>/dev/null || pwd)"
cd "$repo_root"

if [ "$mode" = "--all" ]; then
  py_targets=$(git ls-files -- '*.py' 2>/dev/null || find . -name '*.py' -type f)
  scope_label="all Python files"
else
  py_targets=$(git diff --cached --name-only --diff-filter=ACM -- '*.py' 2>/dev/null || true)
  scope_label="staged Python files"
fi

if [ -z "$py_targets" ]; then
  echo "No ${scope_label}. Skipping lint/type checks."
  exit 0
fi

# Use uv run to execute in the project's venv with dev dependencies (Python 3.12)
UV_RUN="uv run --python 3.12 --extra dev"

echo "Running ruff (lexical checks) on ${scope_label}..."
printf '%s\n' "$py_targets" | xargs $UV_RUN ruff check

echo "Running mypy (syntax & types) on ${scope_label}..."
printf '%s\n' "$py_targets" | xargs $UV_RUN mypy

echo ""
echo "✓ All checks passed!"
