name: build

on:
    workflow_dispatch:
    pull_request:
    push:
        branches: [main]
        paths-ignore:
            - 'example/**'

jobs:
  build:
    if: github.repository == 'mikepqr/resume-markdown'
    runs-on: ${{ matrix.os }}
    strategy:
        fail-fast: false
        matrix:
            # os: [ubuntu-latest, macos-latest, windows-latest]
            os: [ubuntu-latest, windows-latest]
    steps:
    - name: Check out repo
      uses: actions/checkout@v4

    - name: Set up uv
      uses: astral-sh/setup-uv@v5

    - name: Install package
      run: uv tool install .

    - name: Initialize resume templates
      run: resume-markdown init

    - name: Make resume
      run: resume-markdown --debug build

    - name: Rename output (Unix)
      if: runner.os != 'Windows'
      shell: bash
      run: |
          mv resume.pdf resume_${{ runner.os }}.pdf
          mv resume.html resume_${{ runner.os }}.html

    - name: Rename output (Windows)
      if: runner.os == 'Windows'
      shell: pwsh
      run: |
          Move-Item resume.pdf resume_Windows.pdf
          Move-Item resume.html resume_Windows.html

    - name: Upload artifacts
      uses: actions/upload-artifact@v4
      with:
          name: resume-${{ runner.os }}
          path: |
              *.pdf
              *.html

  update-examples:
    if: github.event_name == 'push' && github.ref == 'refs/heads/main'
    runs-on: ubuntu-latest
    needs: build
    steps:
    - name: Check out repo
      uses: actions/checkout@v4

    - name: Install ImageMagick
      run: sudo apt-get update && sudo apt-get install -y imagemagick

    - name: Download Windows artifacts
      uses: actions/download-artifact@v4
      with:
          name: resume-Windows
          path: artifacts

    - name: Update example files
      run: |
          mv artifacts/resume_Windows.pdf example/resume.pdf
          mv artifacts/resume_Windows.html example/resume.html
          convert -density 150 example/resume.pdf[0] -crop 100%x600+0+0 +repage example/resume.png

    - name: Commit changes
      run: |
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"
          git add example/resume.pdf example/resume.html example/resume.png
          git diff --staged --quiet || git commit -m "Update example outputs"
          git push
