#!/bin/bash

ROOT_DIR=$(git rev-parse --show-toplevel)
cd "$ROOT_DIR"

echo "Running lint..."
if ! make lint; then
  echo "Linting failed. Please fix the issues before pushing."
  exit 1
fi

echo "Running security scan..."
if ! make security-scan; then
  echo "Security scan failed. Please fix the issues before pushing."
  exit 1
fi

echo "Running unit tests with coverage check..."
if ! make coverage-json; then
  echo "Unit tests failed. Please fix the issues before pushing."
  exit 1
fi

coverage_percent=$(python -c "import json; print(json.load(open('coverage.json'))['totals']['percent_covered'])")
echo "Coverage: $coverage_percent%"
if (( $(echo "$coverage_percent < 90" | bc -l) )); then
  echo "Code coverage is below 90% threshold ($coverage_percent%). Please add more tests."
  exit 1
else
  echo "Coverage meets 90% threshold."
fi

echo "Running integration tests..."
if ! make test-integration; then
  echo "Integration tests failed. Please fix the issues before pushing."
  exit 1
fi

echo "Running functional tests..."
if ! make test-functional; then
  echo "Functional tests failed. Please fix the issues before pushing."
  exit 1
fi

echo "All tests, linting, and security scan passed!"
exit 0
