name: Release frontend dev

on:
    push:
        branches:
            - dev

jobs:
    send-start-deploy-telegram-message:
        name: Send Telegram message
        runs-on: ubuntu-latest
        steps:
            - name: Checkout source code
              uses: actions/checkout@v2

            - name: Send Telegram message
              uses: proDreams/actions-telegram-notifier@main
              with:
                  token: ${{ secrets.TELEGRAM_TOKEN }}
                  chat_id: ${{ secrets.TELEGRAM_CHAT_ID }}
                  thread_id: ${{ secrets.TELEGRAM_TOPIC_ID }}
                  status: info
                  notify_fields: 'repository,branch,commit,workflow'
                  title: 'Build frontend:dev build started'

    build-and-release:
        name: Build and release
        runs-on: ubuntu-22.04
        steps:
            - name: Checkout
              uses: actions/checkout@v3

            - name: Setup Node
              uses: actions/setup-node@v3
              with:
                  node-version: '22.18.0'

            - name: Install dependencies
              run: |
                  npm ci

            - name: Upload sources
              env:
                  CROWDIN_PROJECT_ID: ${{ secrets.CROWDIN_PROJECT_ID }}
                  CROWDIN_API_TOKEN: ${{ secrets.CROWDIN_API_TOKEN }}
              run: |
                  npm run crowdin:upload

            - name: Build
              run: |
                  npm run start:build

            - name: Create ZIP archive of /dist
              run: |
                  zip -r remnawave-frontend.zip ./dist

            - name: Remove previous ZIP from release (if exists)
              env:
                  GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
              run: |
                  gh release delete-asset dev-build remnawave-frontend.zip || true

            - name: Upload new ZIP to release
              env:
                  GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
              run: |
                  gh release upload dev-build remnawave-frontend.zip --clobber

            - name: Update release notes
              env:
                  GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
              run: |
                  COMMIT_URL="https://github.com/${{ github.repository }}/commit/${{ github.sha }}"
                  DATE=$(date -u +'%Y-%m-%d %H:%M:%S UTC')

                  gh release edit dev-build --notes-file - <<EOF
                  🔄 Updated development build
                  🔗 [Commit]($COMMIT_URL)  
                  📅 Built: $DATE
                  EOF

    send-telegram-message:
        name: Send Telegram message
        needs: [build-and-release]
        runs-on: ubuntu-latest
        steps:
            - name: Checkout source code
              uses: actions/checkout@v2

            - name: Send Telegram message
              uses: proDreams/actions-telegram-notifier@main
              with:
                  token: ${{ secrets.TELEGRAM_TOKEN }}
                  chat_id: ${{ secrets.TELEGRAM_CHAT_ID }}
                  thread_id: ${{ secrets.TELEGRAM_TOPIC_ID }}
                  status: success
                  notify_fields: 'repository,branch,commit,workflow'
                  title: 'Build frontend:dev build finished'

    notify-on-error:
        runs-on: ubuntu-latest
        needs: [build-and-release]
        if: failure()
        steps:
            - name: Checkout source code
              uses: actions/checkout@v2

            - name: Send Telegram message
              uses: proDreams/actions-telegram-notifier@main
              with:
                  token: ${{ secrets.TELEGRAM_TOKEN }}
                  chat_id: ${{ secrets.TELEGRAM_CHAT_ID }}
                  thread_id: ${{ secrets.TELEGRAM_TOPIC_ID }}
                  status: error
                  notify_fields: 'repository,branch,commit,workflow'
                  title: 'Build frontend:dev build failed'
