#!/usr/bin/env bash
set -euo pipefail

echo "Running lockpick pre-commit check..."

if ! lockpick; then
  echo ""
  echo "ERROR: lockpick failed. Commit aborted."
  echo "Fix the reported issues and run lockpick successfully before committing."
  exit 1
fi

echo "lockpick passed."

echo "Running license header check..."

if ! ./.github/check-license-headers.sh; then
  echo ""
  echo "ERROR: License header check failed. Commit aborted."
  echo "Add the correct license header to all source files before committing."
  exit 1
fi

if [ "${SKIP_COVERAGE:-0}" = "1" ]; then
  echo "Skipping coverage gate (SKIP_COVERAGE=1)."
else
  echo "Running 100% coverage check (cargo llvm-cov --branch)..."

  if ! ./.github/check-coverage.sh; then
    echo ""
    echo "ERROR: Coverage gate failed. Commit aborted."
    echo "Run 'cargo llvm-cov --branch --html' for the HTML report (target/llvm-cov/html/index.html) and add the missing tests."
    echo "Set SKIP_COVERAGE=1 to bypass this check temporarily."
    exit 1
  fi
fi

echo "All pre-commit checks passed. Proceeding with commit."
