name: CI

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]
  workflow_dispatch:

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

env:
  NODE_VERSION: "22.17.0"
  PNPM_VERSION: "10.25.0"
  FORCE_COLOR: "1"

jobs:
  lint:
    name: Lint
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: pnpm/action-setup@v4
        with:
          version: ${{ env.PNPM_VERSION }}
      - uses: actions/setup-node@v4
        with:
          node-version: ${{ env.NODE_VERSION }}
          cache: "pnpm"
      - run: pnpm install --frozen-lockfile
      - run: pnpm lint

  typecheck:
    name: Typecheck
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: pnpm/action-setup@v4
        with:
          version: ${{ env.PNPM_VERSION }}
      - uses: actions/setup-node@v4
        with:
          node-version: ${{ env.NODE_VERSION }}
          cache: "pnpm"
      - run: pnpm install --frozen-lockfile
      - run: pnpm typecheck

  test-broker:
    name: Broker tests (Postgres)
    runs-on: ubuntu-latest
    services:
      postgres:
        image: pgvector/pgvector:pg17
        env:
          POSTGRES_USER: turbostarter
          POSTGRES_PASSWORD: turbostarter
          POSTGRES_DB: claudemesh_test
        ports:
          - 5440:5432
        options: >-
          --health-cmd="pg_isready -U turbostarter"
          --health-interval=5s
          --health-timeout=3s
          --health-retries=10
    env:
      DATABASE_URL: postgresql://turbostarter:turbostarter@127.0.0.1:5440/claudemesh_test
    steps:
      - uses: actions/checkout@v4
      - uses: pnpm/action-setup@v4
        with:
          version: ${{ env.PNPM_VERSION }}
      - uses: actions/setup-node@v4
        with:
          node-version: ${{ env.NODE_VERSION }}
          cache: "pnpm"
      - run: pnpm install --frozen-lockfile
      - name: Run migrations
        run: pnpm --filter "@turbostarter/db" db:migrate
      - name: Broker test suite
        run: pnpm --filter "@claudemesh/broker" test

  build-amd64:
    name: Docker build (linux/amd64)
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: docker/setup-buildx-action@v3
      - name: Build broker image
        uses: docker/build-push-action@v6
        with:
          context: .
          file: apps/broker/Dockerfile
          platforms: linux/amd64
          push: false
          tags: claudemesh-broker:ci
          build-args: |
            GIT_SHA=${{ github.sha }}
      - name: Build migrate image
        uses: docker/build-push-action@v6
        with:
          context: .
          file: packages/db/Dockerfile
          platforms: linux/amd64
          push: false
          tags: claudemesh-migrate:ci
      - name: Build web image
        uses: docker/build-push-action@v6
        with:
          context: .
          file: apps/web/Dockerfile
          platforms: linux/amd64
          push: false
          tags: claudemesh-web:ci
          build-args: |
            NEXT_PUBLIC_URL=https://claudemesh.com
