name: "test"
on: [pull_request]

jobs:
  changes:
    runs-on: ubuntu-latest
    outputs:
      desktop: ${{steps.desktop.outputs.src}}
      preview_folder: ${{steps.preview.outputs.folder}}
    steps:
    - uses: actions/checkout@v3
    - uses: dorny/paths-filter@v2
      id: desktop
      with:
        filters: |
          src:
            - 'modules/desktop/**'
            - 'modules/ui/**'
    - name: get s3 preview folder
      id: preview
      run: echo "folder=${{ github.event.number }}-merge" >> $GITHUB_OUTPUT
  no_preview:
    needs: changes
    if: needs.changes.outputs.desktop == 'false'
    runs-on: ubuntu-latest
    steps:
      - name: comment preview site
        uses: mshick/add-pr-comment@v2
        with:
          message-id: preview-comment-${{needs.changes.outputs.preview_folder}}
          message: |
            no preview or changes related to UI
  test:
    needs: changes
    runs-on: ubuntu-latest
    steps:
      - uses: teaxyz/setup@v0
        with:
          version: 0.26.2
      - uses: actions/checkout@v3
      - name: install app dependencies
        run: tea -E xc setup
      # TODO: fix
      # - name: unit test
      #   run: pnpm --filter desktop run coverage
      - name: lint
        run: tea -E pnpm -r lint
  build_svelte:
    needs: changes
    runs-on: ubuntu-latest
    steps:
      - uses: teaxyz/setup@v0
        with:
          version: 0.26.2
      - uses: actions/checkout@v3
      - name: cache node_modules build
        # TODO: cache issue in our self-hosted macos runner ESPIPE: invalid seek, read
        # but its ok to ignore, its still the fastest builder
        # NOTE: enabling cache in the self hosted runner slows down the pipeline by 4m because post-cache builder error ^
        if: startsWith(inputs.platform, 'linux') || matrix.platform.name == 'darwin+x86-64'
        uses: actions/cache@v3
        with:
          key: ubuntu-latest-pnpm
          path: |
            ./pnpm
            ./.pnpm-store
            ./node_modules
            ./modules/desktop/node_modules
            ./modules/ui/node_modules
      - name: cache electron build
        uses: actions/cache@v3
        with:
          key: ubuntu-latest-electron
          path: |
            ./modules/desktop/.svelte-kit
            ./modules/desktop/build
      - name: get gui version
        id: gui-version
        run: |
          tea +stedolan.github.io/jq
          export version=$(echo $(cat modules/desktop/package.json) | jq --raw-output .version)
          export postfix=
          if GIT_DIR=/path/to/repo/.git git rev-parse $1 >/dev/null 2>&1
          then
              echo "Found tag"
          else
              export postfix=-dev
          fi
          echo "version=$version$postfix" >> $GITHUB_OUTPUT

      - name: build
        run: |
          tea -SE xc setup
          cd modules/desktop && tea -ES pnpm vite build && cp build/app.html build/index.html
        env:
          PUBLIC_VERSION: ${{ steps.gui-version.outputs.version }}
          BUILD_FOR:  preview

      - uses: aws-actions/configure-aws-credentials@v1
        with:
          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          aws-region: us-east-1
      - name: sync web files into preview folder
        env:
          prefix: ${{ needs.changes.outputs.preview_folder }}
        run: |
          aws s3 sync ./modules/desktop/build \
            "s3://preview.gui.tea.xyz/$prefix"

      - name: setup preview
        id: preview_setup
        run: |
          tea +stedolan.github.io/jq .github/create-invalidate-preview.sh ${{ needs.changes.outputs.preview_folder }}

      - name: comment preview site
        uses: mshick/add-pr-comment@v2
        with:
          message-id: preview-comment-${{needs.changes.outputs.preview_folder}}
          message: |
            **preview is at**: 
            <a href="https://${{steps.preview_setup.outputs.domain}}" target="_blank">here</a>
            ```bash
            https://${{steps.preview_setup.outputs.domain}}
            ```
            copy-paste into a browser to view
  build_desktop:
    needs: [changes, test]
    # if: needs.changes.outputs.desktop == 'true'
    strategy:
      matrix:
        platform:
          # X86+64 is built with ARM64 also
          - darwin+aarch64
    uses: ./.github/workflows/build-sign-notarize.yml
    with:
      platform: ${{ matrix.platform }}
      s3-prefix: ${{ needs.changes.outputs.preview_folder || 'dev-pr' }}
      debug: 1
    secrets: inherit
  upload:
    needs: [build_desktop, changes]
    runs-on: ubuntu-latest
    strategy:
      matrix:
        platform:
          # x86 included already in aarch64
          - name: darwin+aarch64
            id: mac_m1
          # - name: linux+x86-64
          #   id: linux
          # - name: linux+aarch64
          #   id: linux_arm64
    steps:
    - uses: aws-actions/configure-aws-credentials@v1
      with:
        aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
        aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
        aws-region: us-east-1
    - name: s3 artifact download
      env:
        S3_INSTALLER_KEY: ${{ needs.build_desktop.outputs.s3-custom-notarized-installers-key }}
      run: aws s3 cp $S3_INSTALLER_KEY dist.tgz

    - run: mkdir dist
    - run: tar xzf dist.tgz -C dist

    - name: get installer filenames of arm64 and x86+64
      id: app_files
      working-directory: ./dist
      run: |
        ARM64_ZIP=$(ls | grep -Ev blockmap | grep arm64-mac.zip)
        X86_ZIP=$(ls | grep -Ev blockmap | grep -Ev arm64 | grep mac.zip)
        ARM64_DMG=$(ls | grep -Ev blockmap | grep arm64.dmg)
        X86_DMG=$(ls | grep -Ev blockmap | grep -Ev arm64 | grep dmg)
        echo zip_arm64=$ARM64_ZIP >> $GITHUB_OUTPUT
        echo zip_x86=$X86_ZIP >> $GITHUB_OUTPUT
        echo dmg_arm64=$ARM64_DMG >> $GITHUB_OUTPUT
        echo dmg_x86=$X86_DMG >> $GITHUB_OUTPUT

    - name: build platform output
      id: build_platform
      env:
        platform: ${{ matrix.platform.name }}
      run: |
        BUILD_PLATFORM=$(echo $platform | sed -e "s/darwin+//g" | sed -e "s/linux+//g")
        EXTENSION=dmg
        case $platform in
          "linux+x86-64")
            BUILD_PLATFORM="amd64"
            EXTENSION="deb"
            ;;
          "linux+aarch64")
            BUILD_PLATFORM="aarch64"
            EXTENSION="deb"
            ;;
          "darwin+aarch64")
            BUILD_PLATFORM="aarch64"
            EXTENSION="dmg"
            ;;
          "darwin+x86-64")
            BUILD_PLATFORM="x64"
            EXTENSION="dmg"
            ;;
          *)
            echo "Unknown platform $platform"
            exit 1
            ;;
        esac
        echo "build_platform=$BUILD_PLATFORM" >> $GITHUB_OUTPUT
        echo "extension=$EXTENSION" >> $GITHUB_OUTPUT

    - uses: aws-actions/configure-aws-credentials@v1
      with:
        aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
        aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
        aws-region: us-east-1

    - name: cp package images from prod to preview bucket
      env:
        prefix: ${{ needs.changes.outputs.preview_folder }}
      run: |
        cd dist && \
        aws s3 sync . \
          "s3://preview.gui.tea.xyz/$prefix/"

    - name: comment install for Linux
      if: startsWith(matrix.platform.name, 'linux')
      uses: mshick/add-pr-comment@v2
      with:
        message-id: ${{ matrix.platform.id }}-comment
        message: |
          **installer for Linux ${{ matrix.platform.name }} is at**:
          ```bash
          http://preview.gui.tea.xyz.s3-website-us-east-1.amazonaws.com/${{ needs.changes.outputs.preview_folder }}/${{ steps.build_platform.outputs.filename }}
          ```
          copy-paste into a browser to download

    - name: comment install for MacOS
      if: startsWith(matrix.platform.name, 'darwin')
      uses: mshick/add-pr-comment@v2
      with:
        message-id: darwin+aarch64-comment
        message: |
          **installers for MacOS darwin+aarch64 is at**:
          ```bash
          http://preview.gui.tea.xyz.s3-website-us-east-1.amazonaws.com/${{ needs.changes.outputs.preview_folder }}/${{ steps.app_files.outputs.dmg_arm64 }}
          ```
          copy-paste into a browser to download
    - name: comment install for MacOS
      if: startsWith(matrix.platform.name, 'darwin')
      uses: mshick/add-pr-comment@v2
      with:
        message-id: darwin+x86-64-comment
        message: |
          **installers for MacOS darwin+x86-64 is at**:
          ```bash
          http://preview.gui.tea.xyz.s3-website-us-east-1.amazonaws.com/${{ needs.changes.outputs.preview_folder }}/${{ steps.app_files.outputs.dmg_x86 }}
          ```
          copy-paste into a browser to download
