services:
  postgres:
    image: postgres:16-alpine
    ports:
      - 5422:5432
    environment:
      - POSTGRES_PASSWORD=secret
      - POSTGRES_USER=root
      - POSTGRES_DB=gh
    networks:
      - app
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U root -d gh"]
      interval: 5s
      timeout: 3s
      retries: 5
    volumes:
      - postgres_data:/var/lib/postgresql/data

  mongo:
    container_name: fortunebet-mongo
    image: mongo:7.0
    restart: always
    ports:
      - "27017:27017"
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: secret
    volumes:
      - mongo_data:/data/db
    networks:
      - app
    healthcheck:
      test: ["CMD", "mongosh", "--eval", "db.adminCommand('ping')"]
      interval: 10s
      timeout: 5s
      retries: 5

  migrate:
    image: migrate/migrate
    volumes:
      - ./db/migrations:/migrations
    depends_on:
      postgres:
        condition: service_healthy
    command:
      [
        "-path",
        "/migrations",
        "-database",
        "postgresql://root:secret@postgres:5432/gh?sslmode=disable",
        "up",
      ]
    networks:
      - app

  app:
    build:
      context: .
      dockerfile: Dockerfile
      target: runner
    ports:
      - ${PORT}:8080
    environment:
      - DB_URL=postgresql://root:secret@postgres:5432/gh?sslmode=disable
      - MONGO_URI=mongodb://root:secret@mongo:27017
    depends_on:
      migrate:
        condition: service_completed_successfully
      mongo:
        condition: service_healthy
    networks:
      - app
    command: ["/app/bin/web"]

  test:
    build:
      context: .
      dockerfile: Dockerfile
      target: builder
    volumes:
      - .:/app
    command: ["tail", "-f", "/dev/null"]
    networks:
      - app

networks:
  app:
    driver: bridge

volumes:
  postgres_data:
  mongo_data:
