#!/bin/bash

# Setup Git Hooks for Wasot Compiler
# Automatically installs the pre-commit script as a git hook

set -e

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

echo -e "${BLUE}🔧 Setting up Git Hooks for Wasot Compiler${NC}"
echo "=========================================="

# Check if we're in a git repository
if [ ! -d ".git" ]; then
    echo -e "${RED}❌ Error: This is not a git repository${NC}"
    echo "Run 'git init' first to initialize a git repository"
    exit 1
fi

# Check if we're in the right directory
if [ ! -f "Cargo.toml" ]; then
    echo -e "${RED}❌ Error: This script must be run from the project root directory${NC}"
    exit 1
fi

# Check if pre-commit script exists
if [ ! -f "scripts/pre-commit.sh" ]; then
    echo -e "${RED}❌ Error: Pre-commit script not found at scripts/pre-commit.sh${NC}"
    exit 1
fi

# Create .git/hooks directory if it doesn't exist
mkdir -p .git/hooks

# Backup existing pre-commit hook if it exists
if [ -f ".git/hooks/pre-commit" ]; then
    echo -e "${YELLOW}⚠️  Existing pre-commit hook found, backing up...${NC}"
    mv .git/hooks/pre-commit .git/hooks/pre-commit.backup.$(date +%s)
    echo -e "${GREEN}✅ Backed up existing hook${NC}"
fi

# Create the pre-commit hook
cat > .git/hooks/pre-commit << 'EOF'
#!/bin/bash

# Git Pre-Commit Hook for Wasot Compiler
# Automatically runs quality checks before commit

# Change to project root directory
cd "$(git rev-parse --show-toplevel)"

# Run the pre-commit script
./scripts/pre-commit.sh

# If pre-commit script fails, prevent commit
if [ $? -ne 0 ]; then
    echo ""
    echo "🚫 Commit aborted due to pre-commit check failures"
    echo "Fix the issues above and try again"
    exit 1
fi
EOF

# Make the hook executable
chmod +x .git/hooks/pre-commit

echo -e "${GREEN}✅ Pre-commit hook installed successfully!${NC}"
echo ""
echo -e "${BLUE}🎉 Setup Complete!${NC}"
echo ""
echo -e "${GREEN}What happens now:${NC}"
echo "   - Every 'git commit' will automatically run quality checks"
echo "   - Code will be auto-formatted and fixed when possible"
echo "   - Commits will be blocked if critical issues are found"
echo ""
echo -e "${BLUE}Manual usage:${NC}"
echo "   ./scripts/pre-commit.sh          # Run checks manually"
echo "   ./scripts/ci/fix_all.sh         # Fix all issues automatically"
echo "   ./scripts/ci/linter.sh          # Comprehensive code analysis"
echo ""
echo -e "${YELLOW}To bypass the hook (not recommended):${NC}"
echo "   git commit --no-verify"
echo ""
echo -e "${BLUE}To uninstall the hook:${NC}"
echo "   rm .git/hooks/pre-commit"

# Test the hook
echo ""
echo -e "${BLUE}🧪 Testing the pre-commit hook...${NC}"
if ./scripts/pre-commit.sh > /dev/null 2>&1; then
    echo -e "${GREEN}✅ Pre-commit hook test passed!${NC}"
else
    echo -e "${YELLOW}⚠️  Pre-commit hook test had issues, but hook is installed${NC}"
    echo "Run './scripts/pre-commit.sh' to see detailed output"
fi

echo ""
echo -e "${GREEN}🚀 Git hooks are now active!${NC}"