name: noti-discord-and-update-url-readme
on:
  workflow_call:
    inputs:
      ref:
        required: true
        type: string
        default: 'refs/heads/main'
      build_reason:
        required: true
        type: string
        default: 'Nightly'
      push_to_branch:
        required: true
        type: string
        default: 'main'
      new_version:
        required: true
        type: string
        default: ''
    # secrets:
    #   PAT_SERVICE_ACCOUNT:
    #     required: false
    #   DISCORD_WEBHOOK:
    #     required: false

jobs:
  noti-discord-and-update-url-readme:
    environment: production
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - name: Checkout code
        uses: actions/checkout@v3
        with:
          fetch-depth: "0"
          token: ${{ secrets.PAT_SERVICE_ACCOUNT }}
          ref: ${{ inputs.ref }}

      - name: Set version to environment variable
        run: |
          echo "VERSION=${{ inputs.new_version }}" >> $GITHUB_ENV

      - name: Notify Discord
        uses: Ilshidur/action-discord@master
        with:
          args: |
            Jan App ${{ inputs.build_reason }} build artifact version {{ VERSION }}:
            - Windows: https://delta.jan.ai/latest/jan-win-x64-{{ VERSION }}.exe
            - macOS Intel: https://delta.jan.ai/latest/jan-mac-x64-{{ VERSION }}.dmg
            - macOS Apple Silicon: https://delta.jan.ai/latest/jan-mac-arm64-{{ VERSION }}.dmg
            - Linux Deb: https://delta.jan.ai/latest/jan-linux-amd64-{{ VERSION }}.deb
            - Linux AppImage: https://delta.jan.ai/latest/jan-linux-x86_64-{{ VERSION }}.AppImage
            - Github action run: https://github.com/janhq/jan/actions/runs/{{ GITHUB_RUN_ID }}
        env:
          DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK }}

      - name: Update README.md with artifact URL
        run: |
          sed -i "s|<a href='https://delta.jan.ai/latest/jan-win-x64-.*'>|<a href='https://delta.jan.ai/latest/jan-win-x64-${{ inputs.new_version }}.exe'>|" README.md
          sed -i "s|<a href='https://delta.jan.ai/latest/jan-mac-x64-.*'>|<a href='https://delta.jan.ai/latest/jan-mac-x64-${{ inputs.new_version }}.dmg'>|" README.md
          sed -i "s|<a href='https://delta.jan.ai/latest/jan-mac-arm64-.*'>|<a href='https://delta.jan.ai/latest/jan-mac-arm64-${{ inputs.new_version }}.dmg'>|" README.md
          sed -i "s|<a href='https://delta.jan.ai/latest/jan-linux-amd64-.*'>|<a href='https://delta.jan.ai/latest/jan-linux-amd64-${{ inputs.new_version }}.deb'>|" README.md
          sed -i "s|<a href='https://delta.jan.ai/latest/jan-linux-x86_64-.*'>|<a href='https://delta.jan.ai/latest/jan-linux-x86_64-${{ inputs.new_version }}.AppImage'>|" README.md
          cat README.md
          git config --global user.email "service@jan.ai"
          git config --global user.name "Service Account"
          git add README.md
          git commit -m "${GITHUB_REPOSITORY}: Update README.md with nightly build artifact URL"
          git -c http.extraheader="AUTHORIZATION: bearer ${{ secrets.PAT_SERVICE_ACCOUNT }}" push origin HEAD:${{ inputs.push_to_branch }}
        env:
          GITHUB_RUN_ID: ${{ github.run_id }}
