name: CI

on:
  push:
    branches: [main, master]
  pull_request:
    branches: [main, master]

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  lint:
    name: Lint & Format
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Install uv
        uses: astral-sh/setup-uv@v5
        with:
          version: "0.6.x"

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: "3.13"

      - name: Install dependencies
        run: uv sync --group dev

      - name: Run linter
        run: uv run ruff check privacyforms_pdf tests

      - name: Check formatting
        run: uv run ruff format --check privacyforms_pdf tests

  type-check:
    name: Type Check
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Install uv
        uses: astral-sh/setup-uv@v5
        with:
          version: "0.6.x"

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: "3.13"

      - name: Install dependencies
        run: uv sync --group dev

      - name: Run type checker
        run: uv run ty check

  test:
    name: Test (Python ${{ matrix.python-version }})
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, macos-latest]
        python-version: ["3.13", "3.13t", "3.14", "3.14t"]

    steps:
      - uses: actions/checkout@v4

      - name: Install pdfcpu (Ubuntu)
        if: runner.os == 'Linux'
        run: |
          wget https://github.com/pdfcpu/pdfcpu/releases/download/v0.11.1/pdfcpu_0.11.1_Linux_x86_64.tar.xz
          tar -xf pdfcpu_0.11.1_Linux_x86_64.tar.xz
          sudo mv pdfcpu_0.11.1_Linux_x86_64/pdfcpu /usr/local/bin/
          pdfcpu version

      - name: Install pdfcpu (macOS)
        if: runner.os == 'macOS'
        run: |
          brew install pdfcpu
          pdfcpu version

      - name: Install uv
        uses: astral-sh/setup-uv@v5
        with:
          version: "0.6.x"
          python-version: ${{ matrix.python-version }}

      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v5
        with:
          python-version: ${{ matrix.python-version }}

      - name: Install dependencies
        run: uv sync --group dev

      - name: Run tests
        run: uv run pytest -v --cov --cov-report=xml

      - name: Upload coverage to Codecov
        if: matrix.os == 'ubuntu-latest' && matrix.python-version == '3.13'
        uses: codecov/codecov-action@v5
        with:
          files: ./coverage.xml
          fail_ci_if_error: false
          verbose: true
