name: Test Suite

on:
  push:
    branches: ["main"]
  pull_request:
    branches: ["main", "version-*"]

permissions:
  contents: read

jobs:
  tests:
    name: "Python ${{ matrix.python-version }}"
    runs-on: ubuntu-latest

    strategy:
      matrix:
        python-version: ["3.10", "3.11", "3.12", "3.13", "3.14"]

    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          persist-credentials: false

      - name: Install uv
        uses: astral-sh/setup-uv@08807647e7069bb48b6ef5acd8ec9567f424441b # v8.1.0
        with:
          python-version: ${{ matrix.python-version }}
          enable-cache: true

      - name: Install dependencies
        run: scripts/install

      - name: Run linting checks
        run: scripts/check

      - name: Build package & docs
        run: scripts/build

      - name: Run tests
        run: scripts/test

      - name: Enforce coverage
        run: scripts/coverage

  check:
    if: always()
    needs: [tests]
    runs-on: ubuntu-latest
    steps:
      - name: Decide whether the needed jobs succeeded or failed
        uses: re-actors/alls-green@05ac9388f0aebcb5727afa17fcccfecd6f8ec5fe # v1.2.2
        with:
          jobs: ${{ toJSON(needs) }}

  docs-cloudflare-preview:
    name: Docs preview (Cloudflare)
    if: github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository
    runs-on: ubuntu-latest

    permissions:
      contents: read
      pull-requests: write

    environment:
      name: cloudflare

    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          persist-credentials: false

      - name: Install uv
        uses: astral-sh/setup-uv@08807647e7069bb48b6ef5acd8ec9567f424441b # v8.1.0
        with:
          python-version: "3.11"
          enable-cache: false

      - name: Install dependencies
        run: scripts/install

      - name: Build docs
        run: uv run zensical build --clean

      - uses: cloudflare/wrangler-action@ebbaa1584979971c8614a24965b4405ff95890e0 # v4.0.0
        id: deploy
        with:
          apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
          accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
          command: versions upload --tag pr-${{ github.event.pull_request.number }}

      - name: Comment preview URL
        uses: marocchino/sticky-pull-request-comment@0ea0beb66eb9baf113663a64ec522f60e49231c0 # v3.0.4
        with:
          message: |
            **Docs preview:** ${{ steps.deploy.outputs.deployment-url }}
