name: Build Resilient App

on:
  push:
    branches: [main, master, develop]
  pull_request:
  workflow_dispatch:

jobs:
  build:
    runs-on: self-hosted

    steps:
      - name: Checkout code
        run: |
          git clone "${CI_REPO_URL}" .
          git checkout "${CI_COMMIT_REF}"

      - name: Install system dependencies
        run: |
          apk add --no-cache nodejs npm openjdk17 maven git bash

      - name: Set up Java and Node
        run: |
          export JAVA_HOME=/usr/lib/jvm/java-17-openjdk
          export PATH=$JAVA_HOME/bin:$PATH
          node -v
          npm -v
          java -version
          mvn -v

      - name: Install frontend dependencies
        run: |
          cd src/main/webapp
          npm ci

      - name: Build frontend
        run: |
          cd src/main/webapp
          npm run build -- --configuration production

      - name: Build backend (Spring Boot)
        run: |
          mvn clean package -Pprod -DskipTests

      - name: List output files
        run: |
          ls -lh target/*.jar || true
