# Builds and publishes the temetro Docker images and cuts a GitHub Release.
#
# Trigger: push a semver tag, e.g.
#   git tag v0.1.0 && git push origin v0.1.0
#
# The frontend image bakes NO API URL — it resolves the backend from the host
# the browser uses at runtime — so one published image works for every clinic.
#
# Required repository secrets (Settings → Secrets and variables → Actions):
#   DOCKERHUB_USERNAME  — the Docker Hub account `khalidxv` (or one with push
#                         access to that namespace)
#   DOCKERHUB_TOKEN     — a Docker Hub access token for that account
# Without them the build/login step fails; the workflow is otherwise inert.
name: release

on:
  push:
    tags:
      - "v*.*.*"

permissions:
  contents: write # create the GitHub Release (the update check reads this)

env:
  REGISTRY_NAMESPACE: khalidxv

jobs:
  publish:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Derive version from tag
        id: meta
        run: echo "version=${GITHUB_REF_NAME#v}" >> "$GITHUB_OUTPUT"

      # QEMU lets the amd64 runner emulate arm64 so the images below build for
      # both platforms (Intel + Apple Silicon self-hosters).
      - name: Set up QEMU
        uses: docker/setup-qemu-action@v3

      - name: Set up Buildx
        uses: docker/setup-buildx-action@v3

      - name: Log in to Docker Hub
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: Build & push backend
        uses: docker/build-push-action@v6
        with:
          context: ./backend
          push: true
          platforms: linux/amd64,linux/arm64
          tags: |
            ${{ env.REGISTRY_NAMESPACE }}/temetro-backend:${{ steps.meta.outputs.version }}
            ${{ env.REGISTRY_NAMESPACE }}/temetro-backend:latest

      - name: Build & push frontend
        uses: docker/build-push-action@v6
        with:
          context: ./frontend
          push: true
          platforms: linux/amd64,linux/arm64
          tags: |
            ${{ env.REGISTRY_NAMESPACE }}/temetro-frontend:${{ steps.meta.outputs.version }}
            ${{ env.REGISTRY_NAMESPACE }}/temetro-frontend:latest

      # Pull this version's section out of CHANGELOG.md so the release has real,
      # human-written notes (the auto "Full Changelog" link is still appended
      # below via generate_release_notes). Falls back to a generic line if the
      # version has no CHANGELOG entry.
      - name: Extract changelog notes
        id: notes
        run: |
          version="${{ steps.meta.outputs.version }}"
          awk -v v="$version" '
            $0 ~ "^## \\[" v "\\]" {flag=1; next}
            /^## \[/ {flag=0}
            flag {print}
          ' CHANGELOG.md | sed '/./,$!d' > release-notes.md
          if [ ! -s release-notes.md ]; then
            echo "Release $version. See the changelog for details." > release-notes.md
          fi

      - name: Create GitHub Release
        uses: softprops/action-gh-release@v2
        with:
          body_path: release-notes.md
          generate_release_notes: true
