#!/bin/sh
# RepoKit Cross-Environment Pre-Commit Hook
# This wrapper script detects the environment and calls Python appropriately

# Function to detect Python command
find_python() {
    # Try to detect if we're on Windows
    if [ -n "$WINDIR" ] || [ "$OSTYPE" = "msys" ] || [ "$OSTYPE" = "cygwin" ]; then
        # Windows environment - try various Python commands
        if command -v py >/dev/null 2>&1; then
            echo "py"
        elif command -v python >/dev/null 2>&1; then
            echo "python"
        elif command -v python3 >/dev/null 2>&1; then
            echo "python3"
        else
            echo ""
        fi
    else
        # Unix/Linux/WSL environment
        if command -v python3 >/dev/null 2>&1; then
            echo "python3"
        elif command -v python >/dev/null 2>&1; then
            echo "python"
        else
            echo ""
        fi
    fi
}

# Find Python command
PYTHON_CMD=$(find_python)

# If Python not found, exit gracefully but block commit
if [ -z "$PYTHON_CMD" ]; then
    echo "⚠️  Python not found - cannot run RepoKit pre-commit checks"
    echo "   To enable: Install Python and ensure it's in PATH"
    echo "   To bypass: use 'git commit --no-verify' (not recommended)"
    exit 1
fi

# Execute the Python script. Its messages contain emoji; when output is not a
# UTF-8 console (Windows, or piped from a GUI client) Python would raise
# UnicodeEncodeError and block the commit, so force UTF-8 output.
PYTHONIOENCODING=utf-8 $PYTHON_CMD - << 'EOF'
"""
RepoKit Pre-Commit Hook with Private Content Guardrails.

This hook prevents accidental commits of private content to public branches.
"""

import sys
import os
import subprocess

# Add parent directory to Python path for imports
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))

try:
    from repokit.branch_utils import BranchContext
except ImportError:
    print("ERROR: Could not import RepoKit branch utilities.")
    print("Make sure RepoKit is installed: pip install -e .")
    sys.exit(1)


def main():
    """Main pre-commit hook logic."""
    # Get branch context
    context = BranchContext()
    
    # Show current branch info
    print(f"🔍 RepoKit Pre-Commit Check")
    print(f"   Branch: {context.current_branch} ({context.branch_type})")
    
    # Validate commit
    is_valid, errors = context.validate_commit()
    
    if not is_valid:
        print("\n❌ COMMIT BLOCKED - Private Content Protection\n")
        for error in errors:
            print(error)
        return 1
    
    # Additional checks for public branches
    if context.is_public_branch():
        staged_files = context.get_staged_files()
        
        # Warn about sensitive patterns even if not explicitly private
        warnings = []
        for file in staged_files:
            if any(pattern in file.lower() for pattern in ['password', 'secret', 'token', 'credential']):
                warnings.append(f"  ⚠️  {file} - filename suggests sensitive content")
        
        if warnings:
            print("\n⚠️  WARNING - Potentially Sensitive Files:")
            for warning in warnings:
                print(warning)
            print("\nMake sure these files don't contain actual secrets!")
            print("Press Ctrl+C to cancel, or wait 3 seconds to continue...")
            
            try:
                import time
                time.sleep(3)
            except KeyboardInterrupt:
                print("\n\nCommit cancelled by user.")
                return 1
    
    print("\n✅ Pre-commit checks passed!")
    
    # Check if we should set up branch excludes
    exclude_file = os.path.join('.git', 'info', 'exclude')
    if not os.path.exists(exclude_file) or os.path.getsize(exclude_file) == 0:
        print("\n📝 Setting up branch-specific excludes...")
        context.setup_branch_excludes()
    
    return 0


if __name__ == "__main__":
    sys.exit(main())
EOF