name: Build and Test

on:
  push:
    branches:
      - main
      - develop
  pull_request:
    branches:
      - main
      - develop

jobs:
  build-and-test:
    runs-on: ubuntu-latest
    services:
      postgres:
        image: postgres:17-alpine
        env:
          POSTGRES_USER: testuser
          POSTGRES_PASSWORD: testpassword
          POSTGRES_DB: testdb
        ports:
          - 5432:5432
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5

    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Set up Python
        uses: actions/setup-python@v3
        with:
          python-version: '3.11'

      - name: Set up Node.js
        uses: actions/setup-node@v3
        with:
          node-version: '24'

      - name: Install backend dependencies
        working-directory: ./be
        run: |
          pip install --upgrade pip
          pip install -r requirements.txt

      - name: Install frontend dependencies
        working-directory: ./fe
        run: npm ci

      - name: Run backend tests
        working-directory: ./be
        env:
          DATABASE_URL: postgresql+asyncpg://testuser:testpassword@localhost:5432/testdb
          SECRET_KEY: testsecretkey
          GEMINI_API_KEY: testgeminikey # Mock or skip tests requiring this if not available
          SESSION_SECRET_KEY: testsessionsecret
        run: pytest

      - name: Build frontend
        working-directory: ./fe
        run: npm run build

      # Add frontend test command if you have one e.g. npm test
      # - name: Run frontend tests
      #   working-directory: ./fe
      #   run: npm test