name: Build and Test
on:
  workflow_call:
    inputs:
      python-version:
        required: true
        type: string
      matplotlib-version:
        required: true
        type: string

env:
  LC_ALL: en_US.UTF-8
  LANG: en_US.UTF-8

jobs:
  build-ultraplot:
    name: Test Python ${{ inputs.python-version }} with MPL ${{ inputs.matplotlib-version }}
    runs-on: ubuntu-latest
    timeout-minutes: 60
    defaults:
      run:
        shell: bash -el {0}
    steps:
      - uses: actions/checkout@v6
        with:
          fetch-depth: 0

      - uses: mamba-org/setup-micromamba@v2.0.7
        with:
          environment-file: ./environment.yml
          init-shell: bash
          create-args: >-
            --verbose
            python=${{ inputs.python-version }}
            matplotlib=${{ inputs.matplotlib-version }}
          cache-environment: true
          cache-downloads: false

      - name: Build Ultraplot
        run: |
          pip install --no-build-isolation --no-deps .

      - name: Test Ultraplot
        run: |
          pytest -n auto --cov=ultraplot --cov-branch --cov-report term-missing --cov-report=xml ultraplot

      - name: Upload coverage reports to Codecov
        uses: codecov/codecov-action@v5
        with:
          token: ${{ secrets.CODECOV_TOKEN }}
          slug: Ultraplot/ultraplot

  compare-baseline:
    name: Compare baseline Python ${{ inputs.python-version }} with MPL ${{ inputs.matplotlib-version }}
    runs-on: ubuntu-latest
    defaults:
      run:
        shell: bash -el {0}
    steps:
      - uses: actions/checkout@v6

      - uses: mamba-org/setup-micromamba@v2.0.7
        with:
          environment-file: ./environment.yml
          init-shell: bash
          create-args: >-
            --verbose
            python=${{ inputs.python-version }}
            matplotlib=${{ inputs.matplotlib-version }}
          cache-environment: true
          cache-downloads: false

      # Cache Baseline Figures (Restore step)
      - name: Cache Baseline Figures
        id: cache-baseline
        uses: actions/cache@v4
        with:
          path: ./ultraplot/tests/baseline # The directory to cache
          # Key is based on OS, Python/Matplotlib versions, and the PR number
          key: ${{ runner.os }}-baseline-pr-${{ github.event.pull_request.number }}-${{ inputs.python-version }}-${{ inputs.matplotlib-version }}
          restore-keys: |
            ${{ runner.os }}-baseline-pr-${{ github.event.pull_request.number }}-${{ inputs.python-version }}-${{ inputs.matplotlib-version }}-

      # Conditional Baseline Generation (Only runs on cache miss)
      - name: Generate baseline from main
        # Skip this step if the cache was found (cache-hit is true)
        if: steps.cache-baseline.outputs.cache-hit != 'true'
        run: |
          mkdir -p ultraplot/tests/baseline
          # Checkout the base branch (e.g., 'main') to generate the official baseline
          git fetch origin ${{ github.event.pull_request.base.sha }}
          git checkout ${{ github.event.pull_request.base.sha }}

          # Install the Ultraplot version from the base branch's code
          pip install --no-build-isolation --no-deps .

          # Generate the baseline images and hash library
          python -c "import ultraplot as plt; plt.config.Configurator()._save_yaml('ultraplot.yml')"
          pytest -x -W ignore \
            --mpl-generate-path=./ultraplot/tests/baseline/ \
            --mpl-default-style="./ultraplot.yml"\
            ultraplot/tests

          # Return to the PR branch for the rest of the job
          git checkout ${{ github.sha }}

      # Image Comparison (Uses cached or newly generated baseline)
      - name: Image Comparison Ultraplot
        run: |
          # Re-install the Ultraplot version from the current PR branch
          pip install --no-build-isolation --no-deps .

          mkdir -p results
          python -c "import ultraplot as plt; plt.config.Configurator()._save_yaml('ultraplot.yml')"
          pytest -x -W ignore \
            --mpl \
            --mpl-baseline-path=./ultraplot/tests/baseline \
            --mpl-results-path=./results/ \
            --mpl-generate-summary=html \
            --mpl-default-style="./ultraplot.yml" \
            ultraplot/tests

      # Return the html output of the comparison even if failed
      - name: Upload comparison failures
        if: always()
        uses: actions/upload-artifact@v6
        with:
          name: failed-comparisons-${{ inputs.python-version }}-${{ inputs.matplotlib-version }}-${{ github.sha }}
          path: results/*
