name: Release

on:
  push:
    tags:
      - "v[0-9]+.[0-9]+.[0-9]+" # Only create releases on matching tags following semver X.Y.Z
  workflow_dispatch:
    inputs:
      dry_run:
        description: "Dry run (no release)"
        required: true
        default: true
        type: boolean

jobs:
  scrape-song-list:
    name: Scrape USDB song list
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: "3.14"
      - name: Set up uv
        uses: astral-sh/setup-uv@v7
      - name: Scrape USDB song list to ship with the bundle
        run: >
          uv run --no-dev python -m tools.generate_song_list_json
          -t 'song_list.json'
          -u '${{ secrets.USDB_USER }}'
          -p '${{ secrets.USDB_PASSWORD }}'
      - uses: actions/upload-artifact@v4
        with:
          name: artifacts
          path: |
            song_list.json
            CHANGELOG.md

  bundle:
    name: Build bundles
    runs-on: ${{ matrix.os }}
    container: ${{ matrix.image }}
    needs: scrape-song-list
    strategy:
      matrix:
        include:
          - os: ubuntu-latest
            image: ghcr.io/bohning/syncer_linux_builder_image:latest
            TARGET: Linux
          - os: macos-latest
            TARGET: macOS-arm64
          - os: macos-15-intel
            TARGET: macOS-x64
          - os: windows-latest
            TARGET: Windows-portable
          - os: windows-latest
            TARGET: Windows-install
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - uses: actions/download-artifact@v4
      - name: On macOS and windows, install python normally (linux container has python preinstalled)
        if: contains(matrix.os, 'macos') || contains(matrix.os, 'windows')
        uses: actions/setup-python@v5
        with:
          python-version: "3.14"
      - name: Set up uv
        uses: astral-sh/setup-uv@v7
      - name: Set macOS deployment target (macOS 12+ compatibility)
        if: startsWith(matrix.os, 'macos')
        run: echo "MACOSX_DEPLOYMENT_TARGET=12.0" >> $GITHUB_ENV
      - name: Generate GUI elements
        run: uv run --no-dev --group bundle python -m tools.generate_pyside_files
      - name: Install create-dmg on macOS
        if: contains(matrix.TARGET, 'macOS')
        run: brew install create-dmg
      - name: Bundle with pyinstaller for ${{matrix.TARGET}}
        run: >
          uv run --no-dev --group bundle python -m tools.bundle ${{matrix.TARGET}} --with-songlist
      - uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.TARGET }}-artifacts
          path: |
            dist/*Linux
            dist/*.dmg
            dist/*.exe

  release:
    if: ${{ github.event_name == 'push' || github.event.inputs.dry_run == 'false' }}
    name: Create release
    permissions:
      contents: write
    needs: bundle
    runs-on: ubuntu-latest
    steps:
      - uses: actions/download-artifact@v4
        with:
          path: artifacts
          merge-multiple: true
      - name: Extract Release Notes
        id: extract_notes
        run: >
          awk 'BEGIN { RS="<!-- [0-9]+\\.[0-9]+\\.[0-9]+ -->\\s*"; ORS=""; }
          NR==2 { print $0; exit; }'
          artifacts/CHANGELOG.md >CHANGELOG.md
        shell: bash
      - uses: ncipollo/release-action@v1
        with:
          artifacts: "artifacts/USDB_Syncer*"
          bodyFile: "CHANGELOG.md"

  publish:
    name: Build and publish release to PyPI
    if: ${{ github.event.inputs.dry_run != 'true' }}
    needs: bundle
    runs-on: ubuntu-latest
    environment: pypi
    permissions:
      id-token: write # IMPORTANT: this permission is mandatory for trusted publishing
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: "3.14"
      - name: Set up uv
        uses: astral-sh/setup-uv@v7
      - name: Generate GUI elements
        run: uv run --no-dev python -m tools.generate_pyside_files
      - name: Build package
        run: uv build --force-pep517
      - name: Publish package distributions to PyPI
        uses: pypa/gh-action-pypi-publish@release/v1
        with:
          verbose: true
