#!/bin/bash

set -e

export functionName="$FUNCTION_NAME"
echo "▶ Running integration tests for lambda: $functionName"

# Root directory of the tests: lambda-documents/tests
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
LAMBDA_DIR="$ROOT_DIR/integration/$functionName"
REPORT_FILE="report/report_$functionName.html"

echo "Root directory is: $ROOT_DIR"
echo "▶ Running linting..."
yamllint -s docker-compose.yaml
ruff check .
ruff format . --check --diff
mypy integration models utils config.py

if [ -z "$functionName" ]; then
  echo "❌ functionName not set. Example: $functionName='lambda_a'"
  exit 1
fi

echo "▶ Running integration tests for lambda: $functionName"

if [ -d "$LAMBDA_DIR" ]; then
  echo "→ Testing $lambda"
  pytest $LAMBDA_DIR/test*.py --html="$REPORT_FILE" --self-contained-html --reruns 2 --reruns-delay 1
else
  echo "⚠️ Skipping integration tests for $functionName: No tests in '$LAMBDA_DIR'."
  exit 0
fi

echo "✅ Lambda integration tests done. View test report here: $REPORT_FILE"
