# direnv configuration for netresearch/go-cron
# https://direnv.net/
#
# Install: brew install direnv (macOS) or apt install direnv (Ubuntu)
# Setup:   echo 'eval "$(direnv hook bash)"' >> ~/.bashrc
#          direnv allow

set -euo pipefail

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

# Optional: Load .env file if present (for local development)
dotenv_if_exists .env

echo -e "${BLUE}🔧 Verifying go-cron development environment...${NC}"

# Check Go version requirement
if ! command -v go &> /dev/null; then
    echo -e "${RED}❌ Go not found. Please install Go 1.21+${NC}"
else
    GO_VERSION=$(go version | grep -oE 'go[0-9]+\.[0-9]+' | sed 's/go//')
    echo -e "${GREEN}✅ Go $GO_VERSION detected${NC}"
fi

# Check for required development tools
check_tool() {
    local tool=$1
    local install_hint=$2

    if command -v "$tool" &> /dev/null; then
        echo -e "${GREEN}✅ $tool available${NC}"
        return 0
    else
        echo -e "${YELLOW}⚠️  $tool missing: $install_hint${NC}"
        return 1
    fi
}

echo -e "${BLUE}🛠️  Checking tools...${NC}"
check_tool "golangci-lint" "go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest"
check_tool "lefthook" "go install github.com/evilmartians/lefthook@latest"

# CRITICAL: Git hooks verification
echo ""
echo -e "${BLUE}🪝 Verifying Git hooks...${NC}"

HOOKS_CONFIGURED=false

if [[ -f ".git/hooks/pre-commit" ]] && grep -q "lefthook" ".git/hooks/pre-commit" 2>/dev/null; then
    echo -e "${GREEN}✅ Git hooks properly configured (lefthook)${NC}"
    HOOKS_CONFIGURED=true
fi

if [[ "$HOOKS_CONFIGURED" == "false" ]]; then
    echo -e "${RED}❌ Git hooks not configured!${NC}"
    echo -e "${YELLOW}   Without pre-commit hooks, your commits may fail CI${NC}"
    echo ""
    echo -e "${YELLOW}🔧 Run: make setup${NC}"
    echo ""
fi

# Add local bin to PATH for tools installed via go install
PATH_add "$(go env GOPATH)/bin"

# Project-specific environment
export CGO_ENABLED=0

echo ""
echo -e "${GREEN}✅ Development environment ready!${NC}"
echo ""
echo -e "${BLUE}📋 go-cron (robfig/cron fork)${NC}"
echo ""
echo -e "${YELLOW}🚀 Quick Commands:${NC}"
echo "  make help       # Show all available commands"
echo "  make setup      # Set up development environment with git hooks"
echo "  make test       # Run tests"
echo "  make lint       # Run linter"
echo "  make dev-check  # Run all development checks"
echo ""
