#!/bin/bash

# Wogi Flow - Setup Git Hooks
# Installs optional pre-commit hooks to enforce workflow rules

set -e

WORKFLOW_DIR=".workflow"

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

show_help() {
    echo "Setup Git Hooks"
    echo ""
    echo "Usage: flow setup-hooks [options]"
    echo ""
    echo "Options:"
    echo "  install      Install git hooks (pre-commit + post-commit)"
    echo "  uninstall    Remove git hooks"
    echo "  status       Check if hooks are installed"
    echo ""
    echo "Pre-commit hook enforces:"
    echo "  • request-log.md has been updated"
    echo "  • app-map.md updated if new components"
    echo "  • No console.log in production code"
    echo "  • Tests pass (if configured in config.json)"
    echo "  • Skill learning extraction (if skillLearning.enabled)"
    echo ""
    echo "Post-commit hook:"
    echo "  • Auto-closes auto-created tasks when their files are committed"
    echo "  • Prevents stale tasks from accumulating in the queue"
}

install_hooks() {
    if [ ! -d ".git" ]; then
        echo -e "${RED}Error: Not a git repository${NC}"
        exit 1
    fi
    
    mkdir -p .git/hooks
    
    # Create pre-commit hook
    cat > .git/hooks/pre-commit << 'HOOK'
#!/bin/bash

# Wogi Flow Pre-commit Hook
# Enforces workflow rules before commits

WORKFLOW_DIR=".workflow"
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'

echo "Running Wogi Flow pre-commit checks..."

failed=0

# Check 1: request-log.md was modified (if other files changed)
staged_files=$(git diff --cached --name-only)
non_workflow_files=$(echo "$staged_files" | grep -v "^\.workflow/" | grep -v "^CLAUDE.md" || true)

if [ -n "$non_workflow_files" ]; then
    if ! echo "$staged_files" | grep -q "request-log.md"; then
        echo -e "${YELLOW}⚠ Warning: Code changed but request-log.md not updated${NC}"
        echo "  Consider adding a log entry for this change."
        # Warning only, don't fail
    else
        echo -e "${GREEN}✓${NC} request-log.md updated"
    fi
fi

# Check 2: No console.log in staged JS/TS files (warning only)
js_files=$(echo "$staged_files" | grep -E '\.(js|ts|jsx|tsx)$' || true)
if [ -n "$js_files" ]; then
    console_logs=$(git diff --cached -- $js_files | grep -E '^\+.*console\.(log|debug)' || true)
    if [ -n "$console_logs" ]; then
        echo -e "${YELLOW}⚠ Warning: console.log found in staged files${NC}"
        echo "$console_logs" | head -5
        # Warning only
    fi
fi

# Check 3: Run tests if configured
if [ -f "$WORKFLOW_DIR/config.json" ]; then
    run_tests=$(CONFIG_FILE="$WORKFLOW_DIR/config.json" python3 -c "
import json, os
with open(os.environ['CONFIG_FILE']) as f:
    config = json.load(f)
print(config.get('testing', {}).get('runBeforeCommit', False))
" 2>/dev/null || echo "False")

    if [ "$run_tests" = "True" ]; then
        echo "Running tests..."
        if npm test --silent 2>/dev/null; then
            echo -e "${GREEN}✓${NC} Tests passed"
        else
            echo -e "${RED}✗ Tests failed${NC}"
            failed=1
        fi
    fi
fi

# Check 4: Lint if configured
if [ -f "package.json" ] && grep -q '"lint"' package.json 2>/dev/null; then
    staged_code=$(echo "$staged_files" | grep -E '\.(js|ts|jsx|tsx|vue)$' || true)
    if [ -n "$staged_code" ]; then
        # Only lint staged files
        if command -v npx &> /dev/null && [ -f ".eslintrc.js" ] || [ -f ".eslintrc.json" ] || [ -f ".eslintrc" ]; then
            echo "Linting staged files..."
            if echo "$staged_code" | xargs npx eslint --quiet 2>/dev/null; then
                echo -e "${GREEN}✓${NC} Lint passed"
            else
                echo -e "${YELLOW}⚠${NC} Lint warnings (not blocking)"
            fi
        fi
    fi
fi

# Check 5: Skill Learning (extract learnings before commit)
if [ -f "$WORKFLOW_DIR/config.json" ]; then
    skill_learning=$(CONFIG_FILE="$WORKFLOW_DIR/config.json" python3 -c "
import json, os
with open(os.environ['CONFIG_FILE']) as f:
    config = json.load(f)
sl = config.get('skillLearning', {})
enabled = sl.get('enabled', False) and sl.get('autoExtract', False)
trigger = sl.get('triggers', {}).get('onCommit', True)
print('True' if enabled and trigger else 'False')
" 2>/dev/null || echo "False")

    if [ "$skill_learning" = "True" ]; then
        echo "📚 Extracting skill learnings..."
        if command -v node &> /dev/null && [ -f "scripts/flow-skill-learn.js" ]; then
            node scripts/flow-skill-learn.js --trigger=commit 2>/dev/null || true
        fi
    fi
fi

if [ $failed -eq 1 ]; then
    echo ""
    echo -e "${RED}Pre-commit checks failed. Fix issues or use --no-verify to bypass.${NC}"
    exit 1
fi

echo -e "${GREEN}✓ Pre-commit checks passed${NC}"
exit 0
HOOK
    
    chmod +x .git/hooks/pre-commit

    echo -e "${GREEN}✓ Pre-commit hook installed${NC}"

    # Create post-commit hook for auto-task completion
    cat > .git/hooks/post-commit << 'HOOK'
#!/bin/bash

# Wogi Flow Post-commit Hook
# Auto-closes auto-created tasks when their files are committed

# Run the Node.js post-commit handler if it exists
SCRIPT_DIR="$(dirname "$0")/../../scripts/hooks/git"
if [ -f "$SCRIPT_DIR/post-commit.js" ]; then
    node "$SCRIPT_DIR/post-commit.js" 2>/dev/null || true
fi

# Always exit 0 - don't block after successful commit
exit 0
HOOK

    chmod +x .git/hooks/post-commit

    echo -e "${GREEN}✓ Post-commit hook installed${NC}"
    echo ""
    echo "Pre-commit hook will check:"
    echo "  • request-log.md updates (warning)"
    echo "  • console.log statements (warning)"
    echo "  • Tests (if runBeforeCommit: true in config.json)"
    echo "  • Lint (if eslint configured)"
    echo ""
    echo "Post-commit hook will:"
    echo "  • Auto-close auto-created tasks when their files are committed"
    echo ""
    echo "To bypass: git commit --no-verify"
    echo "To remove: ./scripts/flow setup-hooks uninstall"
}

uninstall_hooks() {
    # Remove pre-commit hook
    if [ -f ".git/hooks/pre-commit" ]; then
        if grep -q "Wogi Flow" .git/hooks/pre-commit 2>/dev/null; then
            rm .git/hooks/pre-commit
            echo -e "${GREEN}✓ Pre-commit hook removed${NC}"
        else
            echo -e "${YELLOW}Pre-commit hook exists but wasn't installed by Wogi Flow${NC}"
            echo "Remove manually if desired: rm .git/hooks/pre-commit"
        fi
    else
        echo "No pre-commit hook installed"
    fi

    # Remove post-commit hook
    if [ -f ".git/hooks/post-commit" ]; then
        if grep -q "Wogi Flow" .git/hooks/post-commit 2>/dev/null; then
            rm .git/hooks/post-commit
            echo -e "${GREEN}✓ Post-commit hook removed${NC}"
        else
            echo -e "${YELLOW}Post-commit hook exists but wasn't installed by Wogi Flow${NC}"
            echo "Remove manually if desired: rm .git/hooks/post-commit"
        fi
    else
        echo "No post-commit hook installed"
    fi
}

check_status() {
    echo -e "${CYAN}Hook Status${NC}"
    echo ""

    if [ ! -d ".git" ]; then
        echo -e "${YELLOW}Not a git repository${NC}"
        exit 0
    fi

    # Check pre-commit hook
    if [ -f ".git/hooks/pre-commit" ]; then
        if grep -q "Wogi Flow" .git/hooks/pre-commit 2>/dev/null; then
            echo -e "${GREEN}✓${NC} Wogi Flow pre-commit hook installed"
        else
            echo -e "${YELLOW}○${NC} Pre-commit hook exists (not Wogi Flow)"
        fi
    else
        echo -e "${YELLOW}○${NC} No pre-commit hook installed"
    fi

    # Check post-commit hook
    if [ -f ".git/hooks/post-commit" ]; then
        if grep -q "Wogi Flow" .git/hooks/post-commit 2>/dev/null; then
            echo -e "${GREEN}✓${NC} Wogi Flow post-commit hook installed"
            echo "    Auto-closes auto-created tasks on commit"
        else
            echo -e "${YELLOW}○${NC} Post-commit hook exists (not Wogi Flow)"
        fi
    else
        echo -e "${YELLOW}○${NC} No post-commit hook installed"
        echo "    Run 'flow setup-hooks install' to enable auto-task completion"
    fi
}

case "${1:-}" in
    install)
        install_hooks
        ;;
    uninstall)
        uninstall_hooks
        ;;
    status)
        check_status
        ;;
    help|--help|-h)
        show_help
        ;;
    *)
        show_help
        ;;
esac
