services:
  # Main application database.
  app_db:
    image: mysql:5.7
    command: --default-authentication-plugin=mysql_native_password
    environment:
      MYSQL_ROOT_PASSWORD: ${ROOT_PASSWORD:-password}
      MYSQL_DATABASE: ${DB_NAME:-wordpress}
      MYSQL_USER: ${DB_USER:-wordpress}
      MYSQL_PASSWORD: ${DB_PASSWORD:-password}
    networks:
      development:

  # Capture and display all email sent by WordPress.
  mailhog:
    image: mailhog/mailhog
    ports:
      - "1025:1025"
      - "8025:8025"
    networks:
      development:

  # Main application service
  testable_app:
    image: woographql/wordpress-dev:${WP_VERSION:-latest}
    depends_on:
      - app_db
      - mailhog
    build:
      context: .
      args:
        PHP_VERSION: ${PHP_VERSION:-8.0}
    volumes:
      - .:/var/www/html/wp-content/plugins/wp-graphql-woocommerce
      - ./codeception.dist.yml:/var/www/html/wp-content/plugins/wp-graphql-woocommerce/codeception.yml
      - ./local/public:/var/www/html # WP core files.
      - ./local/.htaccess:/var/www/html/.htaccess
      - ./.log/testable_app:/var/log/apache2
    env_file: .env.testing
    environment:
      APACHE_RUN_USER: "#1000" # Ensure Apache can write to the filesystem.
      DOCKER_GATEWAY_HOST: "172.17.0.1"
      XDEBUG_CONFIG: remote_host=${DOCKER_GATEWAY_HOST:-host.docker.internal} idekey=vscode
    networks:
      development:
    ports:
      - "9001:9001"

  # Main application database clone for testing inside the main application service.
  testing_db:
    image: mysql:5.7
    volumes:
      - ./local/db:/docker-entrypoint-initdb.d
    command: --default-authentication-plugin=mysql_native_password
    environment:
      MYSQL_ROOT_PASSWORD: ${ROOT_PASSWORD:-password}
      MYSQL_DATABASE: ${DB_NAME:-wordpress}
      MYSQL_USER: ${DB_USER:-wordpress}
      MYSQL_PASSWORD: ${DB_PASSWORD:-password}
    networks:
      development:
      testing:
        aliases:
          - app_db

  # Standalone testing application service (for inside of CI).
  run_tests:
    image: woographql/wordpress-ci:${WP_VERSION:-latest}
    depends_on:
      - testing_db
    build:
      context: .
      args:
        PHP_VERSION: ${PHP_VERSION:-8.3}
    volumes:
      - .:/var/www/html/wp-content/plugins/wp-graphql-woocommerce
      - ./codeception.dist.yml:/var/www/html/wp-content/plugins/wp-graphql-woocommerce/codeception.yml
      - ./local/public:/var/www/html # WP core files.
      - ./local/.htaccess:/var/www/html/.htaccess
      - ./.log/run_tests:/var/log/apache2
    env_file: .env.testing
    environment:
      RUNNING_TEST_STANDALONE: 1
      GRAPHQL_DEBUG: 1
      WPGRAPHQL_WOOCOMMERCE_AUTOLOAD: 1
      APACHE_RUN_USER: "#1000" # Ensure Apache can write to the filesystem.
      WORDPRESS_DOMAIN: localhost
      WP_SITEURL: http://localhost
      WP_HOME: http://localhost
      HPOS: ${HPOS:-}
    command: ./setup-database.sh testing codecept run ${FILTER:-}
    networks:
      testing:
        aliases:
          - run_tests

networks:
  development:
  testing:
