repos:
  - repo: local
    hooks:
      # Pre-commit: fast auto-formatting
      # Auto-fixes formatting and re-stages changed files
      - id: cargo-fmt
        name: cargo fmt (auto-fix)
        entry: bash -c 'set -e; cargo fmt --all && git add -u'
        language: system
        types: [rust]
        pass_filenames: false
        stages: [pre-commit]
        always_run: true

      # Pre-push: full validation
      # Verifies formatting hasn't regressed (safety check)
      - id: cargo-fmt-check
        name: cargo fmt --check
        entry: bash -c 'set -e; cargo fmt --all -- --check'
        language: system
        types: [rust]
        pass_filenames: false
        stages: [pre-push]
        always_run: false

      # Pre-push: linting with strict warnings
      # Auto-fixes issues and re-stages changed files
      - id: cargo-clippy
        name: cargo clippy (strict)
        entry: >
          bash -c '
          set -e;
          if cargo clippy --all-features --all-targets --fix --allow-staged --allow-dirty -- -D warnings -A dead_code 2>/dev/null; then
            if ! git diff --cached --quiet || ! git diff --quiet; then
              git add -u;
              if git rev-parse --verify HEAD >/dev/null 2>&1; then
                git commit --amend --no-edit --no-verify 2>/dev/null || true;
              fi;
            fi;
          fi;
          cargo clippy --all-features --all-targets -- -D warnings -A dead_code
          '
        language: system
        types: [rust]
        pass_filenames: false
        stages: [pre-push]
        always_run: false

      # Pre-push: run tests with nextest
      # Ensures code passes all tests before pushing
      # Uses nextest for faster parallel execution and better output
      - id: cargo-nextest
        name: cargo nextest run
        entry: bash -c 'set -e; cargo nextest run --all-features --profile pre-push'
        language: system
        types: [rust]
        pass_filenames: false
        stages: [pre-push]
        always_run: false
