name: CI

on:
  push:
    branches: [main]
    tags: ['v*']
  pull_request:
    branches: [main]

jobs:
  check:
    runs-on: ubuntu-latest
    timeout-minutes: 10
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 22
          cache: npm
      - run: npm install --no-audit --no-fund
      - run: npm run build
      - run: npx eslint packages/*/src
      - run: npx prettier --check "packages/**/*.{ts,js,css,html}"
      - run: npm test
      - name: Install Playwright browsers
        run: npx playwright install --with-deps chromium
      - name: Playwright web e2e
        run: npx playwright test --project=web
        # Electron playwright project is intentionally skipped on CI: it needs
        # a display + electron-rebuild of better-sqlite3, which is flaky in CI.

  build-linux:
    needs: check
    if: startsWith(github.ref, 'refs/tags/v') || github.ref == 'refs/heads/main'
    runs-on: ubuntu-latest
    timeout-minutes: 20
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 22
          cache: npm
      - run: npm install --no-audit --no-fund
      - run: npm run build
      - run: npx electron-builder --linux --publish never
      - uses: actions/upload-artifact@v4
        with:
          name: agent-desk-linux
          path: |
            release/*.AppImage
            release/*.deb

  build-windows:
    needs: check
    if: startsWith(github.ref, 'refs/tags/v') || github.ref == 'refs/heads/main'
    runs-on: windows-latest
    timeout-minutes: 20
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 22
          cache: npm
      - run: npm install --no-audit --no-fund
      - run: npm run build
      - run: npx electron-builder --win --publish never
      - uses: actions/upload-artifact@v4
        with:
          name: agent-desk-windows
          path: |
            release/*.exe

  build-mac:
    needs: check
    if: startsWith(github.ref, 'refs/tags/v') || github.ref == 'refs/heads/main'
    runs-on: macos-latest
    timeout-minutes: 30
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 22
          cache: npm
      - run: npm install --no-audit --no-fund
      - run: npm run build
      - run: npx electron-builder --mac --arm64 --x64 --publish never
      - uses: actions/upload-artifact@v4
        with:
          name: agent-desk-mac
          path: |
            release/*.dmg
            release/*.zip

  publish-npm:
    needs: check
    if: startsWith(github.ref, 'refs/tags/v')
    runs-on: ubuntu-latest
    timeout-minutes: 10
    permissions:
      contents: read
      id-token: write
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 22
          registry-url: https://registry.npmjs.org
          cache: npm
      - run: npm install --no-audit --no-fund
      - run: npm run build
      - run: npm publish --provenance --access public
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

  release:
    needs: [build-linux, build-windows, build-mac]
    if: startsWith(github.ref, 'refs/tags/v')
    runs-on: ubuntu-latest
    timeout-minutes: 10
    permissions:
      contents: write
    steps:
      - uses: actions/download-artifact@v4
        with:
          merge-multiple: true
          path: artifacts
      - name: Create GitHub Release
        uses: softprops/action-gh-release@v2
        with:
          files: artifacts/*
          generate_release_notes: true
