#!/usr/bin/env bash
# Formata e verifica os arquivos Python staged com `ruff check --fix` e `ruff format` antes do commit.
#
# Instalação (uma vez por clone):
#   git config core.hooksPath .githooks
#
set -euo pipefail

cd "$(git rev-parse --show-toplevel)"

mapfile -t files < <(git diff --cached --name-only --diff-filter=ACMR -- '*.py')

if [ "${#files[@]}" -eq 0 ]; then
    exit 0
fi

if ! uv run ruff check --fix "${files[@]}"; then
    echo "pre-commit: ruff check falhou — commit abortado." >&2
    exit 1
fi

if ! uv run ruff format "${files[@]}"; then
    echo "pre-commit: ruff format falhou — commit abortado." >&2
    exit 1
fi

git add -- "${files[@]}"
