from diagrams import Cluster, Diagram, Edge
from diagrams.aws.compute import Lambda
from diagrams.aws.database import Dynamodb
from diagrams.aws.network import CloudFront
from diagrams.aws.security import Cognito
from diagrams.azure.identity import AzureActiveDirectory
from diagrams.onprem.client import Users

graph_attr = {
    "bgcolor": "transparent",
    "pad": "0.18",
    "rankdir": "LR",
    "splines": "ortho",
    "nodesep": "0.50",
    "ranksep": "0.95",
    "fontname": "Inter",
    "fontsize": "22",
    "labelloc": "t",
}

node_attr = {
    "fontname": "Inter",
    "fontsize": "11",
    "margin": "0.04,0.03",
    "imagescale": "true",
}

edge_attr = {
    "fontname": "Inter",
    "fontsize": "9",
    "color": "#4A5568",
    "fontcolor": "#4A5568",
    "arrowsize": "0.70",
    "penwidth": "1.8",
}

aws_attr = {
    "fillcolor": "#FFF6EE",
    "color": "#E0A86B",
    "fontcolor": "#B7651A",
    "fontsize": "14",
    "penwidth": "1.6",
    "style": "rounded,filled",
    "labeljust": "l",
    "labelloc": "t",
}

azure_attr = {
    "fillcolor": "#EAF1FB",
    "color": "#0078D4",
    "fontcolor": "#0F5BA8",
    "fontsize": "13",
    "penwidth": "1.6",
    "style": "rounded,filled",
    "labeljust": "l",
    "labelloc": "t",
}

with Diagram(
    "Python diagrams · Draw.io House Style",
    filename="/opt/diagram-tools-lab/site/assets/python-diagrams-house-style",
    direction="LR",
    curvestyle="ortho",
    outformat=["png", "svg"],
    show=False,
    graph_attr=graph_attr,
    node_attr=node_attr,
    edge_attr=edge_attr,
):
    user = Users("Mitarbeiter\nBrowser")

    with Cluster("AWS Cloud · eu-central-1", graph_attr=aws_attr):
        web = CloudFront("Web-Tools\nCloudFront + S3")
        cognito = Cognito("Cognito\nBroker / SP")
        trigger = Lambda("Pre-Token\nLambda")
        table = Dynamodb("DynamoDB\nGroups")

    with Cluster("Microsoft Azure · Entra ID Tenant", graph_attr=azure_attr):
        entra = AzureActiveDirectory("Entra ID\nIdP · MFA · CA")

    main = {"color": "#4A5568", "fontcolor": "#4A5568", "penwidth": "1.8"}
    saml = {"color": "#0F6CBD", "fontcolor": "#0F5BA8", "penwidth": "2.3"}
    success = {"color": "#1F7A52", "fontcolor": "#1F7A52", "penwidth": "2.3", "constraint": "false"}

    user >> Edge(label="1 HTTPS", **main) >> web
    web >> Edge(label="2 PKCE", **main) >> cognito
    cognito >> Edge(label="6 trigger", **main) >> trigger
    trigger >> Edge(label="7 groups", **main) >> table

    cognito >> Edge(label="3 AuthnRequest", **saml) >> entra
    entra >> Edge(label="5 Assertion", **saml) >> cognito
    cognito >> Edge(label="8 JWT", **success) >> web
