services:
  gateway:
    image: nginx:1.27-alpine
    depends_on:
      backend:
        condition: service_started
      frontend:
        condition: service_started
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf:ro

  db:
    image: mysql:8.0
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: faildaily
      MYSQL_USER: faildaily_user
      MYSQL_PASSWORD: root
    command: --default-authentication-plugin=mysql_native_password
    healthcheck:
      # Note: use $$ to prevent docker compose from interpolating at parse time
      test: ["CMD-SHELL", "mysqladmin ping -h 127.0.0.1 -uroot -p\"$$MYSQL_ROOT_PASSWORD\" --silent || mysqladmin ping -h 127.0.0.1 -uroot --silent"]
      interval: 5s
      timeout: 3s
      retries: 30
      start_period: 60s
    volumes:
      - ../../backend-api/migrations/faildaily_bdd.sql:/docker-entrypoint-initdb.d/01_schema.sql:ro
      - ./02_seed_static.empty.sql:/docker-entrypoint-initdb.d/02_seed_static.sql:ro

  mailhog:
    image: mailhog/mailhog

  backend:
    build:
      context: ../../backend-api
      dockerfile: ../docker/production/backend.prod.Dockerfile
    environment:
      NODE_ENV: test
      DB_WAIT_FOR_READY: "true"
      PORT: 3000
      DB_HOST: db
      DB_PORT: 3306
      DB_NAME: faildaily
      DB_USER: faildaily_user
      DB_PASSWORD: root
      LOGS_DB_DISABLED: "true"
      # Required by backend secret loader even when logs DB is disabled in NODE_ENV=test
      LOGS_DB_HOST: db
      LOGS_DB_PORT: 3306
      LOGS_DB_USER: faildaily_user
      LOGS_DB_PASSWORD: root
      LOGS_DB_NAME: faildaily_logs
      JWT_SECRET: e2e-secret
      APP_WEB_URL: http://gateway
      SMTP_HOST: mailhog
      SMTP_PORT: 1025
      SMTP_SECURE: 'false'
      SMTP_USER: ''
      SMTP_PASS: ''
      SMTP_FROM: 'FailDaily <no-reply@faildaily.test>'
      CORS_ORIGIN: http://frontend
    depends_on:
      db:
        condition: service_healthy
      mailhog:
        condition: service_started
    volumes:
      - backend_uploads:/app/uploads

  frontend:
    build:
      context: ../../frontend
      dockerfile: ../docker/production/frontend.prod.Dockerfile
    depends_on:
      backend:
        condition: service_started

  cypress:
    image: cypress/included:13.6.2
    working_dir: /e2e
    depends_on:
      - gateway
    environment:
      CYPRESS_baseUrl: http://gateway
      CYPRESS_apiUrl: http://gateway/api
    volumes:
      - ../../e2e:/e2e
    command: ["cypress", "run", "--browser", "chrome"]

volumes:
  backend_uploads:
