version: '3.8'

services:
  # PostgreSQL for testing
  postgres-test:
    image: postgres:15-alpine
    environment:
      POSTGRES_USER: mpc_user
      POSTGRES_PASSWORD: mpc_password
      POSTGRES_DB: mpc_system_test
    ports:
      - "5433:5432"
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U mpc_user -d mpc_system_test"]
      interval: 5s
      timeout: 5s
      retries: 5

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

  # RabbitMQ for testing
  rabbitmq-test:
    image: rabbitmq:3-management-alpine
    environment:
      RABBITMQ_DEFAULT_USER: mpc_user
      RABBITMQ_DEFAULT_PASS: mpc_password
    ports:
      - "5673:5672"
      - "15673:15672"
    healthcheck:
      test: ["CMD", "rabbitmq-diagnostics", "check_running"]
      interval: 10s
      timeout: 10s
      retries: 5

  # Database migration service
  migrate:
    image: migrate/migrate
    depends_on:
      postgres-test:
        condition: service_healthy
    volumes:
      - ../migrations:/migrations
    command: [
      "-path", "/migrations",
      "-database", "postgres://mpc_user:mpc_password@postgres-test:5432/mpc_system_test?sslmode=disable",
      "up"
    ]

  # Integration test runner
  integration-tests:
    build:
      context: ..
      dockerfile: tests/Dockerfile.test
    depends_on:
      postgres-test:
        condition: service_healthy
      redis-test:
        condition: service_healthy
      rabbitmq-test:
        condition: service_healthy
      migrate:
        condition: service_completed_successfully
    environment:
      TEST_DATABASE_URL: postgres://mpc_user:mpc_password@postgres-test:5432/mpc_system_test?sslmode=disable
      TEST_REDIS_URL: redis-test:6379
      TEST_RABBITMQ_URL: amqp://mpc_user:mpc_password@rabbitmq-test:5672/
    command: ["go", "test", "-v", "-tags=integration", "./tests/integration/..."]

  # E2E test services
  session-coordinator-test:
    build:
      context: ..
      dockerfile: services/session-coordinator/Dockerfile
    depends_on:
      postgres-test:
        condition: service_healthy
      redis-test:
        condition: service_healthy
      rabbitmq-test:
        condition: service_healthy
      migrate:
        condition: service_completed_successfully
    environment:
      MPC_DATABASE_HOST: postgres-test
      MPC_DATABASE_PORT: 5432
      MPC_DATABASE_USER: mpc_user
      MPC_DATABASE_PASSWORD: mpc_password
      MPC_DATABASE_DBNAME: mpc_system_test
      MPC_DATABASE_SSLMODE: disable
      MPC_REDIS_HOST: redis-test
      MPC_REDIS_PORT: 6379
      MPC_RABBITMQ_HOST: rabbitmq-test
      MPC_RABBITMQ_PORT: 5672
      MPC_RABBITMQ_USER: mpc_user
      MPC_RABBITMQ_PASSWORD: mpc_password
      MPC_SERVER_HTTP_PORT: 8080
      MPC_SERVER_GRPC_PORT: 9090
      MPC_SERVER_ENVIRONMENT: test
    ports:
      - "8080:8080"
      - "9090:9090"
    healthcheck:
      test: ["CMD", "wget", "-q", "-O", "/dev/null", "http://localhost:8080/health"]
      interval: 5s
      timeout: 5s
      retries: 10

  account-service-test:
    build:
      context: ..
      dockerfile: services/account/Dockerfile
    depends_on:
      postgres-test:
        condition: service_healthy
      redis-test:
        condition: service_healthy
      rabbitmq-test:
        condition: service_healthy
      migrate:
        condition: service_completed_successfully
    environment:
      MPC_DATABASE_HOST: postgres-test
      MPC_DATABASE_PORT: 5432
      MPC_DATABASE_USER: mpc_user
      MPC_DATABASE_PASSWORD: mpc_password
      MPC_DATABASE_DBNAME: mpc_system_test
      MPC_DATABASE_SSLMODE: disable
      MPC_REDIS_HOST: redis-test
      MPC_REDIS_PORT: 6379
      MPC_RABBITMQ_HOST: rabbitmq-test
      MPC_RABBITMQ_PORT: 5672
      MPC_RABBITMQ_USER: mpc_user
      MPC_RABBITMQ_PASSWORD: mpc_password
      MPC_SERVER_HTTP_PORT: 8083
      MPC_SERVER_ENVIRONMENT: test
      MPC_JWT_SECRET_KEY: test-secret-key-for-jwt-tokens!!
      MPC_JWT_ISSUER: mpc-test
    ports:
      - "8083:8083"
    healthcheck:
      test: ["CMD", "wget", "-q", "-O", "/dev/null", "http://localhost:8083/health"]
      interval: 5s
      timeout: 5s
      retries: 10

  # E2E test runner
  e2e-tests:
    build:
      context: ..
      dockerfile: tests/Dockerfile.test
    depends_on:
      session-coordinator-test:
        condition: service_healthy
      account-service-test:
        condition: service_healthy
    environment:
      SESSION_COORDINATOR_URL: http://session-coordinator-test:8080
      ACCOUNT_SERVICE_URL: http://account-service-test:8083
    command: ["go", "test", "-v", "-tags=e2e", "./tests/e2e/..."]

networks:
  default:
    name: mpc-test-network
