#!/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 src/ tests/ config.py --no-cache 
    
    echo "Running Ruff Format Check..."
    uv run ruff format src/ tests/ config.py --check --no-cache 

    echo "Running ty..."
    uv run ty check src/ tests/ config.py
fi

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

echo "Running Pytest..."
uv run pytest "${TEST_ARGS}" --ignore=tests/integration/ 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
