"""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 = 1370, 825
TITLE = "AWS IAM for agents: two agents, one role, two identities"
d = Diagram(W, H, TITLE)

IAM_Y, A, MID, B = 200, 395, 450, 505     # IAM row, agentA row, centre row, agentB row
AG, EP, S3, CT = 445, 760, 1010, 1215      # columns: agents, VPC endpoint, bucket, CloudTrail

# ---------------- containers
d.box(190, 95, 1155, 550, "cloud", "AWS Cloud", "g-cloud")
d.box(215, 130, 1105, 499, "region", "Region", "g-region", "us-east-1")
d.box(250, 265, 550, 348, "vpc", "VPC", "g-vpc")
d.box(270, 300, 350, 297, "subnet", "Private subnet", "g-private")
d.box(290, 335, 310, 248, "dgroup", "One shared IAM execution role")

# ---------------- nodes
d.node(80, MID, "laptop", ["Operator"], ["make demo"], size=56)
d.node(520, IAM_Y, "role", ["AWS IAM and AWS STS"], ["AgentCore attests each runtime,", "STS adds its identity to the role session"], pos="left")
d.node(AG, A, "agentcore", ["AgentCore runtime agentA"], [("mono", "identity .../runtime/agentA/...")])
d.node(AG, B, "agentcore", ["AgentCore runtime agentB"], [("mono", "identity .../runtime/agentB/...")])
d.node(EP, MID, "vpce", ["VPC endpoint"], ["for Amazon S3"], pos="left")
d.node(S3, MID, "s3-bucket", ["Amazon S3 bucket"], ["bucket policy conditions on", ("mono", "aws:OriginIdentity/Subject")])
d.node(CT, MID, "cloudtrail", ["AWS CloudTrail"], ["trail with S3 data events,", "log file validation"])

# ---------------- connectors
d.line("load", [(110, MID), (150, MID), (150, A), (AG - 26, A)])
d.line("load", [(150, MID), (150, B), (AG - 26, B)], dot=(150, MID))
d.line("acc", [(520, IAM_Y + 26), (520, 335)])
d.line("load", [(AG + 26, A), (EP, A), (EP, MID - 26)])
d.line("kill", [(AG + 26, B), (EP, B), (EP, MID + 26)])
d.line("load", [(EP + 26, MID - 9), (S3 - 28, MID - 9)])
d.line("kill", [(EP + 26, MID + 9), (S3 - 28, MID + 9)])
d.line("tel", [(546, IAM_Y), (CT, IAM_Y), (CT, MID - 28)])
d.line("tel", [(S3 + 28, MID), (CT - 28, MID)])

d.text(870, MID - 18, "agentA allowed", "edge", "middle")
d.text(870, MID + 30, "agentB AccessDenied", "edge", "middle")

d.badge(1, 130, MID)
d.badge(2, 520, 246)
d.badge(3, 680, A)
d.badge(4, 680, B)
d.badge(5, 880, IAM_Y)
d.badge(6, 1112, MID)

# ---------------- legend
d.lines_key(24, 680, [
    ("load", "Request", 60),
    ("kill", "Denied by the bucket policy", 185),
    ("acc", "Identity issued", 100),
    ("tel", "Audit events", 85),
])
d.flow(24, 725, [[
    (1, "The operator invokes agentA and agentB with the same request."),
    (2, "Each runtime gets its own identity on its session of the shared role."),
    (3, "agentA reads the object: its identity matches the bucket policy condition."),
], [
    (4, "agentB gets AccessDenied: same role, different identity."),
    (5, "CloudTrail records which runtime got which role session (GetIAMWorkloadIdentityCredentials)."),
    (6, "The trail also records the S3 data events for both calls."),
]], 650)

out = os.path.join(DOCS, "architecture.html")
open(out, "w").write(d.render(TITLE, "Two Amazon Bedrock AgentCore runtimes share one IAM execution role. An S3 bucket policy tells them apart by workload identity (preview)."))
print("wrote", os.path.relpath(out), W, H)
