version: "3.8"

services:
  frontend:
    image: yeagerai/simulator-frontend:${LOCALNETVERSION:-latest}
    ports:
      - "${FRONTEND_PORT:-8080}:8080"
    environment:
      - VITE_*
    env_file:
      - ./.env
    depends_on:
      jsonrpc:
        condition: service_healthy
    expose:
      - "${FRONTEND_PORT:-8080}"
    restart: always
    security_opt:
      - "no-new-privileges=true"
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "3"


  jsonrpc:
    image: yeagerai/simulator-jsonrpc:${LOCALNETVERSION:-latest}
    environment:
      - FLASK_SERVER_PORT=${RPCPORT:-5000}
      - PYTHONUNBUFFERED=1
      - RPCDEBUGPORT=${RPCDEBUGPORT:-5001}
      - WEBREQUESTPORT=${WEBREQUESTPORT:-5002}
      - WEBREQUESTHOST=${WEBREQUESTHOST:-localhost}
      - WEBREQUESTPROTOCOL=${WEBREQUESTPROTOCOL:-http}
    ports:
      - "${RPCPORT:-5000}:${RPCPORT:-5000}"
      - "${RPCDEBUGPORT:-5001}:${RPCDEBUGPORT:-5001}"
    env_file:
      - ./.env
    healthcheck:
      test: [ "CMD", "python3", "backend/healthcheck.py", "--port", "${RPCPORT}" ]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 30s
    depends_on:
      database-migration:
        condition: service_completed_successfully
      webrequest:
        condition: service_healthy
    expose:
      - "${RPCPORT:-5000}"
    restart: always
    security_opt:
      - "no-new-privileges=true"
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "3"
    deploy:
      replicas: ${JSONRPC_REPLICAS:-1}
    volumes:
      - hardhat_artifacts:/app/hardhat/artifacts
      - hardhat_deployments:/app/hardhat/deployments

  webrequest:
    image: yeagerai/simulator-webrequest:${LOCALNETVERSION:-latest}
    shm_size: 2gb
    environment:
      - FLASK_SERVER_PORT=${WEBREQUESTPORT:-5002}
      - WEBREQUESTSELENIUMPORT=${WEBREQUESTSELENIUMPORT:-4444}
      - PYTHONUNBUFFERED=1
      - WEBREQUESTPORT=${WEBREQUESTPORT}
      - WEBREQUESTHOST=${WEBREQUESTHOST}
      - WEBREQUESTPROTOCOL=${WEBREQUESTPROTOCOL}
    expose:
      - "${WEBREQUESTPORT:-5002}:${WEBREQUESTPORT:-5002}"
      - "${WEBREQUESTSELENIUMPORT:-4444}:${WEBREQUESTSELENIUMPORT:-4444}"
    env_file:
      - ./.env
    depends_on:
      ollama:
        condition: service_started
    restart: always
    security_opt:
      - "no-new-privileges=true"
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "3"

  ollama:
    image: ollama/ollama:0.5.11
    ports:
      - 11434:11434
    container_name: ollama
    tty: true
    restart: always
    security_opt:
      - "no-new-privileges=true"
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "3"

  postgres:
    image: postgres:16-alpine
    ports:
      - "${DBPORT:-5432}:5432"
    environment:
      - POSTGRES_USER=${DBUSER:-postgres}
      - POSTGRES_PASSWORD=${DBPASSWORD:-postgres}
      - POSTGRES_DB=${DBNAME:-simulator_db}
    healthcheck:
      test: pg_isready -U ${DBUSER:-postgres} -d ${DBNAME:-simulator_db}
      interval: 10s
      timeout: 3s
      retries: 3
    restart: always
    security_opt:
      - "no-new-privileges=true"
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "3"

    # Uncomment the following lines if you want your DB to persist
    # volumes:
    #   - "./data/postgres:/var/lib/postgresql/data"

  database-migration:
    image: yeagerai/simulator-database-migration:${LOCALNETVERSION:-latest}
    environment:
      - DB_URL=postgresql://${DBUSER:-postgres}:${DBPASSWORD:-postgres}@postgres/${DBNAME:-simulator_db}
      - WEBREQUESTPORT=${WEBREQUESTPORT:-5002}
      - WEBREQUESTHOST=${WEBREQUESTHOST:-localhost}
      - WEBREQUESTPROTOCOL=${WEBREQUESTPROTOCOL:-http}
    depends_on:
      postgres:
        condition: service_healthy
      webrequest:
        condition: service_healthy

  hardhat:
    image: yeagerai/simulator-hardhat:${LOCALNETVERSION:-latest}
    ports:
      - "${HARDHAT_PORT:-8545}:8545"
    environment:
      - HARDHAT_NETWORK=hardhat
    volumes:
      - hardhat_artifacts:/app/artifacts
      - hardhat_deployments:/app/deployments

volumes:
  hardhat_artifacts:
  hardhat_deployments: