name: CI

on:
  pull_request:
  push:
    branches:
      - dev
  workflow_dispatch:

concurrency:
  group: ci-${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
  cancel-in-progress: true

permissions:
  contents: read

jobs:
  repository-policy:
    name: Repository policy
    runs-on: ubuntu-latest
    timeout-minutes: 10
    steps:
      - name: Check out repository
        uses: actions/checkout@v6

      - name: Set up Node.js
        uses: actions/setup-node@v7
        with:
          node-version: 22

      - name: Validate repository contracts
        run: node .github/scripts/validate-repository.mjs

      - name: Install Lua compiler
        run: |
          sudo apt-get update
          sudo apt-get install --yes lua5.4

      - name: Check Lua syntax
        shell: bash
        run: |
          set -euo pipefail
          while IFS= read -r -d '' file; do
            luac5.4 -p "$file"
          done < <(find sky_phone tests -type f -name '*.lua' -print0)

  frontend:
    name: Frontend
    runs-on: ubuntu-latest
    timeout-minutes: 20
    defaults:
      run:
        working-directory: frontend
    steps:
      - name: Check out repository
        uses: actions/checkout@v6

      - name: Set up pnpm
        uses: pnpm/action-setup@v6
        with:
          version: 10.33.0

      - name: Set up Node.js
        uses: actions/setup-node@v7
        with:
          node-version: 22
          cache: pnpm
          cache-dependency-path: frontend/pnpm-lock.yaml

      - name: Install dependencies
        run: pnpm install --frozen-lockfile

      - name: Check repository formatting and schemas
        run: >-
          pnpm exec prettier --check
          "../.github/**/*.{yml,yaml,json,md}"
          "../.github/scripts/*.mjs"
          "../CONTRIBUTING.md"
          "../SECURITY.md"

      - name: Lint frontend
        run: pnpm lint

      - name: Typecheck frontend
        run: pnpm typecheck

      - name: Test frontend
        run: pnpm test

      - name: Build deployable NUI
        run: pnpm build-only

      - name: Verify published NUI entrypoint
        run: test -f ../sky_phone/source/html/index.html

      - name: Package pull request test resource
        if: github.event_name == 'pull_request'
        shell: bash
        working-directory: .
        env:
          PR_NUMBER: ${{ github.event.pull_request.number }}
          HEAD_SHA: ${{ github.event.pull_request.head.sha }}
        run: |
          set -euo pipefail
          artifact_name="sky_phone-pr-${PR_NUMBER}-${HEAD_SHA}"
          mkdir -p artifacts
          zip -r "artifacts/${artifact_name}.zip" sky_phone
          unzip -t "artifacts/${artifact_name}.zip"
          unzip -Z1 "artifacts/${artifact_name}.zip" | grep -Fxq 'sky_phone/fxmanifest.lua'
          unzip -Z1 "artifacts/${artifact_name}.zip" | grep -Fxq 'sky_phone/source/html/index.html'

      - name: Upload pull request test resource
        if: github.event_name == 'pull_request'
        uses: actions/upload-artifact@v7
        with:
          path: artifacts/sky_phone-pr-${{ github.event.pull_request.number }}-${{ github.event.pull_request.head.sha }}.zip
          archive: false
          if-no-files-found: error
          retention-days: 14
