from diagrams import Cluster, Diagram, Edge

from diagrams.azure.database import DatabaseForMariadbServers

from diagrams.onprem.inmemory import Redis
from diagrams.onprem.network import Internet
from diagrams.onprem.groupware import Nextcloud
from diagrams.onprem.monitoring import Grafana, Prometheus
from diagrams.onprem.storage import Glusterfs
from diagrams.onprem.network import Haproxy
from diagrams.onprem.database import Mariadb

from diagrams.custom import Custom

with Diagram(filename="./assets/dia_nc_dev_improved", show=True, direction="TB"):
    out = Internet("Intranet")    

    with Cluster("Web & KeyDB Load Balancing"):
        vm_web_lb = Custom("", "../assets/vagrant.png")
        with Cluster("Node 1"):
            ha_2 = Haproxy("Master")
        with Cluster("Node 2"):
            ha_1 = Haproxy("Backup")    

    with Cluster("Shared persistent storage"):
        vm_nfs = Custom("", "../assets/vagrant.png")
        with Cluster("Node 2"):
            nfs_2 = Glusterfs()
        with Cluster("Node 1"):
            nfs_1 = Glusterfs()   

    with Cluster("Web server"):
        vm_web = Custom("", "../assets/vagrant.png")
        with Cluster("Node 2"):
            nc_frontend_2 = Nextcloud()
            keydb_2 = Custom("PHP Caching", "../assets/keydb.png")
        with Cluster("Node 1"):
            keydb_1 = Custom("PHP Caching", "../assets/keydb.png")
            nc_frontend_1 = Nextcloud()     

        nc_frontend_1 - Edge(style="dashed") - nc_frontend_2
        keydb_1 - Edge(color="firebrick", style="dashed", label=" Master - Master") - keydb_2
        ha_2 - Edge(color="firebrick", style="dashed") - ha_1 

    with Cluster("SQL Load Balancing"):
        vm_sql_lb = Custom("", "../assets/vagrant.png")
        with Cluster("Active"):
            ppSQL = Custom("", "../assets/proxysql.png")
        with Cluster("Passive"):
            paSQL = Custom("", "../assets/proxysql.png")           

    with Cluster("Galera cluster"):
        Custom("", "../assets/vagrant.png")        
        with Cluster("Primary writer"):
            primary = Mariadb()
        with Cluster("Backup writer 2"):
            bckp1 = DatabaseForMariadbServers()
        with Cluster("Backup writer 1"):
            bckp2 = DatabaseForMariadbServers()  
    
        paSQL - Edge(color="firebrick") - primary
        bckp1 - Edge(style="dashed") - paSQL - Edge(style="dashed") - bckp2
        
        ppSQL - Edge(color="firebrick") - primary
        bckp1 - Edge(style="dashed") - ppSQL - Edge(style="dashed") - bckp2
    
    out - vm_web_lb - vm_web
    vm_sql_lb - vm_web - vm_nfs