pipeline {
  agent {
    kubernetes {
      label 'bstein-dev-home'
      defaultContainer 'builder'
      yaml """
apiVersion: v1
kind: Pod
metadata:
  labels:
    app: bstein-dev-home
spec:
  nodeSelector:
    kubernetes.io/arch: arm64
    node-role.kubernetes.io/worker: "true"
  containers:
    - name: dind
      image: docker:27-dind
      securityContext:
        privileged: true
      env:
        - name: DOCKER_TLS_CERTDIR
          value: ""
      args:
        - --mtu=1400
        - --host=unix:///var/run/docker.sock
        - --host=tcp://0.0.0.0:2375
      volumeMounts:
        - name: dind-storage
          mountPath: /var/lib/docker
    - name: builder
      image: docker:27
      command: ["cat"]
      tty: true
      env:
        - name: DOCKER_HOST
          value: tcp://localhost:2375
        - name: DOCKER_TLS_CERTDIR
          value: ""
      volumeMounts:
        - name: workspace-volume
          mountPath: /home/jenkins/agent
        - name: docker-config-writable
          mountPath: /root/.docker
        - name: harbor-config
          mountPath: /docker-config
    - name: tester
      image: python:3.12-slim
      command: ["cat"]
      tty: true
      volumeMounts:
        - name: workspace-volume
          mountPath: /home/jenkins/agent
  volumes:
    - name: workspace-volume
      emptyDir: {}
    - name: docker-config-writable
      emptyDir: {}
    - name: dind-storage
      emptyDir: {}
    - name: harbor-config
      secret:
        secretName: harbor-bstein-robot
        items:
          - key: .dockerconfigjson
            path: config.json
"""
    }
  }
  environment {
    REGISTRY = 'registry.bstein.dev/bstein'
    FRONT_IMAGE = "${REGISTRY}/bstein-dev-home-frontend"
    BACK_IMAGE  = "${REGISTRY}/bstein-dev-home-backend"
    VERSION_TAG = 'dev'
    SEMVER = 'dev'
    SUITE_NAME = 'bstein-home'
    PUSHGATEWAY_URL = 'http://platform-quality-gateway.monitoring.svc.cluster.local:9091'
  }
  options {
    disableConcurrentBuilds()
  }
  triggers {
    // Poll every 2 minutes; notifyCommit can also trigger, but polling keeps it moving without webhook tokens.
    pollSCM('H/2 * * * *')
  }
  stages {
    stage('Checkout') {
      steps {
        checkout scm
      }
    }

    stage('Prep toolchain') {
      steps {
        container('builder') {
          withCredentials([usernamePassword(credentialsId: 'harbor-robot', usernameVariable: 'HARBOR_USERNAME', passwordVariable: 'HARBOR_PASSWORD')]) {
            sh '''
              set -euo pipefail
              for attempt in 1 2 3 4 5; do
                if apk add --no-cache bash git jq curl; then
                  break
                fi
                if [ "$attempt" -eq 5 ]; then
                  echo "apk add failed after ${attempt} attempts" >&2
                  exit 1
                fi
                sleep $((attempt * 2))
              done
              mkdir -p /root/.docker
              printf '%s' "${HARBOR_PASSWORD}" | docker login registry.bstein.dev -u "${HARBOR_USERNAME}" --password-stdin
            '''
          }
        }
      }
    }

    stage('Compute version') {
      steps {
        container('builder') {
          script {
            sh '''
              set -euo pipefail
              if git describe --tags --exact-match >/dev/null 2>&1; then
                SEMVER="$(git describe --tags --exact-match)"
              else
                BASE="$(jq -r '.version' frontend/package.json || echo '0.1.0')"
                SEMVER="${BASE}-${BUILD_NUMBER}"
              fi
              # Accept bare semver or leading v (with optional prerelease).
              if ! echo "$SEMVER" | grep -Eq '^v?[0-9]+\\.[0-9]+\\.[0-9]+([-.][0-9A-Za-z]+)?$'; then
                SEMVER="0.1.0-${BUILD_NUMBER}"
              fi
              echo "SEMVER=${SEMVER}" > build.env
            '''
            def props = readProperties file: 'build.env'
            env.SEMVER = props['SEMVER'] ?: "0.1.0-${env.BUILD_NUMBER}"
            env.VERSION_TAG = env.SEMVER
          }
        }
      }
    }

    stage('Buildx setup') {
      steps {
        container('builder') {
          sh '''
            set -euo pipefail
            ready=0
            for i in $(seq 1 10); do
              if docker info >/dev/null 2>&1; then
                ready=1
                break
              fi
              sleep 2
            done
            if [ "${ready}" -ne 1 ]; then
              echo "docker daemon did not become ready on ${DOCKER_HOST}" >&2
              docker version || true
              exit 1
            fi
            BUILDER_NAME="bstein-builder-${BUILD_NUMBER}"
            docker buildx rm "${BUILDER_NAME}" >/dev/null 2>&1 || true
            docker buildx create --name "${BUILDER_NAME}" --driver docker-container \
              --driver-opt image=moby/buildkit:buildx-stable-1 \
              --bootstrap --use
            docker buildx inspect "${BUILDER_NAME}" --bootstrap
          '''
        }
      }
    }

    stage('Backend unit tests') {
      steps {
        container('tester') {
          sh '''
            set -euo pipefail
            mkdir -p build
            export PYTHONPATH="${WORKSPACE}/backend:${PYTHONPATH:-}"
            python -m pip install --no-cache-dir -r backend/requirements.txt pytest pytest-mock
            python -m pytest backend/tests -q --junitxml=build/junit-backend.xml
          '''
        }
      }
    }

    stage('Build & push frontend') {
      steps {
        container('builder') {
          sh '''
            set -euo pipefail
            VERSION_TAG="$(cut -d= -f2 build.env)"
            docker buildx build \
              --platform linux/arm64 \
              --tag "${FRONT_IMAGE}:${VERSION_TAG}" \
              --tag "${FRONT_IMAGE}:latest" \
              --file Dockerfile.frontend \
              --push \
              .
          '''
        }
      }
    }

    stage('Build & push backend') {
      steps {
        container('builder') {
          sh '''
            set -euo pipefail
            VERSION_TAG="$(cut -d= -f2 build.env)"
            docker buildx build \
              --platform linux/arm64 \
              --tag "${BACK_IMAGE}:${VERSION_TAG}" \
              --tag "${BACK_IMAGE}:latest" \
              --file Dockerfile.backend \
              --push \
              .
          '''
        }
      }
    }
  }

  post {
    success {
      container('tester') {
        sh '''
          set -euo pipefail
          export QUALITY_STATUS=ok
          python - <<'PY'
import os
import re
import urllib.request
import xml.etree.ElementTree as ET
from pathlib import Path

suite = os.environ.get("SUITE_NAME", "bstein-home")
status = os.environ.get("QUALITY_STATUS", "failed")
gateway = os.environ.get("PUSHGATEWAY_URL", "http://platform-quality-gateway.monitoring.svc.cluster.local:9091").rstrip("/")
text = urllib.request.urlopen(f"{gateway}/metrics", timeout=10).read().decode("utf-8", errors="replace")

def counter(name: str) -> float:
    pattern = re.compile(
        rf'^platform_quality_gate_runs_total\\{{[^}}]*job="platform-quality-ci"[^}}]*suite="{re.escape(suite)}"[^}}]*status="{name}"[^}}]*\\}}\\s+([0-9]+(?:\\.[0-9]+)?)$',
        re.M,
    )
    match = pattern.search(text)
    return float(match.group(1)) if match else 0.0

ok = counter("ok")
failed = counter("failed")
if status == "ok":
    ok += 1
else:
    failed += 1

totals = {"tests": 0, "failures": 0, "errors": 0, "skipped": 0}
junit_path = Path("build/junit-backend.xml")
if junit_path.exists():
    root = ET.parse(junit_path).getroot()
    suites = [root] if root.tag == "testsuite" else list(root.findall("testsuite")) if root.tag == "testsuites" else []
    for node in suites:
        for key in totals:
            raw = node.attrib.get(key) or "0"
            try:
                totals[key] += int(float(raw))
            except ValueError:
                pass
passed = max(totals["tests"] - totals["failures"] - totals["errors"] - totals["skipped"], 0)

payload = (
    "# TYPE platform_quality_gate_runs_total counter\\n"
    f'platform_quality_gate_runs_total{{suite="{suite}",status="ok"}} {int(ok)}\\n'
    f'platform_quality_gate_runs_total{{suite="{suite}",status="failed"}} {int(failed)}\\n'
    "# TYPE bstein_home_quality_gate_tests_total gauge\\n"
    f'bstein_home_quality_gate_tests_total{{suite="{suite}",result="passed"}} {passed}\\n'
    f'bstein_home_quality_gate_tests_total{{suite="{suite}",result="failed"}} {totals["failures"]}\\n'
    f'bstein_home_quality_gate_tests_total{{suite="{suite}",result="error"}} {totals["errors"]}\\n'
    f'bstein_home_quality_gate_tests_total{{suite="{suite}",result="skipped"}} {totals["skipped"]}\\n'
)
req = urllib.request.Request(
    f"{gateway}/metrics/job/platform-quality-ci/suite/{suite}",
    data=payload.encode("utf-8"),
    method="POST",
    headers={"Content-Type": "text/plain"},
)
urllib.request.urlopen(req, timeout=10).read()
PY
        '''
      }
    }
    failure {
      container('tester') {
        sh '''
          set -euo pipefail
          export QUALITY_STATUS=failed
          python - <<'PY'
import os
import re
import urllib.request
import xml.etree.ElementTree as ET
from pathlib import Path

suite = os.environ.get("SUITE_NAME", "bstein-home")
status = os.environ.get("QUALITY_STATUS", "failed")
gateway = os.environ.get("PUSHGATEWAY_URL", "http://platform-quality-gateway.monitoring.svc.cluster.local:9091").rstrip("/")
text = urllib.request.urlopen(f"{gateway}/metrics", timeout=10).read().decode("utf-8", errors="replace")

def counter(name: str) -> float:
    pattern = re.compile(
        rf'^platform_quality_gate_runs_total\\{{[^}}]*job="platform-quality-ci"[^}}]*suite="{re.escape(suite)}"[^}}]*status="{name}"[^}}]*\\}}\\s+([0-9]+(?:\\.[0-9]+)?)$',
        re.M,
    )
    match = pattern.search(text)
    return float(match.group(1)) if match else 0.0

ok = counter("ok")
failed = counter("failed")
if status == "ok":
    ok += 1
else:
    failed += 1

totals = {"tests": 0, "failures": 0, "errors": 0, "skipped": 0}
junit_path = Path("build/junit-backend.xml")
if junit_path.exists():
    root = ET.parse(junit_path).getroot()
    suites = [root] if root.tag == "testsuite" else list(root.findall("testsuite")) if root.tag == "testsuites" else []
    for node in suites:
        for key in totals:
            raw = node.attrib.get(key) or "0"
            try:
                totals[key] += int(float(raw))
            except ValueError:
                pass
passed = max(totals["tests"] - totals["failures"] - totals["errors"] - totals["skipped"], 0)

payload = (
    "# TYPE platform_quality_gate_runs_total counter\\n"
    f'platform_quality_gate_runs_total{{suite="{suite}",status="ok"}} {int(ok)}\\n'
    f'platform_quality_gate_runs_total{{suite="{suite}",status="failed"}} {int(failed)}\\n'
    "# TYPE bstein_home_quality_gate_tests_total gauge\\n"
    f'bstein_home_quality_gate_tests_total{{suite="{suite}",result="passed"}} {passed}\\n'
    f'bstein_home_quality_gate_tests_total{{suite="{suite}",result="failed"}} {totals["failures"]}\\n'
    f'bstein_home_quality_gate_tests_total{{suite="{suite}",result="error"}} {totals["errors"]}\\n'
    f'bstein_home_quality_gate_tests_total{{suite="{suite}",result="skipped"}} {totals["skipped"]}\\n'
)
req = urllib.request.Request(
    f"{gateway}/metrics/job/platform-quality-ci/suite/{suite}",
    data=payload.encode("utf-8"),
    method="POST",
    headers={"Content-Type": "text/plain"},
)
urllib.request.urlopen(req, timeout=10).read()
PY
        '''
      }
    }
    always {
      script {
        def props = fileExists('build.env') ? readProperties(file: 'build.env') : [:]
        echo "Build complete for ${props['SEMVER'] ?: env.VERSION_TAG}"
      }
      archiveArtifacts artifacts: 'build/junit-backend.xml', allowEmptyArchive: true
    }
  }
}
