#!/bin/bash
# Safe Installation of Fenrir Validation Hook
#
# This script safely installs the composite pre-commit hook that combines
# your existing version management with new code quality validation.

set -e

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

echo -e "${BLUE}Fenrir Validation Hook Installation${NC}"
echo "===================================="

# Check we're in the right directory
if [ ! -f "CLAUDE.md" ] || [ ! -d "src/fenrirscreenreader" ]; then
    echo -e "${RED}Error: Must be run from Fenrir project root directory${NC}"
    exit 1
fi

# Check if there's already a pre-commit hook
if [ -f ".git/hooks/pre-commit" ]; then
    echo -e "\n${YELLOW}Existing pre-commit hook detected${NC}"
    
    # Check if it's a symlink (our validation hook) or a regular file (version hook)
    if [ -L ".git/hooks/pre-commit" ]; then
        echo -e "${YELLOW}Current hook appears to be our validation hook (symlink)${NC}"
        read -p "Replace with composite hook that includes version management? (y/N): " -n 1 -r
        echo
        if [[ ! $REPLY =~ ^[Yy]$ ]]; then
            echo -e "${YELLOW}Installation cancelled${NC}"
            exit 0
        fi
        rm .git/hooks/pre-commit
    else
        echo -e "${GREEN}Current hook appears to be the version management hook (regular file)${NC}"
        
        # Back up the existing hook
        backup_name=".git/hooks/pre-commit.backup.$(date +%Y%m%d_%H%M%S)"
        cp .git/hooks/pre-commit "$backup_name"
        echo -e "${GREEN}✓ Existing hook backed up to: $backup_name${NC}"
        
        # Verify the backup contains version management code
        if grep -q "versionFile" "$backup_name"; then
            echo -e "${GREEN}✓ Backup contains version management logic${NC}"
        else
            echo -e "${YELLOW}⚠ Backup doesn't appear to contain version management logic${NC}"
            echo -e "${YELLOW}  You may need to manually restore version management functionality${NC}"
        fi
        
        read -p "Install composite hook (version management + validation)? (Y/n): " -n 1 -r
        echo
        if [[ $REPLY =~ ^[Nn]$ ]]; then
            echo -e "${YELLOW}Installation cancelled${NC}"
            exit 0
        fi
    fi
else
    echo -e "${YELLOW}No existing pre-commit hook found${NC}"
    read -p "Install composite hook? (Y/n): " -n 1 -r
    echo
    if [[ $REPLY =~ ^[Nn]$ ]]; then
        echo -e "${YELLOW}Installation cancelled${NC}"
        exit 0
    fi
fi

# Install the composite hook
echo -e "\n${YELLOW}Installing composite pre-commit hook...${NC}"
cp tools/pre-commit-composite .git/hooks/pre-commit
chmod +x .git/hooks/pre-commit
echo -e "${GREEN}✓ Composite hook installed${NC}"

# Test the hook
echo -e "\n${YELLOW}Testing the composite hook...${NC}"
if ./.git/hooks/pre-commit >/dev/null 2>&1; then
    echo -e "${GREEN}✓ Composite hook test passed${NC}"
else
    echo -e "${RED}⚠ Composite hook test found issues (this may be normal)${NC}"
    echo "  Run manually to see details: ./.git/hooks/pre-commit"
fi

# Final instructions
echo -e "\n${GREEN}Installation Complete!${NC}"
echo ""
echo "Your composite pre-commit hook now provides:"
echo "  1. ✓ Version management (existing functionality preserved)"
echo "  2. ✓ Python syntax validation" 
echo "  3. ✓ Core module import testing"
echo "  4. ✓ Common issue detection"
echo ""
echo "Development workflow:"
echo "  • Make your changes"
echo "  • git add . && git commit"
echo "  • Hook runs automatically (version update + validation)"
echo ""
echo "Manual validation (optional):"
echo "  • python3 tools/validate_syntax.py --fix"
echo "  • python3 tools/validate_release.py --quick"
echo ""
echo -e "${BLUE}Environment variables:${NC}"
echo -e "${BLUE}  SKIP_VERSION_UPDATE=1    Skip version management${NC}"
echo ""
if [ -f ".git/hooks/pre-commit.backup."* ]; then
    echo -e "${YELLOW}Note: Your original hook is backed up and can be restored if needed${NC}"
fi