#!/bin/bash

# rstmdb pre-commit hook
# Runs formatting check, clippy, and tests before allowing commit

set -e

RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'

echo -e "${YELLOW}Running pre-commit checks...${NC}"
echo ""

# 1. Check formatting
echo -e "${YELLOW}[1/3] Checking formatting...${NC}"
if ! cargo fmt --all -- --check; then
    echo -e "${RED}Formatting check failed!${NC}"
    echo "Run 'cargo fmt' to fix formatting issues."
    exit 1
fi
echo -e "${GREEN}Formatting OK${NC}"
echo ""

# 2. Run clippy
echo -e "${YELLOW}[2/3] Running clippy...${NC}"
if ! cargo clippy --all-targets --all-features -- -D warnings; then
    echo -e "${RED}Clippy found issues!${NC}"
    echo "Fix the warnings above before committing."
    exit 1
fi
echo -e "${GREEN}Clippy OK${NC}"
echo ""

# 3. Run tests
echo -e "${YELLOW}[3/3] Running tests...${NC}"
if ! cargo test --workspace; then
    echo -e "${RED}Tests failed!${NC}"
    exit 1
fi
echo -e "${GREEN}Tests OK${NC}"
echo ""

echo -e "${GREEN}All pre-commit checks passed!${NC}"
