#!/usr/bin/env bash
set -euo pipefail

# defaults if not set
: "${SKIP_LINT:=0}"
: "${SKIP_TYPE_CHECKS:=0}"
: "${SKIP_TESTS:=0}"
: "${TEST_ARGS:=}"
: "${SKIP_COV:=0}"

# run linting
if [ "$SKIP_LINT" -ne 1 ]; then
    echo 'Running linting...'
    uv lock --check
    shellcheck lint-and-test.sh
    hadolint Dockerfile
    yamllint -s .hadolint.yaml .yamllint.yaml docker-compose.yaml software-catalog.yaml
    ruff check src tests dev
    ruff format --check src tests dev
else
    echo 'Skipping linting...'
fi

# run type checks
if [ "$SKIP_TYPE_CHECKS" -ne 1 ]; then
    echo 'Running type checks...'
    mypy src tests dev
else
    echo 'Skipping type checks...'
fi

# run tests
if [ "$SKIP_TESTS" -ne 1 ]; then
    echo 'Running tests...'
    if [ "$SKIP_COV" -ne 1 ]; then
        COV_ARGS="--cov=src --cov-report=term --cov-report=xml:coverage.xml"
    else
        COV_ARGS="--no-cov"
    fi
    # intentional word splitting for multi-arg variables
    # shellcheck disable=SC2086
    pytest tests/unit tests/functional $COV_ARGS $TEST_ARGS
else
    echo 'Skipping tests...'
fi

echo 'Done!'
