version: '3.8'

services:
  # PostgreSQL for testing
  postgres-test:
    image: postgres:15-alpine
    container_name: leaderboard-postgres-test
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: leaderboard_test_db
    ports:
      - "5433:5432"
    tmpfs:
      - /var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 3s
      timeout: 3s
      retries: 10

  # Redis for testing
  redis-test:
    image: redis:7-alpine
    container_name: leaderboard-redis-test
    ports:
      - "6380:6379"
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 3s
      timeout: 3s
      retries: 10

  # Kafka for testing
  zookeeper-test:
    image: confluentinc/cp-zookeeper:7.5.0
    container_name: leaderboard-zookeeper-test
    environment:
      ZOOKEEPER_CLIENT_PORT: 2181
      ZOOKEEPER_TICK_TIME: 2000

  kafka-test:
    image: confluentinc/cp-kafka:7.5.0
    container_name: leaderboard-kafka-test
    depends_on:
      - zookeeper-test
    ports:
      - "9093:9092"
    environment:
      KAFKA_BROKER_ID: 1
      KAFKA_ZOOKEEPER_CONNECT: zookeeper-test:2181
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka-test:29092,PLAINTEXT_HOST://localhost:9093
      KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT
      KAFKA_INTER_BROKER_LISTENER_NAME: PLAINTEXT
      KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
    healthcheck:
      test: ["CMD-SHELL", "kafka-broker-api-versions --bootstrap-server localhost:9092 || exit 1"]
      interval: 5s
      timeout: 10s
      retries: 10

  # Test runner container
  test-runner:
    build:
      context: .
      dockerfile: Dockerfile
      target: test
    container_name: leaderboard-test-runner
    depends_on:
      postgres-test:
        condition: service_healthy
      redis-test:
        condition: service_healthy
    environment:
      NODE_ENV: test
      DATABASE_URL: postgresql://postgres:postgres@postgres-test:5432/leaderboard_test_db
      REDIS_HOST: redis-test
      REDIS_PORT: 6379
      KAFKA_BROKERS: kafka-test:29092
      JWT_SECRET: test-jwt-secret
      JWT_EXPIRES_IN: 1d
    volumes:
      - ./coverage:/app/coverage
    command: >
      sh -c "npx prisma migrate deploy && npm test -- --coverage"

  # Integration test runner
  integration-test-runner:
    build:
      context: .
      dockerfile: Dockerfile
      target: test
    container_name: leaderboard-integration-test-runner
    depends_on:
      postgres-test:
        condition: service_healthy
      redis-test:
        condition: service_healthy
    environment:
      NODE_ENV: test
      DATABASE_URL: postgresql://postgres:postgres@postgres-test:5432/leaderboard_test_db
      REDIS_HOST: redis-test
      REDIS_PORT: 6379
      KAFKA_BROKERS: kafka-test:29092
      JWT_SECRET: test-jwt-secret
      JWT_EXPIRES_IN: 1d
    volumes:
      - ./coverage:/app/coverage
    command: >
      sh -c "npx prisma migrate deploy && npm run test:integration"

  # E2E test runner
  e2e-test-runner:
    build:
      context: .
      dockerfile: Dockerfile
      target: test
    container_name: leaderboard-e2e-test-runner
    depends_on:
      postgres-test:
        condition: service_healthy
      redis-test:
        condition: service_healthy
    environment:
      NODE_ENV: test
      DATABASE_URL: postgresql://postgres:postgres@postgres-test:5432/leaderboard_test_db
      REDIS_HOST: redis-test
      REDIS_PORT: 6379
      KAFKA_BROKERS: kafka-test:29092
      JWT_SECRET: test-jwt-secret
      JWT_EXPIRES_IN: 1d
    volumes:
      - ./coverage:/app/coverage
    command: >
      sh -c "npx prisma migrate deploy && npm run test:e2e"
