name: Deploy (pre-dev)

on:
  push:
    branches:
      - 'pre-dev'

jobs:
  build:
    name: "Сборка и публикация версии 🔨"
    runs-on: ubuntu-latest
    permissions:
      contents: write

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Setup Node.js
        uses: actions/setup-node@v3
        with:
          node-version: '21'

      - name: Install pnpm
        run: npm -g i pnpm

      - name: Install dependencies
        run: pnpm i

      - name: Install semantic-release packages
        run: pnpm i @semantic-release/changelog @semantic-release/git @semantic-release/github

      - name: Get next release version (dry-run)
        id: get_version
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          # Run dry-run and save output to file
          npx semantic-release --dry-run --no-ci | tee release.log

          # Extract version (now supports pre-releases like -dev.23, -beta.5, etc.)
          NEXT_VERSION=v$(grep 'The next release version is' release.log \
                        | sed -E 's/.* ([0-9]+\.[0-9]+\.[0-9]+[-.0-9a-zA-Z]*).*$/\1/')
          
          echo "APP_VERSION=$NEXT_VERSION" >> $GITHUB_ENV
          echo "Got version: $NEXT_VERSION"

      - name: Create .env file
        run: |
          echo "Writing..."
          echo "API_URL=https://hapticx.ru/api" >> .env
          echo "APP_VERSION=${APP_VERSION}" >> .env

      - name: Build android
        run: pnpm build:mobile

      - name: Upload APK artifact
        uses: actions/upload-artifact@v4
        with:
          name: "ktc-${{ env.APP_VERSION }}.apk"
          path: "${{ env.APP_VERSION }}.apk"
          if-no-files-found: 'error'
          retention-days: 1
          compression-level: 0

      - name: Publishing release
        run: npx semantic-release
        env:
          GH_TOKEN: ${{ secrets.GH_TOKEN }}
  
        
  telegram_notification:
    name: "Уведомление в Telegram 🔔"
    runs-on: ubuntu-latest
    needs: build

    steps:
      - uses: actions/checkout@v4
      - name: Escape bad symbols in variables
        id: escape_vars
        run: |
          escaped_message=$(echo "${{ github.event.head_commit.message }}" | perl -pe 's/([&~\`\*\_\[\]()#\+\-\=\|\{\}\.\!])/\\\\$1/g')
          escaped_actor=$(echo "${{ github.actor }}" | perl -pe 's/([&~\`\*\_\[\]()#\+\-\=\|\{\}\.\!])/\\\\$1/g')
          escaped_branch=$(echo "${{ github.ref_name }}" | perl -pe 's/([&~\`\*\_\[\]()#\+\-\=\|\{\}\.\!])/\\\\$1/g')
          echo "escaped_message=$escaped_message" >> $GITHUB_ENV
          echo "escaped_actor=$escaped_actor" >> $GITHUB_ENV
          echo "escaped_branch=$escaped_branch" >> $GITHUB_ENV

      - name: Send Telegram Message
        run: |
          curl \
            --data-urlencode 'chat_id=${{ secrets.TELEGRAM_CHAT_ID }}' \
            --data-urlencode 'message_thread_id=${{ secrets.TELEGRAM_MESSAGE_THREAD_ID }}' \
            --data-urlencode 'link_preview_options={"url":"${{ github.event.head_commit.url }}"}' \
            --data-urlencode 'parse_mode=markdownv2' \
            --data-urlencode $'text=🔨 *Frontend deploy* 🔨\n\n>${{ env.escaped_message }}\n_by [${{ env.escaped_actor }}](https://github.com/${{ env.escaped_actor }})_ at `${{ env.escaped_branch }}`\n\n[commit link](${{ github.event.head_commit.url }})' \
            curl "https://api.telegram.org/bot${{ secrets.TELEGRAM_TOKEN }}/sendMessage"
