# -----------------------------------------------------------------------------
# cameleer-website — CI (build + test + lint + Lighthouse)
#
# Runs automatically on every push and every PR against main. Does NOT deploy —
# see deploy.yml for that. This workflow exists so every commit gets the full
# quality gate before it can reach production.
#
# Runner: self-hosted arm64 Gitea runner (act_runner).
# Adjust `runs-on` labels if your runner is registered under different tags.
# Architecture note: arm64 build, amd64 deploy is fine — Astro's output is
# plain static HTML/CSS/JS with no arch-specific bits.
# -----------------------------------------------------------------------------

name: ci

on:
  push:
  pull_request:
    branches: [main]

jobs:
  build-test:
    runs-on: ubuntu-latest
    timeout-minutes: 20
    env:
      PUBLIC_AUTH_SIGNIN_URL: ${{ secrets.PUBLIC_AUTH_SIGNIN_URL }}
      PUBLIC_AUTH_SIGNUP_URL: ${{ secrets.PUBLIC_AUTH_SIGNUP_URL }}
      PUBLIC_SALES_EMAIL: ${{ secrets.PUBLIC_SALES_EMAIL }}

    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-node@v4
        with:
          node-version: '20'
          cache: 'npm'

      # Lighthouse CI needs a Chrome/Chromium binary at runtime. Google Chrome
      # has no Linux/arm64 build, so we use distro Chromium when available and
      # fall back to Playwright's bundled Chromium (which supports linux/arm64)
      # when not. The Ubuntu runner ships /usr/bin/chromium-browser as a snap
      # forwarder stub that is on PATH but only prints "install via snap" when
      # invoked — so we MUST probe each candidate by actually running it,
      # not just `command -v`.
      - name: Install Chromium for Lighthouse CI
        shell: bash
        run: |
          set -euo pipefail

          probe() {
            local bin="${1:-}"
            [ -n "$bin" ] && [ -x "$bin" ] && "$bin" --version >/dev/null 2>&1
          }

          CHROME_BIN=""
          for cand in \
            "$(command -v chromium 2>/dev/null || true)" \
            "$(command -v chromium-browser 2>/dev/null || true)" \
            "$(command -v google-chrome 2>/dev/null || true)"; do
            if probe "$cand"; then CHROME_BIN="$cand"; break; fi
          done

          if [ -z "$CHROME_BIN" ]; then
            echo "No working system Chromium — installing Playwright-bundled Chromium."
            # --with-deps apt-installs the system libraries Chromium needs
            # (libnss3, libatk1.0-0, etc.). Playwright handles sudo internally.
            npx -y playwright@latest install --with-deps chromium
            CHROME_BIN="$(find "$HOME/.cache/ms-playwright" \
              -type f -name chrome -executable 2>/dev/null | head -n1)"
          fi

          if ! probe "$CHROME_BIN"; then
            echo "Failed to install a working Chromium binary." >&2
            exit 1
          fi

          echo "CHROME_PATH=$CHROME_BIN" >> "$GITHUB_ENV"
          "$CHROME_BIN" --version

      - name: Install dependencies
        run: npm ci

      - name: Run unit tests
        run: npm test

      - name: Build site
        run: npm run build

      - name: Guard — no TBD markers may ship in built HTML
        run: |
          if grep -rlE '(TBD):' dist 2>/dev/null | grep -E '\.(html|svg)$'; then
            echo "Built output contains unfilled <TBD:...>) markers."
            echo "Fill in imprint.astro and privacy.astro operator fields before merging to main."
            exit 1
          fi

      - name: Validate HTML
        run: npm run lint:html

      - name: Check internal links
        run: npm run lint:links

      - name: Lighthouse CI
        env:
          CHROME_PATH: ${{ env.CHROME_PATH }}
        run: npx lhci autorun
