#!/usr/bin/env bash
# Setup pre-commit hooks for TypeDialog project
set -e

echo "=== Pre-commit Hook Setup ==="
echo ""

# Check if pre-commit is installed
if ! command -v pre-commit &> /dev/null; then
    echo "❌ pre-commit is not installed"
    echo ""
    echo "Install with:"
    echo "  pip install pre-commit"
    echo "  # OR"
    echo "  pipx install pre-commit"
    echo "  # OR (macOS)"
    echo "  brew install pre-commit"
    echo ""
    exit 1
fi

echo "✓ pre-commit found: $(pre-commit --version)"
echo ""

# Install hooks
echo "Installing pre-commit hooks..."
pre-commit install

echo ""
echo "✓ Pre-commit hooks installed successfully"
echo ""

# Run hooks on all files as a test
echo "Testing hooks on all files (this may take a while)..."
echo ""

if pre-commit run --all-files; then
    echo ""
    echo "✅ All hooks passed!"
else
    echo ""
    echo "⚠️  Some hooks failed. Please fix the issues above."
    echo ""
    echo "Common fixes:"
    echo "  - Rust: cargo fmt --all"
    echo "  - Markdown: just dev::lint-markdown-fix"
    echo "  - Shell: shellcheck <file> and fix issues"
    echo ""
    exit 1
fi

echo ""
echo "=== Setup Complete ==="
echo ""
echo "Pre-commit hooks will now run automatically before every commit."
echo ""
echo "To run manually:"
echo "  pre-commit run --all-files"
echo ""
echo "To skip hooks (emergency only):"
echo "  git commit --no-verify"
echo ""
echo "See docs/PRE-COMMIT-SETUP.md for more information."
