pre-commit:
  commands:
    syntax-tree:
      glob: "*.rb,*.rbi,*.rbs"
      run: bundle exec stree write {staged_files}
      stage_fixed: true
      exclude: |
        vendor/
        tmp/
        log/

    sorbet:
      run: bundle exec srb tc
      pass_filenames: false
      fail_text: "Sorbet type checking failed"

    tapioca-gems:
      glob: "{Gemfile,Gemfile.lock}"
      run: bundle exec tapioca gem --verify
      pass_filenames: false
      fail_text: "RBI files are out of date. Please run: bundle exec tapioca gems"

    trailing-whitespace:
      glob: "*.{rb,yml,yaml,json,md,rbi,rbs,gemspec}"
      run: sed -i 's/[[:space:]]*$//' {staged_files}
      stage_fixed: true

    rspec:
      run: bundle exec rspec
      fail_text: "RSpec tests failed"

    bundler-audit:
      run: bundle exec bundler-audit check --update
      pass_filenames: false
      fail_text: "Security vulnerabilities found"

    yaml-lint:
      glob: "*.{yml,yaml}"
      run: |
        for file in {staged_files}; do
          echo "Checking $file"
          ruby -e "require 'yaml'; YAML.load_file('$file')" || exit 1
        done
      fail_text: "YAML syntax error"

    json-lint:
      glob: "*.json"
      exclude:
        - .devcontainer/devcontainer.json
        - .vscode/*.json
      run: |
        for file in {staged_files}; do
          echo "Checking $file"
          ruby -e "require 'json'; JSON.parse(File.read('$file'))" || exit 1
        done
      fail_text: "JSON syntax error"

    gemspec-lint:
      glob: "*.gemspec"
      run: |
        for file in {staged_files}; do
          echo "Checking $file"
          ruby -c "$file" || exit 1
        done
      fail_text: "Gemspec syntax error"

    no-debug:
      glob: "*.rb"
      run: |
        for file in {staged_files}; do
          echo "Checking $file"
          # Filter out comment lines first, then check for debug statements
          if grep -v "^\s*#" "$file" | grep -n "binding\.pry\|debugger\|byebug\|puts.*DEBUG\|^\s*p\s\|^\s*pp\s"; then
            exit 1
          fi
        done
      fail_text: "Debug statements found - please remove before committing"

    file-size:
      glob: "*"
      run: |
        for file in {staged_files}; do
          if [ -f "$file" ]; then
            size=$(stat -c%s "$file" 2>/dev/null || stat -f%z "$file" 2>/dev/null)
            if [ "$size" -gt 2097152 ]; then
              echo "File $file is larger than 1MB"
              exit 1
            fi
          fi
        done
      fail_text: "Large files detected (>1MB)"

pre-push:
  commands:
    rspec:
      run: bundle exec rspec
      fail_text: "Tests failed"
