import json, copy

EL = []
def rect(id, x, y, w, h, bg="transparent", stroke="#1e1e1e", sw=2, dashed=False):
    EL.append({"id": id, "type": "rectangle", "x": x, "y": y, "width": w, "height": h,
               "backgroundColor": bg, "fillStyle": "solid", "strokeColor": stroke, "strokeWidth": sw,
               "strokeStyle": "dashed" if dashed else "solid", "roundness": {"type": 3}})
def text(id, x, y, t, fs=16, color="#1e1e1e", container=None):
    e = {"id": id, "type": "text", "x": x, "y": y, "text": t, "fontSize": fs, "fontFamily": 1,
         "strokeColor": color, "originalText": t, "autoResize": True, "textAlign": "center", "verticalAlign": "middle"}
    if container:
        e["containerId"] = container
        EL[-1].setdefault("boundElements", []).append({"id": id, "type": "text"})
    EL.append(e)
def arrow(id, x, y, pts, color="#1e1e1e", dashed=False, label=None, lfs=16):
    e = {"id": id, "type": "arrow", "x": x, "y": y, "points": pts, "endArrowhead": "arrow",
         "strokeColor": color, "strokeWidth": 2, "strokeStyle": "dashed" if dashed else "solid"}
    if label:
        e["boundElements"] = [{"id": id + "_t", "type": "text"}]
    EL.append(e)
    if label:
        EL.append({"id": id + "_t", "type": "text", "x": x + pts[1][0]/2 - 60, "y": y + pts[1][1]/2 - 10,
                   "text": label, "fontSize": lfs, "fontFamily": 1, "strokeColor": color,
                   "originalText": label, "autoResize": True, "textAlign": "center", "verticalAlign": "middle",
                   "containerId": id})

# ===== canvas =====
text("t_title", 560, 16, "AIハーネス導入：2つの使い方", 28)
text("t_sub", 480, 56, "（モデル例・サービス名は一例です。御社の環境に合わせて調整します）", 14, "#757575")

# ---------- Mode A panel ----------
rect("zoneA", 60, 90, 680, 740, bg="#f1f3f5", stroke="#868e96", sw=1)
text("zA_label", 84, 104, "【使い方A】既存ツールをハーネス経由で統制（今までの使い方）", 17)
rect("rA_agents", 90, 170, 230, 150, bg="#a5d8ff")
text("rA_agents_t", 0, 0, "社内のAIエージェント\n（チャットAI・コーディング\n・自作エージェント）", 16, container="rA_agents")
arrow("aA1", 320, 250, [[0, 0], [100, 0]], label="全通信")
rect("rA_gw", 420, 170, 260, 160, bg="#d0bfff")
text("rA_gw_t", 0, 0, "AIハーネス（ゲートウェイ）\n認証・権限制御\nポリシーチェック\n全ログ記録", 16, container="rA_gw")
rect("gA_models", 90, 470, 330, 210, dashed=True, stroke="#868e96", sw=1)
text("gA_models_l", 110, 480, "LLMモデル", 16, "#757575")
rect("rA_local", 110, 530, 135, 90, bg="#b2f2bb")
text("rA_local_t", 0, 0, "ローカルLLM\n（GLM等）", 16, container="rA_local")
rect("rA_cloud", 265, 530, 140, 90, bg="#ffd8a8")
text("rA_cloud_t", 0, 0, "クラウドLLM\n（許可制）", 16, container="rA_cloud")
arrow("aA_local", 177, 525, [[0, 0], [310, -195]])
arrow("aA_cloud", 335, 525, [[0, 0], [230, -195]])
rect("rA_data", 530, 530, 190, 110, bg="#c3fae8")
text("rA_data_t", 0, 0, "社内データ\n（権限ベースで\nアクセス）", 16, container="rA_data")
arrow("aA_data", 590, 335, [[0, 0], [60, 190]])

