#!/bin/bash
source "$ROOT_PATH"/scripts/common.sh

set -eo pipefail

check_coverage_summary_on_lambda_dir() {
  local lambda_name="$1"

  # Check if coverage summary file exists
  if [[ ! -f "$coverage_summary_file" ]]; then
    handle_error "Coverage summary not found for $lambda_name: $coverage_summary_file"
  fi

  # Extract total lines percentage using jq
  total_lines_pct=$(jq -r '.total.lines.pct' "$coverage_summary_file" 2>/dev/null)

  # Handle potential jq error
  if [[ $? -ne 0 ]]; then
    handle_error "jq not found or failed for $lambda_name, skipping coverage analysis."
    exit 0
  fi

  # Check if COVERAGE_GATE is set and is a number
  if [[ -n "${COVERAGE_GATE}" ]] && [[ "${COVERAGE_GATE}" =~ ^[0-9]+$ ]]; then
    coverage_gate=${COVERAGE_GATE}
  else
    coverage_gate=1
  fi

  echo "Coverage gate for $lambda_name: $coverage_gate%"
  if (( $(echo "$total_lines_pct < $coverage_gate" | bc -l) )); then
    echo_red_bg "Coverage for $lambda_name ($total_lines_pct%) below threshold ($coverage_gate%)."
    if [[ $WILL_FAIL == true ]]; then
      echo_red_bg "WILL_FAIL set to $WILL_FAIL, exiting.."
      exit 1
    fi
  else
    echo_green "Coverage for $lambda_name ($total_lines_pct%) meets or exceeds threshold ($coverage_gate%)."
  fi
}

check_coverage_summary_on_default_dir() {
  # Check if coverage summary file exists
  if [[ ! -f "$coverage_summary_file" ]]; then
    handle_error "Coverage summary not found: $coverage_summary_file"
  fi

  # Extract total lines percentage using jq
  total_lines_pct=$(jq -r '.total.lines.pct' "$coverage_summary_file" 2>/dev/null)

  # Handle potential jq error
  if [[ $? -ne 0 ]]; then
    handle_error "jq not found or failed, skipping coverage analysis."
    exit 0
  fi

  # Check if COVERAGE_GATE is set and is a number
  if [[ -n "${COVERAGE_GATE}" ]] && [[ "${COVERAGE_GATE}" =~ ^[0-9]+$ ]]; then
    coverage_gate=${COVERAGE_GATE}
  else
    coverage_gate=1
  fi

  echo_red "Default case coverage gate: $coverage_gate%"
  if (( $(echo "$total_lines_pct < $coverage_gate" | bc -l) )); then
    echo_red_bg "Default case coverage ($total_lines_pct%) below threshold ($coverage_gate%)."
    if [[ $WILL_FAIL == true ]]; then
      echo_red_bg "WILL_FAIL set to $WILL_FAIL, exiting.."
      exit 1
    fi
  else
    echo_green "Default case coverage ($total_lines_pct%) meets or exceeds threshold ($coverage_gate%)."
  fi
}

# Determine which case to handle: single Lambda, list of Lambdas, or default
if [[ -n "$LAMBDA_NAME" ]]; then
  # Single lambda case
  lambda_name=$LAMBDA_NAME
  echo "Running tests for single $lambda_name lambda..."
  npm run test:lambdas-coverage "$lambda_name" || handle_error "Tests failed for $lambda_name"

  echo "Checking coverage reports for single $lambda_name lambda..."
  coverage_summary_file=".coverage/$lambda_name/coverage-summary.json"
  check_coverage_summary_on_lambda_dir "$lambda_name"
elif [[ "$DOMAIN_REPO" == true ]]; then
  # List of lambdas case
  echo "Running tests for all Lambdas on Domain Repository..."
  npm run test:lambdas-coverage || handle_error "Tests failed for Lambdas on Domain Repository"

  # Prepare lambda list which must be taken after the npm run test command
  lambda_list_file="./lambda_names.env"
  lambda_list=$(<"$lambda_list_file")

  # Clean temporary files
  rm -rf ./lambda_names.env

  echo "Checking coverage reports for these lambdas: $lambda_list"
  # Check coverage summary for all lambdas in list
  for lambda_name in $lambda_list; do
    coverage_summary_file=".coverage/$lambda_name/coverage-summary.json"
    echo "Checking coverage reports for $lambda_name lambda..."
    check_coverage_summary_on_lambda_dir "$lambda_name"
  done
else
  # Default case
  echo "Running tests for lambda on standard repo (default case)..."
  npm run test:lambdas-coverage || handle_error "Tests failed for lambda on standard repository"
  coverage_summary_file=".coverage/coverage-summary.json"
  check_coverage_summary_on_lambda_dir
fi
