version: '3'

networks:
  dicoop:
    name: dicoop
    driver: bridge

services:
  node:
    image: dicoop/blockchain_v5.1.1:dev
    container_name: node
    ports:
      - "8888:8888"
      - "9876:9876"
      - "8080:8080"
    volumes:
      - ./components/boot/blockchain-data:/mnt/dev/data
      - ./components/boot/wallet-data:/root/eosio-wallet
      - ./components/contracts/build/contracts:/contracts
      - ./components/boot/src/configs:/mnt/dev/config
    command: >
      /bin/bash -c "/usr/local/bin/nodeos -d /mnt/dev/data --config-dir /mnt/dev/config --genesis-json /mnt/dev/config/genesis.json"
    restart: unless-stopped
    networks:
      - dicoop
      
  mongo:
    image: mongo:latest
    container_name: mongo
    restart: always
    volumes:
      - mongo_data:/data/db
    ports:
      - 127.0.0.1:27017:27017
    entrypoint: >
      /bin/bash -c "
      mongod --replSet rs0 --bind_ip_all & 
      sleep 5;
      mongosh --eval '
        try {
          rs.status();
        } catch (e) {
          rs.initiate({
            _id: \"rs0\",
            members: [{ _id: 0, host: \"127.0.0.1:27017\" }]
          });
        }
      ';
      tail -f /dev/null"

  redis:
    image: redis:latest
    container_name: redis
    restart: always
    ports:
      - 127.0.0.1:6379:6379

  postgres:
    image: postgres:latest
    container_name: postgres
    restart: always
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: voskhod
    volumes:
      - postgres_data:/var/lib/postgresql/data
    ports:
      - 127.0.0.1:5432:5432
  
  medusa:
    build:
      context: ./components/medusa/backend
      dockerfile: Dockerfile
    image: medusa:test
    container_name: medusa-server
    depends_on:
      - postgres
      - redis
    ports:
      - "9000:9000"
    volumes:
      - ./components/medusa/medusa-config.js:/app/medusa-config.js
      - ./components/medusa/.env:/app/.env
    command: >
      /bin/bash -c "
        psql -h postgres -U postgres -tc \"SELECT 1 FROM pg_database WHERE datname = 'medusa'\" | grep -q 1 || psql -h postgres -U postgres -c \"CREATE DATABASE medusa\";
        medusa develop
      "
    networks:
      - dicoop
volumes:
  mongo_data:
  postgres_data:
