name: "Python CI"

on:
  # Run on all branch pushes to ensure tests always run
  push:
  # All pull requests trigger the workflow
  pull_request:
  release:
    types: [published]

jobs:
  linters:
    name: "Code Quality - Linting"
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Install uv
        uses: astral-sh/setup-uv@v6

      - name: Install Python
        run: uv python install 3.11

      - name: Create virtual environment
        run: uv venv

      - name: Install dependencies
        run: uv pip install -e .[dev,test]

      - name: Lint with ruff
        run: uv run ruff check .

      - name: Type check with mypy
        run: uv run mypy src/

  build:
    needs: [linters]
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, macos-latest]
        python-version: ['3.11']
    steps:
      - uses: actions/checkout@v4

      - name: Install uv
        uses: astral-sh/setup-uv@v6

      - name: Install Python ${{ matrix.python-version }}
        run: uv python install ${{ matrix.python-version }}

      - name: Cache dependencies
        uses: actions/cache@v3
        with:
          path: ~/.cache/uv
          key: ${{ runner.os }}-uv-${{ hashFiles('**/pyproject.toml') }}
          restore-keys: |
            ${{ runner.os }}-uv-

      - name: Create virtual environment
        run: uv venv

      - name: Install dependencies
        run: uv pip install -e .[dev,test]

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

      - name: Upload coverage reports to Codecov
        uses: codecov/codecov-action@v5
        with:
          token: ${{ secrets.CODECOV_TOKEN }}

      - name: Build package
        run: uv build

      - name: Upload artifacts
        uses: actions/upload-artifact@v4
        with:
          name: dist-${{ matrix.os }}-py${{ matrix.python-version }}
          path: dist/

  publish:
    runs-on: ubuntu-latest
    needs: [build]
    if: github.event_name == 'release'
    name: Publish Package

    # Note: To use environments for PyPI publishing, you would need to:
    # 1. Create a 'release' environment in GitHub repository settings
    # 2. Add 'environment: release' here
    # For now, we'll use trusted publishing with OIDC
    permissions:
      id-token: write # Required for trusted publishing

    steps:
      - name: Download artifacts
        uses: actions/download-artifact@v4
        with:
          pattern: dist-*
          merge-multiple: true
          path: dist/
      - name: "Push to PyPI"
        uses: "pypa/gh-action-pypi-publish@release/v1"
        with:
          user: "__token__"
          password: "${{ secrets.PYPI_API_TOKEN }}"