#!/bin/bash
set -e

# Default variables
SKIP_LINT=${SKIP_LINT:-0}
TEST_ARGS=${TEST_ARGS:-}
COV_REPORT=${COV_REPORT:-"off"}
COV_PORT=${COV_PORT:-8000}


if [ "${SKIP_LINT}" -ne "1" ] && [ "${SKIP_LINT}" != "true" ]; then
    echo "Running Ruff Check..."
    uv run ruff check common/ tests/
    
    echo "Running Ruff Format Check..."
    uv run ruff format --check common/ tests/

    echo "Running mypy Type Checking..."
    uv run mypy --cache-dir=/dev/null common/ tests/
fi

COVERAGE_ARGS=""
if [ "${COV_REPORT}" != "off" ]; then
    COVERAGE_ARGS="--cov-report ${COV_REPORT} --cov=common"
fi

echo "Running Pytest..."
uv run pytest "${TEST_ARGS}" tests/ "${COVERAGE_ARGS}"

if [ "${COV_REPORT}" == "html" ]; then
    echo "Serving coverage report on port ${COV_PORT}..."
    python -m http.server "${COV_PORT}" --directory htmlcov --bind 0.0.0.0
fi
