name: CI

on:
  push:
    branches:
      - main
    tags:
      - '*'
  pull_request:
  workflow_dispatch:

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

      - name: Set up Python 3.14
        uses: actions/setup-python@v6
        with:
          python-version: "3.14"

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

      - name: Install dependencies
        run: uv sync --all-extras

      - name: Run ruff format check
        run: uv run ruff format --check .

      - name: Run ruff lint
        run: uv run ruff check .

      - name: Run mypy type checking
        run: uv run mypy src/crump --install-types --non-interactive || true
        continue-on-error: true

  test:
    name: Test on ${{ matrix.os }} / Python ${{ matrix.python-version }}
    runs-on: ${{ matrix.os }}
    permissions:
      contents: write
      pull-requests: write
      checks: write
    strategy:
      fail-fast: false
      matrix:
        include:
          # Test all Python versions on Ubuntu
          - os: ubuntu-latest
            python-version: "3.10"
          - os: ubuntu-latest
            python-version: "3.11"
          - os: ubuntu-latest
            python-version: "3.12"
          - os: ubuntu-latest
            python-version: "3.13"
          - os: ubuntu-latest
            python-version: "3.14"
          # Test latest Python on other OSes
          - os: windows-latest
            python-version: "3.14"
          - os: macos-latest
            python-version: "3.14"

    steps:
      - uses: actions/checkout@v6

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

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

      - name: Install dependencies
        run: uv sync --all-extras

      - name: Run tests with coverage
        run: uv run pytest --cov=crump --cov-report=xml --cov-report=term --junitxml=test-results.xml

      - name: Upload test results
        if: always() && matrix.os == 'ubuntu-latest' && matrix.python-version == '3.14'
        uses: actions/upload-artifact@v6
        with:
          name: test-results-linux-3.14
          path: test-results.xml

      - name: Publish Test Report
        if: always() && matrix.os == 'ubuntu-latest' && matrix.python-version == '3.14'
        uses: EnricoMi/publish-unit-test-result-action@v2
        with:
          files: test-results.xml
          check_name: 'Test Results (Linux 3.14)'

      - name: Upload coverage to Codecov
        if: matrix.os == 'ubuntu-latest' && matrix.python-version == '3.14'
        uses: codecov/codecov-action@v5
        with:
          files: ./coverage.xml
          fail_ci_if_error: false
          token: ${{ secrets.CODECOV_TOKEN }}

      - name: Coverage comment
        if: matrix.os == 'ubuntu-latest' && matrix.python-version == '3.14' && github.event_name == 'pull_request'
        uses: py-cov-action/python-coverage-comment-action@v3
        with:
          GITHUB_TOKEN: ${{ github.token }}
          COVERAGE_DATA_BRANCH: ${{ github.head_ref || github.ref_name }}
          MINIMUM_GREEN: 80
          MINIMUM_ORANGE: 70

  build:
    name: Build package
    runs-on: ubuntu-latest
    needs: [lint, test]
    steps:
      - uses: actions/checkout@v6

      - name: Set up Python 3.14
        uses: actions/setup-python@v6
        with:
          python-version: "3.14"

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

      - name: Install build dependencies
        run: uv pip install --system build hatchling

      - name: Build package
        run: python -m build

      - name: Upload artifacts
        uses: actions/upload-artifact@v6
        with:
          name: dist
          path: dist/

  docs:
    name: Deploy Documentation
    runs-on: ubuntu-latest
    if: github.ref == 'refs/heads/main'
    permissions:
      contents: write
    steps:
      - uses: actions/checkout@v6

      - name: Set up Python 3.14
        uses: actions/setup-python@v6
        with:
          python-version: "3.14"

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

      - name: Install dependencies
        run: uv sync --all-extras

      - name: Build and deploy docs
        run: uv run mkdocs gh-deploy --force

  publish-and-release:
    name: Publish to PyPI and Create GitHub Release
    environment: pypi
    runs-on: ubuntu-latest
    needs: [lint, test, build]
    if: startsWith(github.ref, 'refs/tags/')
    permissions:
      id-token: write  # Required for trusted publishing
      contents: write  # Required for creating releases
    steps:
      - name: Checkout code
        uses: actions/checkout@v6

      - name: Download artifacts
        uses: actions/download-artifact@v7
        with:
          name: dist
          path: dist/

      - name: Publish to PyPI
        uses: pypa/gh-action-pypi-publish@release/v1
        continue-on-error: true
        with:
          password: ${{ secrets.PYPI_API_TOKEN }}

      - name: Create GitHub Release
        uses: softprops/action-gh-release@v2
        continue-on-error: true
        with:
          files: dist/*
          generate_release_notes: true
          draft: false
          prerelease: false
