import json
import os

files = {
    'zh_TW': '/home/mama/projects/star-cloud/lang/zh_TW.json',
    'en': '/home/mama/projects/star-cloud/lang/en.json',
    'ja': '/home/mama/projects/star-cloud/lang/ja.json'
}

new_keys = {
    "CREATE STOCK TRANSFERS BETWEEN WAREHOUSES AND MACHINE RETURNS": {
        "zh_TW": "建立倉庫間調撥或機台退庫單",
        "en": "Create stock transfers between warehouses and machine returns",
        "ja": "倉庫間の転送および機台からの返品伝票を作成"
    },
    "Create and manage replenishment orders from warehouse to machines": {
        "zh_TW": "建立並管理從倉庫到機台的補貨單",
        "en": "Create and manage replenishment orders from warehouse to machines",
        "ja": "倉庫から機台への補充伝票の作成と管理"
    },
    "Create stock transfers between warehouses and machine returns": {
        "zh_TW": "建立倉庫間調撥或機台退庫單",
        "en": "Create stock transfers between warehouses and machine returns",
        "ja": "倉庫間の転送および機台からの返品伝票を作成"
    },
    "Create Transfer Order": {
        "zh_TW": "建立調撥單",
        "en": "Create Transfer Order",
        "ja": "転送伝票を作成"
    },
    "Create Replenishment Order": {
        "zh_TW": "建立補貨單",
        "en": "Create Replenishment Order",
        "ja": "補充伝票を作成"
    },
    "New Transfer": {
        "zh_TW": "新增調撥單",
        "en": "New Transfer",
        "ja": "新規転送"
    },
    "New Replenishment": {
        "zh_TW": "新增補貨單",
        "en": "New Replenishment",
        "ja": "新規補充"
    },
    "New Stock-In Order": {
        "zh_TW": "新增進貨單",
        "en": "New Stock-In Order",
        "ja": "新規入庫伝票"
    },
    "No transfer orders found": {
        "zh_TW": "查無調撥單紀錄",
        "en": "No transfer orders found",
        "ja": "転送伝票が見つかりません"
    },
    "No replenishment orders found": {
        "zh_TW": "查無補貨單紀錄",
        "en": "No replenishment orders found",
        "ja": "補充伝票が見つかりません"
    },
    "No stock data found": {
        "zh_TW": "查無庫存資料",
        "en": "No stock data found",
        "ja": "在庫データが見つかりません"
    },
    "No stock-in orders found": {
        "zh_TW": "查無進貨單紀錄",
        "en": "No stock-in orders found",
        "ja": "入庫伝票が見つかりません"
    },
    "No movement records found": {
        "zh_TW": "查無異動紀錄",
        "en": "No movement records found",
        "ja": "移動履歴が見つかりません"
    },
    "Replenishment Orders": {
        "zh_TW": "機台補貨單",
        "en": "Replenishment Orders",
        "ja": "機台補充伝票"
    },
    "Search products...": {
        "zh_TW": "搜尋商品...",
        "en": "Search products...",
        "ja": "商品を検索..."
    },
    "Search Product": {
        "zh_TW": "搜尋商品",
        "en": "Search Product",
        "ja": "商品を検索"
    },
    "Select Warehouse": {
        "zh_TW": "選擇倉庫",
        "en": "Select Warehouse",
        "ja": "倉庫を選択"
    },
    "Select Machine": {
        "zh_TW": "選擇機台",
        "en": "Select Machine",
        "ja": "機台を選択"
    },
    "Select Product": {
        "zh_TW": "選擇商品",
        "en": "Select Product",
        "ja": "商品を選択"
    },
    "Source Warehouse": {
        "zh_TW": "來源倉庫",
        "en": "Source Warehouse",
        "ja": "発送元倉庫"
    },
    "Target Warehouse": {
        "zh_TW": "目標倉庫",
        "en": "Target Warehouse",
        "ja": "対象倉庫"
    },
    "Stock-In Orders": {
        "zh_TW": "進貨單管理",
        "en": "Stock-In Orders",
        "ja": "入庫伝票"
    },
    "Stock-In Management": {
        "zh_TW": "進貨管理",
        "en": "Stock-In Management",
        "ja": "入庫管理"
    },
    "Stock flow history": {
        "zh_TW": "庫存流向歷史",
        "en": "Stock flow history",
        "ja": "在庫移動履歴"
    },
    "Stock-In Orders Tab": {
        "zh_TW": "進貨單管理",
        "en": "Stock-In Orders",
        "ja": "入庫伝票"
    },
    "Transfer Orders": {
        "zh_TW": "調撥單管理",
        "en": "Transfer Orders",
        "ja": "転送伝票"
    },
    "Track stock-in orders and movement history": {
        "zh_TW": "追蹤進貨單據與庫存異動紀錄",
        "en": "Track stock-in orders and movement history",
        "ja": "入庫伝票および移動履歴の追跡"
    },
    "Warehouse to Warehouse": {
        "zh_TW": "倉庫對倉庫",
        "en": "Warehouse to Warehouse",
        "ja": "倉庫間転送"
    },
    "Warehouse Transfer": {
        "zh_TW": "倉庫調撥",
        "en": "Warehouse Transfer",
        "ja": "倉庫転送"
    },
    "Warehouse Management": {
        "zh_TW": "倉儲管理",
        "en": "Warehouse Management",
        "ja": "倉庫管理"
    },
    "Warehouse purchase records": {
        "zh_TW": "倉庫採購進貨紀錄",
        "en": "Warehouse purchase records",
        "ja": "倉庫購入記録"
    },
    "Machine to Warehouse": {
        "zh_TW": "機台對倉庫",
        "en": "Machine to Warehouse",
        "ja": "機台から倉庫"
    },
    "Machine Return": {
        "zh_TW": "機台退庫",
        "en": "Machine Return",
        "ja": "機台返品"
    },
    "Machine Replenishment": {
        "zh_TW": "機台補貨單",
        "en": "Machine Replenishment",
        "ja": "機台補充"
    },
    "Movement History": {
        "zh_TW": "異動紀錄",
        "en": "Movement History",
        "ja": "移動履歴"
    },
    "Movement Logs": {
        "zh_TW": "異動紀錄",
        "en": "Movement Logs",
        "ja": "移動ログ"
    },
    "Manage stock transfers between warehouses and machine returns": {
        "zh_TW": "建立倉庫間調撥或機台退庫單",
        "en": "Manage stock transfers between warehouses and machine returns",
        "ja": "倉庫間の転送および機台からの返品伝票を作成"
    },
    "Main": {
        "zh_TW": "總倉",
        "en": "Main",
        "ja": "総倉庫"
    },
    "Note": {
        "zh_TW": "備註",
        "en": "Note",
        "ja": "備考"
    },
    "Transfer Audit": {
        "zh_TW": "調撥單",
        "en": "Transfer Audit",
        "ja": "転送監査"
    },
    "Transfers": {
        "zh_TW": "調撥單",
        "en": "Transfers",
        "ja": "転送"
    },
    "Inventory Management": {
        "zh_TW": "庫存管理",
        "en": "Inventory Management",
        "ja": "在庫管理"
    },
    "Manage stock levels, stock-in orders, and movement history": {
        "zh_TW": "追蹤庫存水位、進貨單與異動紀錄",
        "en": "Manage stock levels, stock-in orders, and movement history",
        "ja": "在庫レベル、入庫伝票、および移動履歴の管理"
    },
    "Track stock levels, stock-in orders, and movement history": {
        "zh_TW": "追蹤庫存水位、進貨單與異動紀錄",
        "en": "Track stock levels, stock-in orders, and movement history",
        "ja": "在庫レベル、入庫伝票、および移動履歴の追跡"
    }
}

for lang, path in files.items():
    if not os.path.exists(path):
        continue
    
    with open(path, 'r', encoding='utf-8') as f:
        data = json.load(f)
    
    # Update menu.warehouses
    if lang == 'zh_TW':
        data['menu.warehouses'] = "倉儲管理"
    
    # Add/Update keys
    for k, v in new_keys.items():
        if lang in v:
            data[k] = v[lang]
        elif lang == 'en':
            data[k] = k

    # Sort keys and write back
    with open(path, 'w', encoding='utf-8') as f:
        json.dump(data, f, ensure_ascii=False, indent=4, sort_keys=True)
