name: Generate and cache Pixi lockfile

on:
  workflow_call:
    inputs:
      pixi-version:
        type: string
    outputs:
      cache-id:
        description: "The lock file contents"
        value: ${{ jobs.cache-pixi-lock.outputs.cache-id }}

jobs:
  cache-pixi-lock:
    name: Pixi lock
    runs-on: ubuntu-latest
    outputs:
      cache-id: ${{ steps.restore.outputs.cache-primary-key }}
    steps:
      - uses: actions/checkout@v6
        with:
          fetch-depth: 0
          submodules: recursive
      - name: Get current date
        id: date
        run: echo "date=$(date +'%Y-%m-%d')" >> "$GITHUB_OUTPUT"
      - uses: actions/cache/restore@v5
        id: restore
        with:
          path: |
            pixi.lock
          key: ${{ steps.date.outputs.date }}_${{ inputs.pixi-version }}_${{hashFiles('pixi.toml')}}
      - uses: prefix-dev/setup-pixi@v0.9.3
        if: ${{ !steps.restore.outputs.cache-hit }}
        with:
          pixi-version: ${{ inputs.pixi-version }}
          run-install: false
      - name: Run pixi lock
        if: ${{ !steps.restore.outputs.cache-hit }}
        run: pixi lock
      - uses: actions/cache/save@v5
        if: ${{ !steps.restore.outputs.cache-hit }}
        id: cache
        with:
          path: |
            pixi.lock
          key: ${{ steps.restore.outputs.cache-primary-key }}
      - name: Upload pixi.lock
        uses: actions/upload-artifact@v6
        with:
          name: pixi-lock
          path: pixi.lock
