#!/usr/bin/env bash
# Pre-commit: format check, lint, and secret scan
set -euo pipefail

echo "Running gofmt check..."
UNFORMATTED=$(gofmt -l . 2>/dev/null | grep -v vendor || true)
if [ -n "$UNFORMATTED" ]; then
  echo "ERROR: Files not formatted:"
  echo "$UNFORMATTED"
  echo "Run: gofmt -l -w ."
  exit 1
fi

echo "Running golangci-lint..."
if command -v golangci-lint &>/dev/null; then
  golangci-lint run
else
  echo "Warning: golangci-lint not installed, running go vet instead"
  go vet ./...
fi

# Scan staged changes for secrets
if command -v gitleaks &>/dev/null; then
  gitleaks git --pre-commit --staged
else
  echo "Warning: gitleaks not installed, skipping secret scan"
fi
