name: NexusGuard CI

on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main

env:
  BUILD_TOKEN: ${{ secrets.BUILD_TOKEN }}

jobs:
  # ──────────────────────────────────────────────
  # TEST — all 3 components in parallel
  # ──────────────────────────────────────────────
  server-core-test:
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: apps/server-core
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: true
          fetch-depth: 0
          token: ${{ secrets.BUILD_TOKEN }}
          persist-credentials: true
          github-server-url: 'https://git.datadunia.com'
      - uses: actions/setup-go@v5
        with:
          go-version: '1.25'
      - name: Test
        run: go test ./... -tags dev -cover

  device-agent-test:
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: apps/device-agent
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: true
          fetch-depth: 0
          token: ${{ secrets.BUILD_TOKEN }}
          persist-credentials: true
          github-server-url: 'https://git.datadunia.com'
      - uses: actions/setup-go@v5
        with:
          go-version: '1.25'
      - name: Test
        run: go test ./... -cover

  dashboard-test:
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: apps/dashboard-ui
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: true
          fetch-depth: 0
          token: ${{ secrets.BUILD_TOKEN }}
          persist-credentials: true
          github-server-url: 'https://git.datadunia.com'
      - uses: actions/setup-node@v4
        with:
          node-version: '24'
      - name: Install
        run: npm ci || npm install
      - name: Build
        run: npm run build

  # ──────────────────────────────────────────────
  # BUILD — binaries + frontend dist
  # ──────────────────────────────────────────────
  server-core-build:
    runs-on: ubuntu-latest
    needs: server-core-test
    defaults:
      run:
        working-directory: apps/server-core
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: true
          token: ${{ secrets.BUILD_TOKEN }}
          persist-credentials: true
          github-server-url: 'https://git.datadunia.com'
      - uses: actions/setup-go@v5
        with:
          go-version: '1.25'
      - name: Build
        run: go build -o bin/server-core .
      - name: Upload binary
        uses: actions/upload-artifact@v4
        with:
          name: server-core-linux-amd64
          path: apps/server-core/bin/server-core

  device-agent-cross-build:
    runs-on: ubuntu-latest
    needs: device-agent-test
    strategy:
      matrix:
        goos: [linux, windows]
        goarch: [amd64, arm64]
        exclude:
          - goos: windows
            goarch: arm64
    defaults:
      run:
        working-directory: apps/device-agent
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: true
          token: ${{ secrets.BUILD_TOKEN }}
          persist-credentials: true
          github-server-url: 'https://git.datadunia.com'
      - uses: actions/setup-go@v5
        with:
          go-version: '1.25'
      - name: Build
        env:
          CGO_ENABLED: 0
          GOOS: ${{ matrix.goos }}
          GOARCH: ${{ matrix.goarch }}
        run: |
          EXT=""
          if [ "${{ matrix.goos }}" = "windows" ]; then EXT=".exe"; fi
          go build -o bin/nexus-device-agent-${{ matrix.goos }}-${{ matrix.goarch }}${EXT} .
      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: nexus-device-agent-${{ matrix.goos }}-${{ matrix.goarch }}
          path: apps/device-agent/bin/nexus-device-agent-*

  dashboard-dist:
    runs-on: ubuntu-latest
    needs: dashboard-test
    defaults:
      run:
        working-directory: apps/dashboard-ui
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: true
          token: ${{ secrets.BUILD_TOKEN }}
          github-server-url: 'https://git.datadunia.com'
      - uses: actions/setup-node@v4
        with:
          node-version: '24'
      - name: Install
        run: npm ci || npm install
      - name: Build
        run: npm run build
      - name: Upload dist
        uses: actions/upload-artifact@v4
        with:
          name: dashboard-ui-dist
          path: apps/dashboard-ui/dist/


