name: CI

on:
  pull_request:
  push:
    branches:
      - main

jobs:
  infra:
    name: Infra (Compose + Scripts)
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Validate Docker Compose config
        run: docker compose -f infra/docker-compose.yml config -q

      - name: Validate Docker Compose with .env.example
        run: docker compose -f infra/docker-compose.yml --env-file .env.example config -q

      - name: Validate startup scripts
        run: |
          bash -n start-unified.sh
          bash -n start.sh

      - name: Run startup preflight tests
        run: bash scripts/test-start-unified-preflight.sh

  backend:
    name: Backend (Go)
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: app/backend
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup Go
        uses: actions/setup-go@v5
        with:
          go-version-file: app/backend/go.mod
          cache: true

      - name: Run backend tests
        run: go test ./...

  frontend:
    name: Frontend (React)
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: app/frontend
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup Node
        uses: actions/setup-node@v4
        with:
          node-version: "22"
          cache: npm
          cache-dependency-path: app/frontend/package-lock.json

      - name: Install dependencies
        run: npm ci

      - name: Lint
        run: npm run lint

      - name: Build (typecheck + bundle)
        run: npm run build:check

      - name: Test
        run: npm test
