name: ci

on:
  push:
    branches: ["main", "master", "sales"]
  pull_request:
  workflow_dispatch:

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup Python
        uses: actions/setup-python@v5
        with:
          python-version: "3.11"

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -r requirements.txt

      - name: Run tests
        run: pytest -q

  postgres-readiness:
    runs-on: ubuntu-latest
    services:
      postgres:
        image: postgres:16-alpine
        env:
          POSTGRES_DB: mvpcc
          POSTGRES_USER: mvp
          POSTGRES_PASSWORD: mvp
        options: >-
          --health-cmd "pg_isready -U mvp -d mvpcc"
          --health-interval 10s
          --health-timeout 5s
          --health-retries 10
      rabbitmq:
        image: rabbitmq:3-management
    env:
      DATABASE_URL: postgresql://mvp:mvp@postgres:5432/mvpcc
      SCHEMA_MANAGEMENT_MODE: migrations
      CC_DATA_DIR: .testdata_pg
      EVENT_BUS_ENABLED: "0"
      EVENT_BUS_URL: amqp://guest:guest@rabbitmq:5672/
      APP_TOKEN_SECRET: ci-secret
      ALLOW_LEGACY_HEADER_AUTH: "1"
      TELEGRAM_BOT_ENABLED: "0"
      WHATSAPP_OUTBOUND_ENABLED: "0"
      AI_PROVIDER: stub
      AI_TELEGRAM_ENABLED: "0"
      AI_WHATSAPP_ENABLED: "0"
      AI_VOICE_ENABLED: "0"
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup Python
        uses: actions/setup-python@v5
        with:
          python-version: "3.11"

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -r requirements.txt

      - name: Apply migrations
        run: python scripts/migrate_core_db.py

      - name: Run PostgreSQL readiness smoke
        run: pytest -q tests/test_postgres_readiness.py

  build-image:
    runs-on: ubuntu-latest
    needs: [test, postgres-readiness]
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Build image
        run: docker build -t mvp-cc-platform:ci .
