#!/usr/bin/env bash
# Crewly Quality Gates — Git pre-commit hook
#
# Calls the Crewly Quality Gates API before each commit.
# Blocks the commit if the quality gate check fails.
# Gracefully allows the commit if the API is unreachable.
#
# Install: bash config/hooks/install-hooks.sh
# Or:      npx crewly install-hooks

set -euo pipefail

CREWLY_PORT="${CREWLY_PORT:-8787}"
CREWLY_HOST="${CREWLY_HOST:-localhost}"
CREWLY_API_URL="http://${CREWLY_HOST}:${CREWLY_PORT}/api/quality-gates/pre-commit"
CREWLY_TIMEOUT="${CREWLY_TIMEOUT:-5}"

# Resolve session name from environment or fallback
SESSION_NAME="${CREWLY_SESSION_NAME:-}"
if [ -z "$SESSION_NAME" ]; then
  # Try to detect from terminal title or default to unknown
  SESSION_NAME="unknown"
fi

BRANCH="$(git branch --show-current 2>/dev/null || echo "detached")"

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
NC='\033[0m' # No Color

# --- Tagged debug logs (debugging SOP) -------------------------------------
# A staged added line containing the debug tag prefix fails the commit.
# Reports how many files were scanned. If the staged list cannot be read, the
# commit fails (a guard must not pass silently). Markdown files are skipped
# (docs may mention the tag); nothing staged is not an error (message-only amend).
DEBUG_TAG='\[DEBUG-'
if ! STAGED_FILES="$(git diff --cached --name-only --diff-filter=ACM 2>/dev/null)"; then
  echo -e "${RED}[CREWLY] Debug-tag scan: could not read the staged file list${NC}"
  exit 1
fi
SCANNED=0
DEBUG_HITS=""
while IFS= read -r STAGED; do
  [ -z "$STAGED" ] && continue
  case "$STAGED" in *.md) continue ;; esac
  SCANNED=$((SCANNED + 1))
  HIT="$(git diff --cached -U0 -- "$STAGED" 2>/dev/null | grep -E "^\+[^+].*${DEBUG_TAG}|^\+${DEBUG_TAG}" || true)"
  if [ -n "$HIT" ]; then
    DEBUG_HITS="${DEBUG_HITS}${STAGED}\n"
  fi
done <<< "$STAGED_FILES"
echo -e "${GREEN}[CREWLY] Debug-tag scan: ${SCANNED} file(s) scanned${NC}"
if [ -n "$DEBUG_HITS" ]; then
  echo -e "${RED}[CREWLY] COMMIT BLOCKED: staged debug log tagged [DEBUG-...] in:${NC}"
  echo -e "${RED}${DEBUG_HITS}${NC}"
  echo -e "${YELLOW}Remove them (grep -rn '\[DEBUG-' .) and commit again.${NC}"
  exit 1
fi

# Check if curl is available
if ! command -v curl &>/dev/null; then
  echo -e "${YELLOW}[CREWLY] curl not found — skipping quality gate check${NC}"
  exit 0
fi

# Check if jq is available (used for JSON parsing)
HAS_JQ=false
if command -v jq &>/dev/null; then
  HAS_JQ=true
fi

# Call the Quality Gates API
RESPONSE=""
HTTP_CODE=""

RESPONSE=$(curl -s -w "\n%{http_code}" \
  --connect-timeout "$CREWLY_TIMEOUT" \
  --max-time "$((CREWLY_TIMEOUT * 2))" \
  -X POST "$CREWLY_API_URL" \
  -H "Content-Type: application/json" \
  -d "{\"sessionName\":\"${SESSION_NAME}\",\"branch\":\"${BRANCH}\"}" \
  2>/dev/null) || {
  # API unreachable — graceful degradation
  echo -e "${YELLOW}[CREWLY] Quality Gates API unreachable at ${CREWLY_API_URL}${NC}"
  echo -e "${YELLOW}[CREWLY] Proceeding with commit (graceful degradation)${NC}"
  exit 0
}

# Split response body and HTTP status code
HTTP_CODE=$(echo "$RESPONSE" | tail -n1)
BODY=$(echo "$RESPONSE" | sed '$d')

# Handle non-200 HTTP responses
if [ "$HTTP_CODE" != "200" ]; then
  echo -e "${YELLOW}[CREWLY] Quality Gates API returned HTTP ${HTTP_CODE}${NC}"
  echo -e "${YELLOW}[CREWLY] Proceeding with commit (graceful degradation)${NC}"
  exit 0
fi

# Parse the response
if [ "$HAS_JQ" = true ]; then
  PASS=$(echo "$BODY" | jq -r '.pass // false' 2>/dev/null || echo "false")
  FEEDBACK=$(echo "$BODY" | jq -r '.feedback // "No feedback provided"' 2>/dev/null || echo "No feedback provided")
else
  # Fallback: basic string matching without jq
  if echo "$BODY" | grep -q '"pass"\s*:\s*true'; then
    PASS="true"
  else
    PASS="false"
  fi
  # Extract feedback with basic regex
  FEEDBACK=$(echo "$BODY" | grep -o '"feedback"\s*:\s*"[^"]*"' | sed 's/"feedback"\s*:\s*"//' | sed 's/"$//' || echo "No feedback provided")
fi

# Act on the result
if [ "$PASS" = "true" ]; then
  echo -e "${GREEN}[CREWLY] Quality Gates: All checks passed${NC}"
  exit 0
else
  echo ""
  echo -e "${RED}============================================${NC}"
  echo -e "${RED}  CREWLY QUALITY GATE: COMMIT BLOCKED${NC}"
  echo -e "${RED}============================================${NC}"
  echo ""
  echo -e "${RED}${FEEDBACK}${NC}"
  echo ""
  echo -e "${YELLOW}Fix the above issues before committing.${NC}"
  echo -e "${YELLOW}To bypass (not recommended): git commit --no-verify${NC}"
  echo -e "${RED}============================================${NC}"
  exit 1
fi
