name: Release frontend

on:
    push:
        tags:
            - '*'

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 started'

    build-and-release:
        runs-on: ubuntu-22.04
        steps:
            - name: Checkout
              uses: actions/checkout@v3
              with:
                  fetch-depth: 0

            - name: NewTag
              id: tag
              uses: JinoArch/get-latest-tag@latest

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

            - name: Generate changelog
              id: changelog
              run: |
                  CHANGELOG=$(npx changelogen --from=${{ steps.tag.outputs.previousTag }} --to=${{ steps.tag.outputs.latestTag }} | sed '/^\[log\]/d')
                  echo "CHANGELOG<<EOF" >> $GITHUB_ENV
                  echo "$CHANGELOG" >> $GITHUB_ENV
                  echo "EOF" >> $GITHUB_ENV

            - name: Install dependencies and build
              run: |
                  npm ci
                  npm run start:build

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

            - name: Upload Release Asset
              uses: softprops/action-gh-release@da05d552573ad5aba039eaac05058a918a7bf631
              env:
                  GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
              with:
                  files: remnawave-frontend.zip
                  tag_name: ${{ github.ref_name }}
                  generate_release_notes: true
                  append_body: true
                  make_latest: true
                  draft: false
                  prerelease: false
                  name: ${{ github.ref_name }}
                  body: |
                      # 🌊 Remnawave Frontend v${{ github.ref_name }}

                      <p align="center">
                      <a href="https://t.me/remnawave" target="_blank" rel="noopener noreferrer">
                        <img src="https://img.shields.io/badge/Join%20community-Telegram-26A5E4?style=for-the-badge&logo=telegram&logoColor=white" alt="Join community on Telegram" width="220" height="auto">
                      </a>
                      </p>

                      ${{ env.CHANGELOG }}

                      ### 📦 Artifacts
                      - remnawave-frontend.zip - archive with built frontend

                      ### 🔍 Details
                      - Commit: ${{ github.sha }}
                      - Branch: ${{ github.ref_name }}
                      - Build time: ${{ github.event.head_commit.timestamp }}

    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 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 failed'
