name: Restore Python
inputs:
  python-version:
    description: Python version to restore
    required: true
    type: string
  cache-key:
    description: Cache key to use
    required: true
    type: string
outputs:
  python-version:
    description: Python version restored
    value: ${{ steps.python.outputs.python-version }}
runs:
  using: "composite"
  steps:
    - name: Set up Python ${{ inputs.python-version }}
      id: python
      uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0
      with:
        python-version: ${{ inputs.python-version }}
    - name: Restore Python virtual environment
      id: cache-venv
      uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
      with:
        path: venv
        # yamllint disable-line rule:line-length
        key: ${{ runner.os }}-${{ steps.python.outputs.python-version }}-venv-${{ inputs.cache-key }}
    - name: Set up uv
      # Only needed on cache miss to populate the venv. ``uv pip install``
      # detects the activated venv via ``VIRTUAL_ENV`` so the venv layout
      # downstream jobs rely on is preserved.
      if: steps.cache-venv.outputs.cache-hit != 'true'
      uses: astral-sh/setup-uv@c771a70e6277c0a99b617c7a806ffedaca235ff9 # v9.0.0
      with:
        enable-cache: true
        # Pull request saves land in per-PR scopes nothing else can
        # reuse; dev pushes seed the shared copy instead.
        save-cache: ${{ github.event_name != 'pull_request' }}
        # Pin uv version so the action does not have to fetch the
        # manifest from raw.githubusercontent.com on every cache
        # miss; that fetch flakes on Windows runners.
        version: "0.11.15"
    - name: Create Python virtual environment
      if: steps.cache-venv.outputs.cache-hit != 'true' && runner.os != 'Windows'
      shell: bash
      run: |
        python -m venv venv
        source venv/bin/activate
        python --version
        uv pip install -r requirements.txt -r requirements_test.txt
        uv pip install -e .
    - name: Create Python virtual environment
      if: steps.cache-venv.outputs.cache-hit != 'true' && runner.os == 'Windows'
      shell: bash
      run: |
        python -m venv venv
        source ./venv/Scripts/activate
        python --version
        uv pip install -r requirements.txt -r requirements_test.txt
        uv pip install -e .
