#!/usr/bin/env bash
#
# PMAT TDG Baseline Auto-Update Post-Commit Hook
# Auto-generated by `pmat hooks install --tdg-enforcement`
# DO NOT EDIT MANUALLY - Regenerate with `pmat hooks refresh`
#
# This hook automatically updates the TDG baseline after successful commits
# to keep quality tracking in sync with codebase evolution.
#
# Configuration: .pmat/tdg-rules.toml

set -e  # Exit on first error

# Colors for output
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# Configuration (injected by pmat hooks install)
BASELINE_PATH="{{BASELINE_PATH}}"
AUTO_UPDATE="{{AUTO_UPDATE}}"
STORE_IN_GIT="{{STORE_IN_GIT}}"

# Check if auto-update is enabled
if [ "${AUTO_UPDATE}" != "true" ]; then
    exit 0  # Silent exit if auto-update disabled
fi

# Check if pmat binary is available
if ! command -v pmat &> /dev/null; then
    # Silent failure - don't block post-commit
    exit 0
fi

echo -e "${BLUE}📊 PMAT TDG: Updating baseline...${NC}"

# Update baseline with current project state
if pmat tdg baseline update --output "${BASELINE_PATH}" --path . 2>/dev/null; then
    echo -e "${GREEN}✅ Baseline updated: ${BASELINE_PATH}${NC}"

    # Add baseline to git if configured
    if [ "${STORE_IN_GIT}" = "true" ]; then
        # Check if baseline changed
        if git diff --quiet "${BASELINE_PATH}" 2>/dev/null; then
            # No changes, nothing to commit
            :
        else
            # Stage baseline for next commit
            git add "${BASELINE_PATH}" 2>/dev/null || true
            echo -e "${YELLOW}📝 Baseline staged for next commit${NC}"
        fi
    fi
else
    # Silent failure - baseline update is best-effort
    echo -e "${YELLOW}⚠️  Baseline update failed (continuing)${NC}"
fi

exit 0
