#!/bin/bash

set -e

: "${SKIP_LINT:=0}"
: "${COV_REPORT:=term}"
: "${COV_PORT:=8000}"

COV_REPORT=$(echo "${COV_REPORT}" | tr '[:upper:]' '[:lower:]')

if [ "$SKIP_LINT" -ne 1 ]; then
    echo 'Running linting...'
    uv run yamllint -s -d "{extends: relaxed, rules: {line-length: {max: 100}}}" docker-compose.yaml
    uv run ruff check snapshot_contracts/ tests/ config.py app.py
    uv run ruff format --check snapshot_contracts/ tests/ config.py app.py
else
    echo 'Skipping linting...'
fi

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

if [ "$RUN_INTEGRATION" -ne 1 ]; then
    echo 'Running tests...'
    uv run -- python -m pytest $TEST_ARGS --ignore=tests/integration/ tests/ $COVERAGE_ARGS
else

    echo 'Running create_db_fixtures.py...'
    uv run -- python tests/integration/utils/create_db_fixtures.py

    echo 'Running truncate_snowflake_test_table.py...'
    uv run -- python tests/integration/utils/truncate_snowflake_test_table.py

    echo 'Running integration tests...'
    uv run -- python -m pytest tests/integration -s $TEST_ARGS $COVERAGE_ARGS \
      --html=report.html \
      --self-contained-html \
      --cov-report xml \
      --junitxml=pyunit.xml \
      --jira
fi

if [ "$COV_REPORT" = "html" ]; then
    uv run --no-sync -- python -m http.server --directory htmlcov $COV_PORT
fi

echo 'Done!'
