name: docs

on:
  push:
    branches:
      - master
  workflow_dispatch:

env:
  PYTHON_VERSION: "3.12"

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Install uv
        uses: astral-sh/setup-uv@v3
        with:
          enable-cache: true

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

      - name: Cache folder for cartes
        uses: actions/cache@v4
        id: cache-folder
        with:
          path: |
            ~/.cache/cartes/
          key: cartes-${{ runner.os }}

      - name: Ubuntu system dependencies
        run: |
          sudo apt update
          sudo apt install -y libgdal-dev libgeos-dev libproj-dev proj-bin proj-data

      - name: Install the project
        run: uv sync --all-extras --dev

      - name: Populate cache if need be
        if: steps.cache-folder.outputs.cache-hit != 'true'
        env:
          LD_LIBRARY_PATH: /usr/local/lib
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          uv run python -c "from cartes.atlas import default, world_atlas"

      - name: Build pages
        env:
          LD_LIBRARY_PATH: /usr/local/lib
        run: |
          # reuse the same cache folder as tests
          cd docs && CARTES_CACHE=../tests/cache uv run make html

      - name: Deploy pages
        uses: peaceiris/actions-gh-pages@v4
        with:
          deploy_key: ${{ secrets.ACTIONS_DEPLOY_KEY }}
          external_repository: cartes-viz/cartes-viz.github.io
          publish_branch: master
          publish_dir: docs/build/html
