name: Stable Release

on:
  push:
    tags:
      - 'v[0-9]+.[0-9]+.[0-9]+'

jobs:
  release:
    runs-on: server-runner
    steps:
      - name: Checkout
        run: |
          TAG="${GITHUB_REF#refs/tags/}"
          if [ -d .git ]; then
            git fetch --tags origin
            git checkout "$TAG"
            git clean -fd
          else
            git clone --depth=1 --branch "$TAG" https://gitea.it-drui.de/viewit/KX-Bridge-Release.git .
          fi

      - name: Install Docker CLI
        run: |
          if ! command -v docker >/dev/null 2>&1; then
            ARCH=$(uname -m)
            if [ "$ARCH" = "x86_64" ]; then
              DARCH="x86_64"
              BARCH="amd64"
            else
              DARCH="aarch64"
              BARCH="arm64"
            fi
            wget -qO- "https://download.docker.com/linux/static/stable/${DARCH}/docker-27.5.1.tgz" \
              | tar xz --strip-components=1 -C /usr/local/bin docker/docker
            chmod +x /usr/local/bin/docker
            mkdir -p /usr/local/lib/docker/cli-plugins
            wget -qO /usr/local/lib/docker/cli-plugins/docker-buildx \
              "https://github.com/docker/buildx/releases/download/v0.23.0/buildx-v0.23.0.linux-${BARCH}"
            chmod +x /usr/local/lib/docker/cli-plugins/docker-buildx
          fi
          docker version --format '{{.Client.Version}}'

      - name: Set up QEMU
        run: |
          docker run --rm --privileged tonistiigi/binfmt:latest --install all

      - name: Set up buildx
        run: |
          docker buildx inspect kxbuilder 2>/dev/null || \
            docker buildx create --name kxbuilder --use
          docker buildx use kxbuilder

      - name: Login to Gitea registry
        run: |
          echo "${{ secrets.REGISTRY_TOKEN }}" | \
            docker login gitea.it-drui.de -u "${{ secrets.REGISTRY_USER }}" --password-stdin

      - name: Build & push (amd64 + arm64)
        run: |
          VERSION="${GITHUB_REF#refs/tags/}"
          docker buildx build \
            --platform linux/amd64,linux/arm64 \
            --push \
            --provenance=false \
            --no-cache \
            -t "gitea.it-drui.de/viewit/kx-bridge:latest" \
            -t "gitea.it-drui.de/viewit/kx-bridge:${VERSION}" \
            .

      - name: Create Gitea Release
        env:
          GITEA_TOKEN: ${{ secrets.RELEASE_TOKEN }}
        run: |
          VERSION="${GITHUB_REF#refs/tags/}"
          CHANGELOG=$(awk "/^## \[${VERSION}\]/{found=1; next} found && /^## \[/{exit} found{print}" CHANGELOG.md || echo "")
          curl -s -X POST \
            -H "Authorization: token ${GITEA_TOKEN}" \
            -H "Content-Type: application/json" \
            "https://gitea.it-drui.de/api/v1/repos/viewit/KX-Bridge-Release/releases" \
            -d "{
              \"tag_name\": \"${VERSION}\",
              \"name\": \"KX-Bridge ${VERSION}\",
              \"body\": $(echo "$CHANGELOG" | python3 -c 'import sys,json; print(json.dumps(sys.stdin.read()))'),
              \"draft\": false,
              \"prerelease\": false
            }"
