# Pre-commit Hooks Configuration
#
# This file defines hooks that run automatically before each commit.
# They ensure code quality and consistency across the team.
#
# Installation:
#   pip install pre-commit
#   pre-commit install
#
# Run manually on all files:
#   pre-commit run --all-files
#
# Update hooks to latest versions:
#   pre-commit autoupdate
#
# Skip hooks (emergency only):
#   git commit --no-verify

# Minimum pre-commit version required
minimum_pre_commit_version: '2.20.0'

# Default settings for all hooks
default_language_version:
  python: python3.14.0
  node: '22.21.0'

# Exclude these files/directories from all hooks
exclude: |
  (?x)^(
    migrations/versions/.*\.py|
    \.git/.*|
    \.venv/.*|
    node_modules/.*|
    build/.*|
    dist/.*|
    \.mypy_cache/.*|
    \.pytest_cache/.*|
    \.coverage.*|
    htmlcov/.*
  )$

# ==============================================================================
# HOOKS
# ==============================================================================

repos:
  # ----------------------------------------------------------------------------
  # Pre-commit built-in hooks
  # ----------------------------------------------------------------------------
  - repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v6.0.0
    hooks:
      # Remove trailing whitespace
      - id: trailing-whitespace
        name: Trim trailing whitespace
        args: [--markdown-linebreak-ext=md]

      # Ensure files end with newline
      - id: end-of-file-fixer
        name: Fix end of files

      # Validate YAML files
      - id: check-yaml
        name: Check YAML files
        args: [--safe]
        exclude: '^\.github/workflows/.*\.yml$'  # Allow GitHub Actions specific syntax

      # Validate JSON files
      - id: check-json
        name: Check JSON files

      # Validate TOML files
      - id: check-toml
        name: Check TOML files

      # Check for large files (>500KB)
      - id: check-added-large-files
        name: Check for large files
        args: [--maxkb=500]

      # Check for merge conflicts
      - id: check-merge-conflict
        name: Check for merge conflicts

      # Check for debugger imports
      - id: debug-statements
        name: Check for debugger statements

      # Check for files that would conflict on case-insensitive filesystems
      - id: check-case-conflict
        name: Check for case conflicts

      # Prevent committing to main/master
      - id: no-commit-to-branch
        name: Prevent commits to main/master
        args: [--branch, main, --branch, master]

  # ----------------------------------------------------------------------------
  # Python: Ruff (Linter + Formatter)
  # ----------------------------------------------------------------------------
  - repo: https://github.com/astral-sh/ruff-pre-commit
    rev: v0.14.2
    hooks:
      # Run the linter
      - id: ruff
        name: Lint Python code (Ruff)
        args: [--fix, --exit-non-zero-on-fix]
      # Run the formatter
      - id: ruff-format
        name: Format Python code (Ruff)

  # ----------------------------------------------------------------------------
  # Python: Bandit (Security Linter)
  # ----------------------------------------------------------------------------
  - repo: https://github.com/PyCQA/bandit
    rev: 1.8.6
    hooks:
      - id: bandit
        name: Security check Python code (Bandit)
        args:
          - -c
          - pyproject.toml
        files: \.py$
        exclude: tests/.*\.py$

  # ----------------------------------------------------------------------------
  # Python: Docstring Coverage
  # ----------------------------------------------------------------------------
  - repo: https://github.com/econchick/interrogate
    rev: 1.7.0
    hooks:
      - id: interrogate
        name: Check Python docstring coverage
        args:
          - --fail-under=80
          - --ignore-init-method
          - --ignore-init-module
          - --ignore-magic
          - --ignore-module
          - --ignore-nested-functions
          - --exclude=tests
          - --verbose
        files: \.py$

  # ----------------------------------------------------------------------------
  # JavaScript/TypeScript: Prettier (Formatter)
  # ----------------------------------------------------------------------------
  - repo: https://github.com/pre-commit/mirrors-prettier
    rev: v4.0.0-alpha.8
    hooks:
      - id: prettier
        name: Format JS/TS/JSON/YAML (Prettier)
        types_or: [javascript, jsx, ts, tsx, json, yaml, markdown]
        args:
          - --write
          - --ignore-unknown
        files: \.(js|jsx|ts|tsx|json|yaml|yml|md)$

  # ----------------------------------------------------------------------------
  # JavaScript/TypeScript: ESLint (Linter)
  # ----------------------------------------------------------------------------
  - repo: https://github.com/pre-commit/mirrors-eslint
    rev: v9.38.0
    hooks:
      - id: eslint
        name: Lint JS/TS code (ESLint)
        files: \.(js|jsx|ts|tsx)$
        types: [file]
        args:
          - --fix
          - --max-warnings=0
        additional_dependencies:
          - eslint@8.56.0
          - '@typescript-eslint/parser@6.19.0'
          - '@typescript-eslint/eslint-plugin@6.19.0'
          - eslint-plugin-react@7.33.2
          - eslint-plugin-react-hooks@4.6.0

  # ----------------------------------------------------------------------------
  # Markdown: markdownlint
  # ----------------------------------------------------------------------------
  - repo: https://github.com/igorshubovych/markdownlint-cli
    rev: v0.45.0
    hooks:
      - id: markdownlint
        name: Lint Markdown files
        args:
          - --fix
          - --disable=MD013  # Line length (too strict)
          - --disable=MD033  # Inline HTML (sometimes needed)
        files: \.md$

  # ----------------------------------------------------------------------------
  # Secrets Detection
  # ----------------------------------------------------------------------------
  - repo: https://github.com/Yelp/detect-secrets
    rev: v1.5.0
    hooks:
      - id: detect-secrets
        name: Detect secrets in code
        args:
          - --baseline
          - .secrets.baseline
        exclude: package-lock\.json|poetry\.lock

# ==============================================================================
# CUSTOM HOOKS (Optional)
# ==============================================================================

  # ----------------------------------------------------------------------------
  # Run pytest on Python files
  # ----------------------------------------------------------------------------
  # - repo: local
  #   hooks:
  #     - id: pytest
  #       name: Run pytest
  #       entry: pytest
  #       language: system
  #       types: [python]
  #       pass_filenames: false
  #       always_run: true

  # ----------------------------------------------------------------------------
  # Check migrations are up to date
  # ----------------------------------------------------------------------------
  # - repo: local
  #   hooks:
  #     - id: check-migrations
  #       name: Check database migrations
  #       entry: alembic check
  #       language: system
  #       pass_filenames: false
  #       files: ^(app/models/.*\.py|migrations/.*\.py)$