services:
  #============== API Hub ====================================================================
  api:
    build:
      context: ./backend
      target: builder
    container_name: MIH-API-Hub
    #command: sh -c "sleep 10s; uvicorn backend.main:app --reload --port=8080 --host=0.0.0.0"
    #============Dev=================
    # command: sh -c "sleep 10s; fastapi dev main.py --port 8080"
    #============prod=================
    #command: sh -c "sleep 10s; fastapi run backend/main.py --proxy-headers --port 8080"
    ports:
      - 8080:80
    volumes:
      - ./backend:/app
    networks:
      - MIH-network
    depends_on:
      - mysqldb
  #============== My SQL DB ==================================================================== 
  mysqldb:
    #build: ./database/
    platform: linux/amd64
    image: mysql:5.7
    container_name: MIH-Database
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: ${SQL_ROOT_PW}
      MYSQL_USER: ${SQL_USER}
      MYSQL_PASSWORD: ${SQL_USER_PW}
      MYSQL_DATABASE: ${SUPERTOKENS_DB}
    networks:
      - MIH-network
    ports:
      - '3306:3306'
    volumes:
      - ./database:/var/lib/mysql
  #============== Super Token Auth ====================================================================
  supertokens:
    container_name: MIH-SuperTokens
    image: supertokens/supertokens-mysql:latest
    # image: registry.supertokens.io/supertokens/supertokens-mysql
    depends_on:
      - mysqldb
    ports:
      - 3567:3567
    environment:
      REFRESH_TOKEN_VALIDITY: '604800'
      ACCESS_TOKEN_VALIDITY: '86400'
      PASSWORD_RESET_TOKEN_LIFETIME: '7200000'
      MYSQL_USER: ${SQL_USER}
      MYSQL_PASSWORD: ${SQL_USER_PW}
      MYSQL_HOST: mysqldb
      MYSQL_PORT: 3306
      MYSQL_DATABASE_NAME: ${SUPERTOKENS_DB}
      API_KEYS: ${SUPERTOKENS_API_KEY}
    networks:
      - MIH-network
    restart: unless-stopped
    healthcheck:
      test: >
        bash -c 'exec 3<>/dev/tcp/127.0.0.1/3567 && echo -e "GET /hello HTTP/1.1\r\nhost: 127.0.0.1:3567\r\nConnection: close\r\n\r\n" >&3 && cat <&3 | grep "Hello"'
      interval: 10s
      timeout: 5s
      retries: 5
  #============== PHP My Admin ====================================================================        
  phpmyadmin:
    platform: linux/amd64
    image: phpmyadmin/phpmyadmin
    container_name: MIH-phpmyadmin
    environment:
      PMA_HOST: mysqlDB
      PMA_PORT: 3306
      PMA_ARBITRARY:
    networks:
      - MIH-network
    restart: always
    ports:
      - 8081:80
    depends_on:
      - mysqldb
  #============== Nginx Proxy Server ====================================================================
  nginx:
    container_name: nginx
    restart: unless-stopped
    image: nginx
    ports:
      - 80:80
      - 443:443
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf
      - certbotConf:/etc/letsencrypt
      - certbotChall:/var/www/certbot
    depends_on:
      - user-interface
    networks:
      - MIH-network
    profiles: [ 'prod' ]
  #============== Flutter App ====================================================================
  user-interface:
    container_name: MIH-UX
    build:
      context: ./Frontend
    ports:
      - "83:83"
    networks:
      - MIH-network
    depends_on:
      - api
  #============== Cert Not ==================================================================== 
  certbot:
    image: certbot/certbot
    container_name: certbot
    volumes:
      - certbotConf:/etc/letsencrypt
      - certbotChall:/var/www/certbot
    #command: certonly --test-cert --webroot -w /var/www/certbot --force-renewal --email yasienmeth@gmail.com -d mzansi-innovation-hub.co.za -d www.mzansi-innovation-hub.co.za --agree-tos
    command: certonly --webroot -w /var/www/certbot --force-renewal --email yasienmeth@gmail.com -d app.mzansi-innovation-hub.co.za -d api.mzansi-innovation-hub.co.za -d minio.mzansi-innovation-hub.co.za -d monitor.mzansi-innovation-hub.co.za -d ai.mzansi-innovation-hub.co.za --agree-tos
    networks:
      - MIH-network
    depends_on:
      - nginx
    profiles: [ 'withCert' ]
  #============== Minio File Storage ====================================================================  
  minio:
    platform: linux/amd64
    container_name: MIH-Minio
    hostname: minio
    image: docker.io/bitnami/minio:2022
    ports:
      - '9000:9000'
      - '9001:9001'
    volumes:
      - './File_Storage:/data'
    environment:
      MINIO_ROOT_USER: ${MINIO_ROOT_USER}
      MINIO_ROOT_PASSWORD: ${MINIO_ROOT_PW}
    networks:
      - MIH-network
  #============== Minio File Storage ==================================================================== 
  portainer:
    container_name: MIH-Monitor
    image: portainer/portainer-ce:2.20.3
    ports:
      - 9443:9443
    volumes:
      - data:/data
      - /var/run/docker.sock:/var/run/docker.sock
    restart: unless-stopped
    networks:
      - MIH-network
  #============== Minio File Storage ==================================================================== 
  ollama:
    container_name: MIH-AI
    image: ollama/ollama:latest
    ports:
      - 11434:11434
    volumes:
      # - ./Mzansi_AI:/code
      - ./Mzansi_AI/ollama/ollama:/root/.ollama
    pull_policy: always
    tty: true
    restart: always
    environment:
      - OLLAMA_ORIGINS=*
      - OLLAMA_KEEP_ALIVE=24h
      - OLLAMA_HOST=0.0.0.0
    networks:
      - MIH-network
  #============== Firebaase ====================================================================   
  # firebase:
  #   container_name: MIH-firebase-emulator
  #   build:
  #     context: ./Firebase-emulator
  #   environment:
  #     DATA_DIRECTORY: "firebase/data"
  #     FIREBASE_PROJECT: "mzansi-innovation-hub"
  #   ports:
  #     - 8082:8080 # FIRESTORE_PORT
  #     - 5005:5005 # FIRESTORE_WS_PORT
  #     - 4000:4000 # UI_PORT
  #     - 9099:9099 # AUTH_PORT
  #     - 5000:6001 # Hosting
  #   volumes:
  #     - ./Firebase-emulator:/srv/firebase:rw
  #     # - ./cache:/root/.cache/:rw
  #     # - ~/.config/:/root/.config
  #     - ./Firebase-emulator/firebase/data:/srv/firebase/data:rw
  #============== Named Volumes ====================================================================   
volumes:
  certbotConf:
  certbotChall:
  data: #============== MIH Network ====================================================================
networks:
  MIH-network:
    driver: bridge
