#!/bin/sh
# 🐲 Pixiu Harness Hook: pre-commit
# This hook is managed by Pixiu Harness.

echo "🐲 Pixiu Guardian: Checking commit..."

# 1. 檢查 package.json 是否存在
if [ -f "package.json" ]; then
    # 2. 檢查是否有 Lint 腳本
    if grep -q '"lint":' "package.json"; then
        echo "🔍 Running Lint Check..."
        npm run lint
        if [ $? -ne 0 ]; then
            echo "❌ Lint failed. Commit aborted."
            exit 1
        fi
    fi

    # 3. 檢查是否有 Test 腳本
    if grep -q '"test":' "package.json"; then
        # 簡易檢查：只在 test 快速時執行，或只檢查 type
        if grep -q '"typecheck":' "package.json"; then
             echo "🔍 Running Type Check..."
             npm run typecheck
             if [ $? -ne 0 ]; then
                 echo "❌ Type check failed. Commit aborted."
                 exit 1
             fi
        fi
    fi
fi

# 4. Check for console.log (Simple grep)
# Exclude node_modules, dist, build, .git
if git diff --cached --name-only | grep -vE 'node_modules|dist|build|\.git' | xargs grep -n "console.log" > /dev/null 2>&1; then
    echo "⚠️  Warning: console.log DETECTED in staged files."
    # Uncomment to block:
    # exit 1
fi

echo "✅ Guardian passed. Proceeding..."
exit 0