# ---------- Mode B panel ----------
rect("zoneB", 760, 90, 700, 740, bg="#e9ecef", stroke="#868e96", sw=1)
text("zB_label", 784, 104, "【使い方B】ハーネス・チャットを直接利用（これからの使い方）", 17)
rect("rB_chat", 790, 170, 230, 150, bg="#a5d8ff")
text("rB_chat_t", 0, 0, "AIハーネス・チャット\n（ブラウザで即利用\n・クライアント設定不要）", 16, container="rB_chat")
arrow("aB1", 1020, 250, [[0, 0], [140, 0]], label="すべての質問・指示")
rect("rB_gw", 1160, 170, 250, 160, bg="#d0bfff")
text("rB_gw_t", 0, 0, "AIハーネス（ゲートウェイ）\n認証・権限制御\nポリシーチェック\n全ログ記録", 16, container="rB_gw")
rect("gB_models", 790, 470, 330, 210, dashed=True, stroke="#868e96", sw=1)
text("gB_models_l", 810, 480, "LLMモデル", 16, "#757575")
rect("rB_local", 810, 530, 135, 90, bg="#b2f2bb")
text("rB_local_t", 0, 0, "ローカルLLM\n（GLM等）", 16, container="rB_local")
rect("rB_cloud", 965, 530, 140, 90, bg="#ffd8a8")
text("rB_cloud_t", 0, 0, "クラウドLLM\n（許可制）", 16, container="rB_cloud")
arrow("aB_local", 877, 525, [[0, 0], [360, -195]])
arrow("aB_cloud", 1035, 525, [[0, 0], [260, -195]])
rect("rB_caps", 1160, 470, 250, 190, bg="#b2f2bb")
text("rB_caps_t", 0, 0, "できること\n・社内データRAG（検索）\n・ファイル操作\n・レポート自動生成\n・メール/カレンダー連携", 16, container="rB_caps")
arrow("aB_caps", 1285, 335, [[0, 0], [0, 130]])
rect("rB_data", 1160, 700, 250, 90, bg="#c3fae8")
text("rB_data_t", 0, 0, "社内データ（RAG・検索の素材）", 16, container="rB_data")
arrow("aB_data", 1285, 695, [[0, 0], [0, -30]])

# ---------- footer ----------
rect("footer", 60, 860, 1400, 60, bg="#fff3bf")
text("footer_t", 200, 880, "導入効果：トークン課金ゼロ ／ 全ログ記録で監査対応 ／ ノウハウ社内DBに資産化（ベンダーロック回避）", 18)

# ===== English mirror =====
translations = {
    "AIハーネス導入：2つの使い方": "AI Harness rollout: two ways to use it",
    "（モデル例・サービス名は一例です。御社の環境に合わせて調整します）": "(model & service names are examples — tailored to your environment)",
    "【使い方A】既存ツールをハーネス経由で統制（今までの使い方）": "[Mode A] Govern existing tools via the harness (today's usage)",
    "社内のAIエージェント\n（チャットAI・コーディング\n・自作エージェント）": "Internal AI agents\n(chat AI, coding agents,\ncustom agents)",
    "全通信": "all traffic",
    "AIハーネス（ゲートウェイ）\n認証・権限制御\nポリシーチェック\n全ログ記録": "AI Harness (Gateway)\nAuth & permissions\nPolicy checks\nFull audit log",
    "LLMモデル": "LLM models",
    "ローカルLLM\n（GLM等）": "Local LLM\n(GLM etc.)",
    "クラウドLLM\n（許可制）": "Cloud LLMs\n(allowlisted)",
    "社内データ\n（権限ベースで\nアクセス）": "Internal data\n(permission-based\naccess)",
    "【使い方B】ハーネス・チャットを直接利用（これからの使い方）": "[Mode B] Use the Harness Chat directly (the new way)",
    "AIハーネス・チャット\n（ブラウザで即利用\n・クライアント設定不要）": "AI Harness Chat\n(instant in browser,\nno client setup)",
    "すべての質問・指示": "all questions & tasks",
    "できること\n・社内データRAG（検索）\n・ファイル操作\n・レポート自動生成\n・メール/カレンダー連携": "What you can do\n• RAG on internal data (search)\n• File operations\n• Automated reports\n• Mail / calendar integration",
    "社内データ（RAG・検索の素材）": "Internal data (source for RAG & search)",
    "導入効果：トークン課金ゼロ ／ 全ログ記録で監査対応 ／ ノウハウ社内DBに資産化（ベンダーロック回避）":
        "Impact: zero token billing / full audit logs / know-how stays in your own DB (no vendor lock-in)",
}
x_nudge = {"t_title": 560, "t_sub": 430, "footer_t": 120}

clones = []
for e in EL:
    c = copy.deepcopy(e)
    c["id"] = e["id"] + "_en"
    c["y"] = e["y"] + 990
    if e["type"] == "text":
        src = e["originalText"]
        if src in translations:
            c["text"] = translations[src]
            c["originalText"] = translations[src]
        if e["id"] in x_nudge:
            c["x"] = x_nudge[e["id"]]
    if c.get("containerId"):
        c["containerId"] += "_en"
    if c.get("boundElements"):
        c["boundElements"] = [{**b, "id": b["id"] + "_en"} for b in c["boundElements"]]
    clones.append(c)

scene = {"type": "excalidraw", "version": 2, "source": "hermes-agent",
         "elements": EL + clones, "appState": {"viewBackgroundColor": "#ffffff"}}
json.dump(scene, open("/home/min/hermes_workspace/demo_review/ai_harness_final.excalidraw", "w"),
          ensure_ascii=False, indent=2)
print("elements:", len(scene["elements"]))