#!/bin/bash
#
# Pre-commit hook for documentation synchronization
#
# Validates manifest and warns about documentation changes

PROJECT_ROOT="$(git rev-parse --show-toplevel)"
DOC_SYNC_DIR="$PROJECT_ROOT/.dss/doc-sync"
RUNNER="$DOC_SYNC_DIR/doc_sync_runner.py"

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

echo "🔍 Pre-commit: Validating documentation manifest..."

# Check if runner exists
if [ ! -f "$RUNNER" ]; then
    echo -e "${YELLOW}⚠️  Doc sync runner not found, skipping validation${NC}"
    exit 0
fi

# Validate manifest
cd "$PROJECT_ROOT"
python3 "$RUNNER" validate 2>&1

if [ $? -ne 0 ]; then
    echo -e "${RED}❌ Manifest validation failed${NC}"
    echo -e "${YELLOW}Fix errors in .dss/doc-sync/manifest.json${NC}"
    exit 1
fi

echo -e "${GREEN}✓ Manifest is valid${NC}"

# Warn if .knowledge/ files are being committed without corresponding code changes
STAGED_KNOWLEDGE=$(git diff --cached --name-only | grep "^\.knowledge/")
STAGED_CODE=$(git diff --cached --name-only | grep -E "\.(py|js|ts|jsx|tsx)$")

if [ -n "$STAGED_KNOWLEDGE" ] && [ -z "$STAGED_CODE" ]; then
    echo -e "${YELLOW}⚠️  Warning: You're committing .knowledge/ changes without code changes${NC}"
    echo -e "${YELLOW}   This might indicate manual edits to generated files${NC}"
    echo ""
    echo "Staged knowledge files:"
    echo "$STAGED_KNOWLEDGE" | sed 's/^/  - /'
    echo ""
    echo -e "${YELLOW}Continue? (y/N)${NC}"
    read -r response
    if [[ ! "$response" =~ ^[Yy]$ ]]; then
        echo "Commit aborted"
        exit 1
    fi
fi

exit 0
