#!/bin/bash
#
# Pre-commit hook for fgumi
#
# Runs cargo ci-fmt and ci-lint before each commit.
# ci-test is skipped by default (too slow) but can be enabled with:
#   FGUMI_PRECOMMIT_TEST=1 git commit
#
# To bypass the hook entirely (use sparingly):
#   git commit --no-verify

set -e

echo "Running pre-commit checks..."

# Check if we're in the right directory
if [[ ! -f "Cargo.toml" ]]; then
    echo "Error: Not in fgumi root directory"
    exit 1
fi

# Run formatting check
echo "==> cargo ci-fmt"
if ! cargo ci-fmt; then
    echo ""
    echo "Formatting check failed!"
    echo "Run 'cargo fmt' to fix formatting issues."
    exit 1
fi

# Run linting check
echo "==> cargo ci-lint"
if ! cargo ci-lint; then
    echo ""
    echo "Lint check failed!"
    echo "Fix the clippy warnings above before committing."
    exit 1
fi

# Check for bare SAM-tag byte literals
echo "==> cargo ci-tag-literals"
if ! cargo ci-tag-literals; then
    echo ""
    echo "Bare SAM-tag byte literal check failed!"
    echo "Replace the flagged literals with SamTag::XX constants, or add them to the"
    echo "PAYLOAD_ALLOWLIST in crates/xtask/src/check_tag_literals.rs with a justification."
    exit 1
fi

# Optionally run tests
if [[ "${FGUMI_PRECOMMIT_TEST:-0}" == "1" ]]; then
    echo "==> cargo ci-test"
    if ! cargo ci-test; then
        echo ""
        echo "Tests failed!"
        exit 1
    fi
else
    echo "==> Skipping ci-test (set FGUMI_PRECOMMIT_TEST=1 to enable)"
fi

echo ""
echo "Pre-commit checks passed!"
