name: Release

on:
  push:
    tags:
      - "v*"

permissions:
  contents: write

jobs:
  verify:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 24
          cache: npm
      - run: npm ci
      - run: npm ci --prefix web
      - run: node desktop/verify-release-tag.mjs "$GITHUB_REF_NAME"
      - run: npm run typecheck
      - run: npx tsc -p tsconfig.desktop.json --noEmit
      - run: npm run lint
      - run: npm test
      - run: npm run test:web
      - run: npm run check:engine-browser

  package:
    needs: verify
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: windows-latest
            command: package:desktop:win
            artifact: desktop-windows
          - os: ubuntu-latest
            command: package:desktop:linux
            artifact: desktop-linux
          - os: macos-latest
            command: package:desktop:mac
            artifact: desktop-macos
    runs-on: ${{ matrix.os }}
    env:
      GH_TOKEN: ${{ github.token }}
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 24
          cache: npm
      - run: npm ci
      - run: npm ci --prefix web
      - run: npm run ${{ matrix.command }} -- --publish never
      - uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.artifact }}
          path: |
            release/*.exe
            release/*.exe.blockmap
            release/*.AppImage
            release/*.dmg
            release/*.zip
            release/latest*.yml
          if-no-files-found: error

  package-android:
    needs: verify
    runs-on: ubuntu-latest
    env:
      ANDROID_KEYSTORE_FILE: ${{ github.workspace }}/android-release.jks
      ANDROID_KEYSTORE_PASSWORD: ${{ secrets.ANDROID_KEYSTORE_PASSWORD }}
      ANDROID_KEY_ALIAS: ${{ secrets.ANDROID_KEY_ALIAS }}
      ANDROID_KEY_PASSWORD: ${{ secrets.ANDROID_KEY_PASSWORD }}
      REQUIRE_ANDROID_SIGNING: "1"
      ANDROID_VERSION_CODE: ${{ github.run_number }}
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 24
          cache: npm
      - uses: actions/setup-java@v4
        with:
          distribution: temurin
          java-version: 21
          cache: gradle
      - run: npm ci
      - run: npm ci --prefix web
      - name: Restore Android release keystore
        env:
          ANDROID_KEYSTORE_BASE64: ${{ secrets.ANDROID_KEYSTORE_BASE64 }}
        run: |
          test -n "$ANDROID_KEYSTORE_BASE64"
          printf '%s' "$ANDROID_KEYSTORE_BASE64" | base64 --decode > "$ANDROID_KEYSTORE_FILE"
      - run: npm run package:android:release
      - uses: actions/upload-artifact@v4
        with:
          name: android
          path: .phase-g/release/*.apk
          if-no-files-found: error

  publish:
    needs: [package, package-android]
    runs-on: ubuntu-latest
    steps:
      - uses: actions/download-artifact@v4
        with:
          pattern: desktop-*
          path: release
          merge-multiple: true
      - uses: actions/download-artifact@v4
        with:
          name: android
          path: release
      - name: Create GitHub release
        env:
          GH_TOKEN: ${{ github.token }}
        run: gh release create "$GITHUB_REF_NAME" release/* --verify-tag --generate-notes --title "$GITHUB_REF_NAME"
