#!/bin/bash
# Fenrir Validation Setup Script
#
# Sets up the validation tools and pre-commit hooks for Fenrir development.
# Run this once after cloning the repository.

set -e

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

echo -e "${GREEN}Fenrir Development Environment Setup${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

# Make validation scripts executable
echo -e "\n${YELLOW}1. Making validation scripts executable...${NC}"
chmod +x tools/validate_syntax.py
chmod +x tools/validate_pep8.py
chmod +x tools/validate_release.py
chmod +x tools/cleanup_cache.py
chmod +x tools/pre-commit-hook
chmod +x tools/install_validation_hook.sh
chmod +x tools/pre-commit-composite
echo -e "${GREEN}✓ Scripts are now executable${NC}"

# Install pre-commit hook
echo -e "\n${YELLOW}2. Installing composite pre-commit hook...${NC}"
echo -e "${YELLOW}This preserves existing version management functionality.${NC}"

# Use the safe installation script
if ./tools/install_validation_hook.sh; then
    echo -e "${GREEN}✓ Composite pre-commit hook installed${NC}"
else
    echo -e "${RED}⚠ Hook installation encountered issues${NC}"
    echo "  You can install manually with: ./tools/install_validation_hook.sh"
fi

# Test validation tools
echo -e "\n${YELLOW}3. Testing validation tools...${NC}"

# Test syntax validator
if python3 tools/validate_syntax.py --check-only >/dev/null 2>&1; then
    echo -e "${GREEN}✓ Syntax validator working${NC}"
else
    echo -e "${RED}⚠ Syntax validator found issues${NC}"
    echo "  Run: python3 tools/validate_syntax.py --fix"
fi

# Test pre-commit hook
if ./tools/pre-commit-hook >/dev/null 2>&1; then
    echo -e "${GREEN}✓ Pre-commit hook working${NC}"
else
    echo -e "${RED}⚠ Pre-commit hook found issues${NC}"
    echo "  This is normal if there are uncommitted changes"
fi

# Verify dependencies for full validation
echo -e "\n${YELLOW}4. Checking validation dependencies...${NC}"
missing_deps=()

if ! command -v python3 >/dev/null 2>&1; then
    missing_deps+=("python3")
fi

if ! python3 -c "import ast" >/dev/null 2>&1; then
    missing_deps+=("python3-ast")
fi

if [ ${#missing_deps[@]} -eq 0 ]; then
    echo -e "${GREEN}✓ All validation dependencies available${NC}"
else
    echo -e "${RED}Missing dependencies: ${missing_deps[*]}${NC}"
fi

# Final instructions
echo -e "\n${GREEN}Setup complete!${NC}"
echo ""
echo "Development workflow:"
echo "  1. Make your changes"
echo "  2. python3 tools/validate_syntax.py --fix"
echo "  3. python3 tools/validate_release.py --quick" 
echo "  4. git add . && git commit (pre-commit hook runs automatically)"
echo ""
echo "Before releases:"
echo "  python3 tools/validate_release.py"
echo "  cat RELEASE_CHECKLIST.md"
echo ""
echo -e "${YELLOW}Tip: The pre-commit hook will now run automatically on every commit${NC}"
echo -e "${YELLOW}     and prevent syntax errors from being committed.${NC}"