name: Build and Release

on:
  push:
    tags:
      - 'v*'

jobs:
  build-and-release:
    runs-on: ubuntu-latest

    permissions:
      contents: write

    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
          fetch-tags: true

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '18'
          cache: 'npm'

      - name: Install dependencies
        run: npm ci

      - name: Build all-in-one HTML
        run: npm run build
        env:
          VERSION: ${{ github.ref_name }}

      - name: Prepare release assets
        run: |
          cd dist
          mv index.html management.html
          ls -lh management.html

      - name: Generate release notes
        run: |
          set -euo pipefail
          current_tag="${GITHUB_REF_NAME}"
          previous_tag="$(git tag --list 'v*' --sort=-v:refname | grep -v "^${current_tag}$" | head -n 1 || true)"
          if [ -n "${previous_tag}" ]; then
            range="${previous_tag}..${current_tag}"
          else
            range="${current_tag}"
          fi

          : > release-notes.md
          git log --pretty=format:"- %h %s" "${range}" >> release-notes.md

      - name: Create Release
        uses: softprops/action-gh-release@v1
        with:
          files: dist/management.html
          body_path: release-notes.md
          draft: false
          prerelease: false
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
