services:
  # PostgreSQL database for real-world testing
  db:
    image: postgres:15-alpine
    environment:
      - POSTGRES_DB=gridpilot_test
      - POSTGRES_USER=gridpilot_test_user
      - POSTGRES_PASSWORD=gridpilot_test_pass
    ports:
      - "5433:5432" # Use different port to avoid conflicts with dev
    volumes:
      - test_db_data:/var/lib/postgresql/data
    networks:
      - gridpilot-test-network
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U gridpilot_test_user -d gridpilot_test"]
      interval: 2s
      timeout: 2s
      retries: 10
      start_period: 5s
    restart: "no"

  # Ready check - simple service that verifies dependencies are available
  ready:
    image: node:20-alpine
    working_dir: /app
    volumes:
      - ./:/app
    command:
      [
        "sh",
        "-lc",
        "set -e; echo '[ready] Checking dependencies...'; if [ -d \"/app/node_modules\" ] && [ -f \"/app/node_modules/.package-lock.json\" ]; then echo '[ready] Dependencies found'; exit 0; else echo '[ready] Dependencies not found - please run: npm install'; exit 1; fi"
      ]
    networks:
      - gridpilot-test-network
    restart: "no"

  # Real API server with TypeORM/PostgreSQL
  api:
    image: node:20-alpine
    working_dir: /app/apps/api
    environment:
      - NODE_ENV=test
      - PORT=3000
      - GRIDPILOT_API_PERSISTENCE=postgres
      - GRIDPILOT_API_BOOTSTRAP=true
      - GRIDPILOT_API_FORCE_RESEED=true
      - DATABASE_URL=postgres://gridpilot_test_user:gridpilot_test_pass@db:5432/gridpilot_test
      - POSTGRES_DB=gridpilot_test
      - POSTGRES_USER=gridpilot_test_user
      - POSTGRES_PASSWORD=gridpilot_test_pass
    ports:
      - "3101:3000"
    volumes:
      - ./:/app
      - /Users/marcmintel/Projects/gridpilot/node_modules:/app/node_modules:ro
    command: ["sh", "-lc", "echo '[api] Starting real API with TypeORM...'; npm run start:dev"]
    depends_on:
      ready:
        condition: service_completed_successfully
      db:
        condition: service_healthy
    networks:
      - gridpilot-test-network
    restart: unless-stopped
    healthcheck:
      test:
        [
          "CMD",
          "node",
          "-e",
          "fetch('http://localhost:3000/health').then(r=>process.exit(r.ok?0:1)).catch(()=>process.exit(1))",
        ]
      interval: 2s
      timeout: 2s
      retries: 30
      start_period: 10s

networks:
  gridpilot-test-network:
    driver: bridge

volumes:
  test_db_data: