"""Architecture diagram for this kit. Run from the kit directory:

    python3 docs/diagram.py                                               # writes docs/architecture.html
    ../../shared/diagrams/render.sh docs/architecture.html docs/architecture.png

Coordinates are pixels. node(cx, cy, ...) is centred on the icon; lines must be orthogonal.
See shared/diagrams/README.md for the API and the icon names.
"""
import os, sys

DOCS = os.path.dirname(os.path.abspath(__file__))
sys.dont_write_bytecode = True
sys.path.insert(0, os.path.join(DOCS, "..", "..", "..", "shared", "diagrams"))
from lib import Diagram

W, H = 1500, 880
TITLE = "Amazon Ergo playground"
d = Diagram(W, H, TITLE)

TOP, MID, BOT = 215, 365, 515                 # static page row, sign-in row, API row
TRUNK, EDGE, IN, FN, AI, DEST = 200, 340, 620, 860, 1100, 1360   # columns

# ---------------- containers
d.box(240, 100, 1236, 580, "cloud", "AWS Cloud", "g-cloud")
d.box(480, 140, 740, 520, "region", "Region", "g-region", "us-east-1")
d.box(1250, 400, 206, 260, "region", "Region", "g-region", "set by profile")

# ---------------- nodes
d.node(90, MID, "user", ["Presenter"], ["web browser"], size=56)
d.node(EDGE, TOP, "cloudfront", ["Amazon CloudFront"], ["HTTPS endpoint"])
d.small(EDGE - 62, TOP - 82, "waf", ["AWS WAF web ACL"])
d.node(IN, TOP, "s3", ["Amazon S3"], ["static page, private bucket"])
d.node(IN, MID, "cognito", ["Amazon Cognito"], ["Midway sign-in, issues token"], pos="right")
d.node(IN, BOT, "apigw", ["Amazon API Gateway"], ["REST API, Cognito authorizer"])
d.small(IN - 62, BOT + 88, "waf", ["AWS WAF web ACL"])
d.node(FN, BOT, "lambda", ["AWS Lambda"], ["Node.js, response streaming"])
d.node(FN, MID, "cwlogs", ["Amazon CloudWatch Logs"], ["API access and function logs"], pos="right")
d.node(AI, BOT, "bedrock", ["Amazon Bedrock"], ["ConverseStream", ("mono", "global.amazon.ergo-1.0")])
d.node(DEST, BOT, "bedrock", ["Amazon Ergo-1.0"], ["model inference"])

# ---------------- connectors
d.line("load", [(122, MID), (TRUNK, MID), (TRUNK, TOP), (EDGE - 26, TOP)])
d.line("load", [(EDGE + 26, TOP), (IN - 26, TOP)])
d.line("acc", [(TRUNK, MID), (IN - 26, MID)])
d.line("load", [(TRUNK, MID), (TRUNK, BOT), (IN - 26, BOT)], dot=(TRUNK, MID))
d.line("load", [(IN + 26, BOT), (FN - 26, BOT)])
d.line("load", [(FN + 26, BOT), (AI - 26, BOT)])
d.line("load", [(AI + 26, BOT), (DEST - 26, BOT)])
d.line("tel", [(FN, BOT - 26), (FN, MID + 26)])
d.line("tel", [(IN + 14, BOT - 26), (IN + 14, 455), (FN, 455)], arrow=False, dot=(FN, 455))

d.badge(1, 270, TOP)
d.badge(2, 425, TOP)
d.badge(3, 400, MID)
d.badge(4, 400, BOT)
d.badge(5, 740, BOT)
d.badge(6, 980, BOT)
d.badge(7, 1175, BOT)
d.badge(8, FN, 425)

# ---------------- legend
d.lines_key(24, 715, [
    ("load", "Request and streamed answer", 190),
    ("acc", "Sign-in", 55),
    ("tel", "Logs", 40),
])
d.flow(24, 760, [[
    (1, "The presenter opens the playground through Amazon CloudFront, behind AWS WAF."),
    (2, "CloudFront serves the static page from a private Amazon S3 bucket."),
    (3, "The browser signs in through Amazon Cognito (Midway) and gets a token."),
    (4, "The browser sends the prompt and reasoning effort to API Gateway with the token."),
], [
    (5, "API Gateway checks the token and invokes the Lambda function."),
    (6, "Lambda calls ConverseStream on the global inference profile and streams the answer back."),
    (7, "The profile routes the request to a Region it selects (cross-Region inference)."),
    (8, "API Gateway and Lambda write logs to Amazon CloudWatch Logs."),
]], 700)

out = os.path.join(DOCS, "architecture.html")
open(out, "w").write(d.render(TITLE, "A static page streams answers from Amazon Ergo-1.0 on Amazon Bedrock and shows speed and cost for each request."))
print("wrote", os.path.relpath(out), W, H)
