#!/bin/bash

# Commit-msg hook for quickcall-integrations
# Enforces commit message format and blocks Claude/Anthropic attribution

RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m'

COMMIT_MSG_FILE="$1"
COMMIT_MSG=$(cat "$COMMIT_MSG_FILE")

# ============================================
# 1. Enforce conventional commit format
# ============================================
# Valid prefixes: feat, add, update, fix, remove, refactor, docs, test, chore, ci, perf, style
VALID_PREFIXES="^(feat|add|update|fix|remove|refactor|docs|test|chore|ci|perf|style):"

if ! echo "$COMMIT_MSG" | head -1 | grep -qE "$VALID_PREFIXES"; then
    echo -e "${RED}ERROR: Commit message must start with a valid prefix${NC}"
    echo -e "${YELLOW}Valid prefixes:${NC}"
    echo -e "  feat:     New feature"
    echo -e "  add:      New file or module"
    echo -e "  update:   Update existing functionality"
    echo -e "  fix:      Bug fix"
    echo -e "  remove:   Remove code or files"
    echo -e "  refactor: Code refactoring (no functional change)"
    echo -e "  docs:     Documentation only"
    echo -e "  test:     Adding or updating tests"
    echo -e "  chore:    Maintenance tasks"
    echo -e "  ci:       CI/CD changes"
    echo -e "  perf:     Performance improvements"
    echo -e "  style:    Code style/formatting"
    echo -e ""
    echo -e "Example: feat: user authentication module"
    exit 1
fi

# ============================================
# 2. Require a "Why:" section at the end
# ============================================
# Commit must have a "Why:" header followed by 2-3 bullet points.
# Exception: chore, ci, style commits are exempt.
COMMIT_PREFIX=$(echo "$COMMIT_MSG" | head -1 | grep -oE '^(feat|add|update|fix|remove|refactor|docs|test|chore|ci|perf|style)')

if [[ "$COMMIT_PREFIX" != "chore" && "$COMMIT_PREFIX" != "ci" && "$COMMIT_PREFIX" != "style" ]] && ! grep -q '^Why:' "$COMMIT_MSG_FILE"; then
    echo -e "${RED}ERROR: Commit message must include a 'Why:' section at the end${NC}"
    echo -e "${YELLOW}Expected format:${NC}"
    echo -e ""
    echo -e "  fix: resolve null timestamps in cursor sessions"
    echo -e ""
    echo -e "  - Updated parser to assign sequential timestamps"
    echo -e "  - Fixed viewer sort order for cursor sessions"
    echo -e ""
    echo -e "  Why:"
    echo -e "  - Cursor transcripts have no per-message timestamps"
    echo -e "  - Viewer was showing scrambled message order"
    exit 1
fi

WHY_SECTION=$(sed -n '/^Why:/,$p' "$COMMIT_MSG_FILE" | tail -n +2 | sed '/^$/d' | sed '/^#/d')
WHY_BULLET_COUNT=$(echo "$WHY_SECTION" | grep -cE '^\s*[-*•]')

if [[ "$COMMIT_PREFIX" != "chore" && "$COMMIT_PREFIX" != "ci" && "$COMMIT_PREFIX" != "style" ]] && [ "$WHY_BULLET_COUNT" -lt 2 ]; then
    echo -e "${RED}ERROR: 'Why:' section needs at least 2 bullet points${NC}"
    echo -e "${YELLOW}Current Why section:${NC}"
    echo "$WHY_SECTION"
    echo -e ""
    echo -e "${YELLOW}Add bullets like:${NC}"
    echo -e "  Why:"
    echo -e "  - What problem triggered this change?"
    echo -e "  - What was the context or discovery?"
    exit 1
fi

# ============================================
# 3. Block Claude/Anthropic attribution
# ============================================
if echo "$COMMIT_MSG" | grep -iE '(claude|anthropic|co-authored-by.*claude|co-authored-by.*anthropic)' > /dev/null; then
    echo -e "${RED}ERROR: Commit message contains Claude/Anthropic attribution${NC}"
    echo -e "Please remove any Claude or Anthropic references from your commit message"
    exit 1
fi

exit 0
