#!/bin/bash

# This script is used to run tests for the lambdas in the src directory.
#
# It takes the following arguments:
# - Optional --coverage flag to run tests with coverage
# - Optional list of lambda names to run tests for
#
# If no lambda names are provided as arguments, it will run tests for all lambdas in the src directory.
#
# The script checks if all lambdas have corresponding Jest config files.
# If any of the lambdas do not have a corresponding Jest config file, the script will print an error message and exit.
#
# NOTE: Lambda functions are identified by matching swg-lambda-* directories in the src directory.

root_dir=$(pwd)
src_dir="$root_dir/src"
jest_config_dir="$root_dir/tests/config"
coverage_flag=""

# Check if the first argument is --coverage
if [ "$1" == "--coverage" ]; then
    coverage_flag="--coverage"
    # Remove --coverage argument from the list
    shift
fi

# Function to check if all src lambdas have config files
check_lambda_configs() {
    echo "Checking if all lambdas in src have corresponding Jest config files..."
    missing_configs=0
    for lambda_dir in "$src_dir"/swg-lambda-*; do
        if [ -d "$lambda_dir" ]; then
            lambda_name=$(basename "$lambda_dir")
            config_file="$jest_config_dir/jest.config.$lambda_name.js"
            if [[ ! -f "$config_file" ]]; then
                echo "Missing Jest config file for $lambda"
                echo "Please create a Jest configuration file at $config_file"
                echo "-------------------------------------------------------------------"
                missing_configs=$((missing_configs + 1))
            fi
        fi
    done

    if [ $missing_configs -ne 0 ]; then
        echo "Config files are missing for $missing_configs lambdas. Tests will not run."
        exit 1
    fi
}

# Function to run tests for a specific lambda
run_tests_for_lambda() {
    lambda_name="$1"
    config_file="$jest_config_dir/jest.config.$lambda_name.js"
    if [[ -f "$config_file" ]]; then
        echo "Running tests for '$lambda' ${coverage_flag:+with coverage}${coverage_flag:+"without coverage"}"
        echo "jest --config "$config_file" $coverage_flag "
        jest --config "$config_file" $coverage_flag --forceExit

        # Get coverage percentage
        SVC_COVERAGE=$(jq -r '.total.lines.pct' .coverage/$lambda_name/coverage-summary.json)
        echo "SVC_COVERAGE: $SVC_COVERAGE"


    else
        echo "Jest Config file not found for $lambda_name"
        echo "Please create the following Jest config file: $config_file"
        echo "-------------------------------------------------------------------"

    fi
}

lambda_names=()

# Check if lambda names were provided as arguments
if [ $# -eq 0 ]; then
    # No lambda names provided, perform a check for config files for all lambdas in src
    check_lambda_configs

    # Find all config files and add their lambda names to the list
    for config_file in "$jest_config_dir"/jest.config.*.js; do
        lambda=$(basename "$config_file" | sed 's/jest.config.//; s/.js//')
        lambda_names+=("$lambda")
    done
else
    # Lambda names provided as arguments, add them to the list
    lambda_names=("$@")
fi

# Export the filtered list as an environment variable
export LAMBDA_NAMES="$lambda_names"
echo "$LAMBDA_NAMES" > ./lambda_names.env


# Run tests for all lambdas in the list
for lambda in "${lambda_names[@]}"; do
    run_tests_for_lambda "$lambda"
done
