from __future__ import annotations

import tkinter as tk

from app_core.permissions import EDITABLE_MODULE_KEYS
from app_core.version import format_versioned_app_title
from app_modules.bom_module import BOM_FILTER_FIELD_OPTIONS, BOM_SORT_KEY_OPTIONS
from app_modules.costing_list_module import COSTING_FILTER_FIELD_OPTIONS, COSTING_SORT_KEY_OPTIONS
from app_modules.po_module import PO_FILTER_FIELD_OPTIONS, PO_SORT_KEY_OPTIONS
from app_modules.samples_module import SAMPLES_FILTER_FIELD_OPTIONS, SAMPLES_SORT_KEY_OPTIONS
from app_modules.tech_pack_list_module import TECH_PACK_FILTER_FIELD_OPTIONS, TECH_PACK_SORT_KEY_OPTIONS


MANAGED_MODULE_KEYS = list(EDITABLE_MODULE_KEYS)

PERMISSION_ACTION_CATALOG = [
    ("action.purchase_order.import_pdf", "permission_action_import_pdf"),
    ("action.purchase_order.import_folder", "permission_action_import_folder"),
    ("action.purchase_order.export_report", "permission_action_export_report"),
    ("action.purchase_order.delete_po", "permission_action_delete_po"),
    ("action.purchase_order.edit_po", "permission_action_edit_po"),
    ("action.purchase_order.compare_po", "permission_action_compare_po"),
    ("action.purchase_order.approve", "permission_action_purchase_order_approve"),
    ("action.costing.import_pdf", "permission_action_costing_import_pdf"),
    ("action.costing.import_ws", "permission_action_costing_import_ws"),
    ("action.costing.import_folder", "permission_action_costing_import_folder"),
    ("action.costing.export_report", "permission_action_costing_export_report"),
    ("action.costing.adjust_crop", "permission_action_costing_adjust_crop"),
    ("action.costing.delete_style", "permission_action_costing_delete_style"),
    ("action.costing.edit_row", "permission_action_costing_edit_row"),
    ("action.costing.collection_list", "permission_action_costing_collection_list"),
    ("action.costing.approve", "permission_action_costing_approve"),
    ("action.production.edit", "permission_action_production_edit"),
    ("action.production.delete", "permission_action_production_delete"),
    ("action.production.export_report", "permission_action_production_export_report"),
    ("action.production.packing_list", "permission_action_production_packing_list"),
    ("action.production.factory_list", "permission_action_production_factory_list"),
    ("action.tool.report_builder", "permission_action_tool_report_builder"),
    ("action.tool.download_tech_pack", "permission_action_tool_download_tech_pack"),
    ("action.bom.new", "permission_action_bom_new"),
    ("action.bom.copy", "permission_action_bom_copy"),
    ("action.bom.edit", "permission_action_bom_edit"),
    ("action.bom.delete", "permission_action_bom_delete"),
    ("action.bom.export_report", "permission_action_bom_export_report"),
    ("action.bom.generate_from_tech_pack", "permission_action_bom_generate_from_tech_pack"),
    ("action.bom.import_fds", "permission_action_bom_import_fds"),
    ("action.bom.approve", "permission_action_bom_approve"),
    ("action.bom.supplier_list", "permission_action_bom_supplier_list"),
    ("action.bom.supplier_add", "permission_action_bom_supplier_add"),
    ("action.bom.supplier_edit", "permission_action_bom_supplier_edit"),
    ("action.bom.supplier_delete", "permission_action_bom_supplier_delete"),
    ("action.tech_pack.import_pdf", "permission_action_tech_pack_import_pdf"),
    ("action.tech_pack.import_folder", "permission_action_tech_pack_import_folder"),
    ("action.tech_pack.export_report", "permission_action_tech_pack_export_report"),
    ("action.tech_pack.delete_style", "permission_action_tech_pack_delete_style"),
    ("action.tech_pack.edit_style", "permission_action_tech_pack_edit_style"),
    ("action.tech_pack.detail_edit", "permission_action_tech_pack_detail_edit"),
    ("action.tech_pack.detail_save", "permission_action_tech_pack_detail_save"),
    ("action.consumption.approve", "permission_action_consumption_approve"),
    ("action.samples.new", "permission_action_samples_new"),
    ("action.samples.copy", "permission_action_samples_copy"),
    ("action.samples.import_report", "permission_action_samples_import_report"),
    ("action.samples.export_report", "permission_action_samples_export_report"),
    ("action.samples.delete", "permission_action_samples_delete"),
    ("action.samples.edit", "permission_action_samples_edit"),
    ("action.hr.manage_shared_lists", "permission_action_hr_manage_shared_lists"),
    ("action.hr.employee.new", "permission_action_hr_employee_new"),
    ("action.hr.employee.edit", "permission_action_hr_employee_edit"),
    ("action.hr.employee.delete", "permission_action_hr_employee_delete"),
    ("action.hr.employee.toggle_active", "permission_action_hr_employee_toggle_active"),
    ("action.hr.attendance.new", "permission_action_hr_attendance_new"),
    ("action.hr.attendance.delete", "permission_action_hr_attendance_delete"),
    ("action.hr.attendance.quick", "permission_action_hr_attendance_quick"),
    ("action.hr.attendance.overview", "permission_action_hr_attendance_overview"),
    ("action.hr.attendance.check_in", "permission_action_hr_attendance_check_in"),
    ("action.hr.attendance.check_out", "permission_action_hr_attendance_check_out"),
    ("action.hr.attendance.edit", "permission_action_hr_attendance_edit"),
    ("action.hr.export_report", "permission_action_hr_export_report"),
    ("action.setting.save_online_setup", "permission_action_setting_save"),
    ("action.setting.test_connection", "permission_action_setting_test"),
    ("action.setting.check_updates", "permission_action_setting_updates"),
    ("action.setting.save_mail_settings", "permission_action_setting_save_mail"),
    ("action.setting.send_test_email", "permission_action_setting_send_test_mail"),
]

SORT_ORDER_OPTIONS = [("asc", "ascending"), ("desc", "descending")]


def update_translations(self) -> None:
    self.root.title(format_versioned_app_title(self.t("app_title")))
    self.widget_text["language_label"].configure(text=self.t("language") + ":")
    if "current_user_label" in self.widget_text:
        self.widget_text["current_user_label"].configure(text=self.t("current_user") + ":")
    if "refresh_button" in self.widget_text:
        refresh_button_text = self.t("refresh_button")
        if refresh_button_text == "refresh_button":
            refresh_button_text = f"{self.t('refresh')}(F5)"
        self.widget_text["refresh_button"].configure(text=refresh_button_text)
    if "app_latency_label" in self.widget_text:
        self.widget_text["app_latency_label"].configure(text=self.t("app_latency_label") + ":")
    if "server_latency_label" in self.widget_text:
        self.widget_text["server_latency_label"].configure(text=self.t("server_latency_label") + ":")
    if "logout_button" in self.widget_text:
        self.widget_text["logout_button"].configure(text=self.t("logout_button"))
    self.widget_text["menu_section"].configure(text=self.t("menu_section"))
    for module_key, button in self.module_buttons.items():
        button.configure(text=self.t(f"menu_{module_key}"))
    if "tool_section_title" in self.widget_text:
        self.widget_text["tool_section_title"].configure(text=self.t("tool_section"))
    if "tool_home_hint" in self.widget_text:
        self.widget_text["tool_home_hint"].configure(text=self.t("tool_home_hint"))
    if "selected_sources" in self.widget_text:
        self.widget_text["selected_sources"].configure(text=self.t("selected_sources"))
    if "tool_report_builder_button" in self.widget_text:
        self.widget_text["tool_report_builder_button"].configure(text=self.t("tool_report_builder_open"))
    if "tool_download_tech_pack_button" in self.widget_text:
        self.widget_text["tool_download_tech_pack_button"].configure(text=self.t("tool_download_tech_pack_open"))
    if "tool_back_home_button" in self.widget_text:
        self.widget_text["tool_back_home_button"].configure(text=self.t("tool_back_home"))
    if "history_section_title" in self.widget_text:
        self.widget_text["history_section_title"].configure(text=self.t("history_section_title"))
    if "history_section_subtitle" in self.widget_text:
        self.widget_text["history_section_subtitle"].configure(text=self.t("history_section_subtitle"))
    if "history_scope_label" in self.widget_text:
        self.widget_text["history_scope_label"].configure(text=self.t("history_scope_label"))
    if "history_user_label" in self.widget_text:
        self.widget_text["history_user_label"].configure(text=self.t("history_user_label"))
    if "history_keyword_label" in self.widget_text:
        self.widget_text["history_keyword_label"].configure(text=self.t("history_keyword_label"))
    if "history_date_from_label" in self.widget_text:
        self.widget_text["history_date_from_label"].configure(text=self.t("history_date_from_label"))
    if "history_date_to_label" in self.widget_text:
        self.widget_text["history_date_to_label"].configure(text=self.t("history_date_to_label"))
    if "history_refresh_button" in self.widget_text:
        self.widget_text["history_refresh_button"].configure(text=self.t("history_refresh_button"))
    if hasattr(self, "history_scope_combo"):
        selected_label = self.history_scope_var.get().strip()
        selected_key = dict(getattr(self, "history_scope_options_by_label", {})).get(selected_label, "__all__")
        self.history_scope_options_by_label = {
            self.translate_history_scope(key): key
            for key, _text_key in getattr(self, "HISTORY_SCOPE_OPTIONS", [("__all__", "history_filter_all_modules")])
        }
        self.history_scope_combo.configure(values=list(self.history_scope_options_by_label))
        reverse_scope_map = {value: key for key, value in self.history_scope_options_by_label.items()}
        self.history_scope_var.set(reverse_scope_map.get(selected_key, self.t("history_filter_all_modules")))
    if hasattr(self, "history_tree"):
        for column_key in ("changed_at", "scope_label", "entity_label", "action_label", "user_display_name", "summary"):
            self.history_tree.heading(column_key, text=self.t(f"history_column_{column_key}"))
    if "tool_download_tech_pack_back_button" in self.widget_text:
        self.widget_text["tool_download_tech_pack_back_button"].configure(text=self.t("tool_back_home"))
    if "download_tech_pack_title" in self.widget_text:
        self.widget_text["download_tech_pack_title"].configure(text=self.t("download_tech_pack_title"))
    if "download_tech_pack_subtitle" in self.widget_text:
        self.widget_text["download_tech_pack_subtitle"].configure(text=self.t("download_tech_pack_subtitle"))
    if "download_tech_pack_user_id_label" in self.widget_text:
        self.widget_text["download_tech_pack_user_id_label"].configure(text=self.t("download_tech_pack_user_id"))
    if "download_tech_pack_password_label" in self.widget_text:
        self.widget_text["download_tech_pack_password_label"].configure(text=self.t("download_tech_pack_password"))
    if "download_tech_pack_show_password" in self.widget_text:
        self.widget_text["download_tech_pack_show_password"].configure(text=self.t("download_tech_pack_show_password"))
    if "download_tech_pack_style_list_label" in self.widget_text:
        self.widget_text["download_tech_pack_style_list_label"].configure(text=self.t("download_tech_pack_style_list"))
    if "download_tech_pack_style_hint" in self.widget_text:
        self.widget_text["download_tech_pack_style_hint"].configure(text=self.t("download_tech_pack_style_hint"))
    if "download_tech_pack_download_folder_label" in self.widget_text:
        self.widget_text["download_tech_pack_download_folder_label"].configure(
            text=self.t("download_tech_pack_download_folder")
        )
    if "download_tech_pack_download_folder_hint" in self.widget_text:
        self.widget_text["download_tech_pack_download_folder_hint"].configure(
            text=self.t("download_tech_pack_download_folder_hint")
        )
    if "download_tech_pack_browse_folder" in self.widget_text:
        self.widget_text["download_tech_pack_browse_folder"].configure(text=self.t("download_tech_pack_browse_folder"))
    if "download_tech_pack_headless" in self.widget_text:
        self.widget_text["download_tech_pack_headless"].configure(text=self.t("download_tech_pack_headless"))
    if "download_tech_pack_debug" in self.widget_text:
        self.widget_text["download_tech_pack_debug"].configure(text=self.t("download_tech_pack_debug"))
    if "download_tech_pack_pattern_zip" in self.widget_text:
        self.widget_text["download_tech_pack_pattern_zip"].configure(text=self.t("download_tech_pack_pattern_zip"))
    if "download_tech_pack_comment" in self.widget_text:
        self.widget_text["download_tech_pack_comment"].configure(text=self.t("download_tech_pack_comment"))
    if "download_tech_pack_start_button" in self.widget_text:
        start_key = "download_tech_pack_stop" if self.download_tech_pack_running else "download_tech_pack_start"
        self.widget_text["download_tech_pack_start_button"].configure(text=self.t(start_key))
    if "download_tech_pack_checkpoint_button" in self.widget_text:
        self.widget_text["download_tech_pack_checkpoint_button"].configure(text=self.t("download_tech_pack_save_checkpoint"))
    if "download_tech_pack_log_title" in self.widget_text:
        self.widget_text["download_tech_pack_log_title"].configure(text=self.t("download_tech_pack_log"))
    if "download_tech_pack_log_hint" in self.widget_text:
        self.widget_text["download_tech_pack_log_hint"].configure(text=self.t("download_tech_pack_log_hint"))
    if "online_setup_title" in self.widget_text:
        self.widget_text["online_setup_title"].configure(text=self.t("online_setup_title"))
    if "online_setup_subtitle" in self.widget_text:
        self.widget_text["online_setup_subtitle"].configure(text=self.t("online_setup_subtitle"))
    if "online_setup_data_mode" in self.widget_text:
        self.widget_text["online_setup_data_mode"].configure(text=self.t("online_setup_data_mode"))
    if hasattr(self, "online_setup_local_mode_radio"):
        self.online_setup_local_mode_radio.configure(text=self.t("online_setup_mode_local"))
    if hasattr(self, "online_setup_online_mode_radio"):
        self.online_setup_online_mode_radio.configure(text=self.t("online_setup_mode_online"))
    if hasattr(self, "online_setup_mode_hint_label"):
        hint_key = "online_setup_local_hint" if self.is_local_data_mode() else "online_setup_online_hint"
        self.online_setup_mode_hint_label.configure(text=self.t(hint_key))
    if "online_setup_api_base_url" in self.widget_text:
        self.widget_text["online_setup_api_base_url"].configure(text=self.t("online_setup_api_base_url"))
    if "online_setup_shared_files_base_url" in self.widget_text:
        self.widget_text["online_setup_shared_files_base_url"].configure(text=self.t("online_setup_shared_files_base_url"))
    if "online_setup_healthcheck_path" in self.widget_text:
        self.widget_text["online_setup_healthcheck_path"].configure(text=self.t("online_setup_healthcheck_path"))
    if "online_setup_update_manifest_url" in self.widget_text:
        self.widget_text["online_setup_update_manifest_url"].configure(text=self.t("online_setup_update_manifest_url"))
    if "online_setup_device_name" in self.widget_text:
        self.widget_text["online_setup_device_name"].configure(text=self.t("online_setup_device_name"))
    if "online_setup_request_timeout" in self.widget_text:
        self.widget_text["online_setup_request_timeout"].configure(text=self.t("online_setup_request_timeout"))
    if "online_setup_device_id" in self.widget_text:
        self.widget_text["online_setup_device_id"].configure(text=self.t("online_setup_device_id"))
    if "online_setup_runtime_info" in self.widget_text:
        self.widget_text["online_setup_runtime_info"].configure(text=self.t("online_setup_runtime_info"))
    if "online_setup_auto_check_updates" in self.widget_text:
        self.widget_text["online_setup_auto_check_updates"].configure(text=self.t("online_setup_auto_check_updates"))
    if "online_setup_save_button" in self.widget_text:
        self.widget_text["online_setup_save_button"].configure(text=self.t("online_setup_save_button"))
    if "online_setup_test_button" in self.widget_text:
        self.widget_text["online_setup_test_button"].configure(text=self.t("online_setup_test_button"))
    if "online_setup_check_updates_button" in self.widget_text:
        self.widget_text["online_setup_check_updates_button"].configure(text=self.t("online_setup_check_updates_button"))
    if "online_setup_status_label" in self.widget_text:
        self.widget_text["online_setup_status_label"].configure(text=self.t("online_setup_status_label"))
    for key in (
        "mail_settings_title",
        "mail_settings_subtitle",
        "mail_settings_delivery_mode",
        "mail_settings_host",
        "mail_settings_port",
        "mail_settings_username",
        "mail_settings_password",
        "mail_settings_from",
        "mail_settings_reset_base_url",
        "mail_settings_local_outbox",
        "mail_settings_test_recipient",
    ):
        if key in self.widget_text:
            self.widget_text[key].configure(text=self.t(key))
    if "mail_settings_use_tls" in self.widget_text:
        self.widget_text["mail_settings_use_tls"].configure(text=self.t("mail_settings_use_tls"))
    if "mail_settings_save_button" in self.widget_text:
        self.widget_text["mail_settings_save_button"].configure(text=self.t("mail_settings_save_button"))
    if "mail_settings_send_test_button" in self.widget_text:
        self.widget_text["mail_settings_send_test_button"].configure(text=self.t("mail_settings_send_test_button"))
    if "shared_lists_title" in self.widget_text:
        self.widget_text["shared_lists_title"].configure(text=self.t("shared_lists_title"))
    if "shared_lists_subtitle" in self.widget_text:
        self.widget_text["shared_lists_subtitle"].configure(text=self.t("shared_lists_subtitle"))
    if "shared_lists_open_button" in self.widget_text:
        self.widget_text["shared_lists_open_button"].configure(text=self.t("shared_lists_open_button"))
    if "review_po_selection_button" in self.widget_text:
        self.widget_text["review_po_selection_button"].configure(text=self.t("review_po_selection"))
    if "review_costing_selection_button" in self.widget_text:
        self.widget_text["review_costing_selection_button"].configure(text=self.t("review_costing_selection"))
    if "admin_access_summary" in self.widget_text:
        self.widget_text["admin_access_summary"].configure(text=self.t("role_access_summary"))
    if "admin_user_list" in self.widget_text:
        self.widget_text["admin_user_list"].configure(text=self.t("role_user_list"))
    if "admin_folder_explorer_title" in self.widget_text:
        self.widget_text["admin_folder_explorer_title"].configure(text=self.t("admin_folder_explorer_title"))
    if "admin_folder_explorer_subtitle" in self.widget_text:
        self.widget_text["admin_folder_explorer_subtitle"].configure(text=self.t("admin_folder_explorer_subtitle"))
    if "admin_folder_explorer_open_button" in self.widget_text:
        self.widget_text["admin_folder_explorer_open_button"].configure(text=self.t("admin_folder_explorer_open_button"))
    if "moderate_user_list" in self.widget_text:
        self.widget_text["moderate_user_list"].configure(text=self.t("role_user_list"))
    if "moderate_access_summary" in self.widget_text:
        self.widget_text["moderate_access_summary"].configure(text=self.t("role_access_summary"))
    if "user_access_summary" in self.widget_text:
        self.widget_text["user_access_summary"].configure(text=self.t("role_access_summary"))
    for manager_key in ("admin", "moderate"):
        for suffix, text_key in (
            ("account_delete_button", "account_delete_selected"),
            ("account_reload_button", "account_reload"),
            ("account_select_header", "account_select"),
            ("account_column_email_header", "account_column_email"),
            ("account_column_display_name_header", "account_column_display_name"),
            ("account_column_role_header", "account_column_role"),
            ("account_column_approval_header", "account_column_approval"),
            ("account_column_active_header", "account_column_active"),
            ("account_column_edit_header", "account_column_edit"),
        ):
            widget_key = f"{manager_key}_{suffix}"
            if widget_key in self.widget_text:
                self.widget_text[widget_key].configure(text=self.t(text_key))
    for editor_key in ("admin", "moderate"):
        if f"{editor_key}_permission_target_account" in self.widget_text:
            self.widget_text[f"{editor_key}_permission_target_account"].configure(text=self.t("permission_target_account") + ":")
        if f"{editor_key}_permission_assign_role" in self.widget_text:
            self.widget_text[f"{editor_key}_permission_assign_role"].configure(text=self.t("permission_assign_role") + ":")
        if f"{editor_key}_permission_save_button" in self.widget_text:
            self.widget_text[f"{editor_key}_permission_save_button"].configure(text=self.t("permission_save_button"))
        if f"{editor_key}_permission_reload_button" in self.widget_text:
            self.widget_text[f"{editor_key}_permission_reload_button"].configure(text=self.t("permission_reload_button"))
        context = self.permission_editor_contexts.get(editor_key, {})
        permissions_frame = context.get("permissions_frame")
        if permissions_frame is not None:
            permissions_frame.configure(text=self.t("permission_modules_section"))
        role_combo = context.get("role_combo")
        target_roles = ["admin", "moderate", "user"] if editor_key == "admin" else ["user"]
        if role_combo is not None:
            role_combo.configure(values=[self.permission_role_label(role) for role in target_roles])
        role_var = context.get("role_var")
        if isinstance(role_var, tk.StringVar):
            normalized_role = self.permission_role_from_label(role_var.get())
            role_var.set(self.permission_role_label(normalized_role))
        for module_key in MANAGED_MODULE_KEYS:
            module_frame = dict(context.get("module_frames", {})).get(module_key)
            if module_frame is not None:
                module_frame.configure(text=self.t(f"menu_{module_key}"))
            widget_key = f"{editor_key}_module_permission_{module_key}"
            if widget_key in self.widget_text:
                self.widget_text[widget_key].configure(text=self.t("permission_enable_module"))
        for action_key, text_key in PERMISSION_ACTION_CATALOG:
            widget_key = f"{editor_key}_action_permission_{action_key}"
            if widget_key in self.widget_text:
                self.widget_text[widget_key].configure(text=self.t(text_key))
        self.reload_permission_editor(editor_key)
    for manager_key, context in self.user_management_contexts.items():
        for row in list(context.get("rows", [])):
            edit_button = row.get("edit_button")
            if edit_button is not None:
                edit_button.configure(text=self.t("account_edit"))
    if "saved_templates" in self.widget_text:
        self.widget_text["saved_templates"].configure(text=self.t("saved_templates"))
    self.widget_text["import_pdf_button"].configure(text=self.t("import_pdf"))
    self.widget_text["import_folder_button"].configure(text=self.t("import_folder"))
    self.widget_text["export_report_toolbar_button"].configure(text=self.t("export_report_toolbar"))
    self.widget_text["delete_po_button"].configure(text=self.t("delete_po"))
    self.widget_text["edit_po_button"].configure(text=self.t("edit"))
    self.widget_text["compare_po_button"].configure(text=self.t("compare_po"))
    if "quick_po_status_button" in self.widget_text:
        self.widget_text["quick_po_status_button"].configure(text=self.t("quick_po_status"))
    self.widget_text["costing_import_button"].configure(text=self.t("costing_import_pdf"))
    self.widget_text["costing_import_ws_button"].configure(text=self.t("costing_import_ws"))
    self.widget_text["costing_import_folder_button"].configure(text=self.t("costing_import_folder"))
    self.widget_text["costing_export_report_button"].configure(text=self.t("costing_export_report"))
    self.widget_text["costing_adjust_button"].configure(text=self.t("costing_adjust_crop"))
    self.widget_text["costing_delete_button"].configure(text=self.t("costing_delete_style"))
    self.widget_text["costing_edit_button"].configure(text=self.t("edit"))
    self.widget_text["costing_collection_list_button"].configure(text=self.t("costing_collection_list"))
    if "costing_form_button" in self.widget_text:
        self.widget_text["costing_form_button"].configure(text="Costing form")
    if "tech_pack_import_button" in self.widget_text:
        self.widget_text["tech_pack_import_button"].configure(text=self.t("import_pdf"))
    if "tech_pack_import_folder_button" in self.widget_text:
        self.widget_text["tech_pack_import_folder_button"].configure(text=self.t("import_folder"))
    if "tech_pack_export_button" in self.widget_text:
        self.widget_text["tech_pack_export_button"].configure(text=self.t("export_report_toolbar"))
    if "tech_pack_add_style_button" in self.widget_text:
        self.widget_text["tech_pack_add_style_button"].configure(text="Add style")
    if "tech_pack_delete_button" in self.widget_text:
        self.widget_text["tech_pack_delete_button"].configure(text=self.t("delete_style"))
    if "tech_pack_edit_button" in self.widget_text:
        self.widget_text["tech_pack_edit_button"].configure(text=self.t("edit"))
    if "tech_pack_sync_button" in self.widget_text:
        sync_text_key = "tech_pack_sync_button_running" if getattr(self, "sync_tech_pack_running", False) else "tech_pack_sync_button"
        self.widget_text["tech_pack_sync_button"].configure(text=self.t(sync_text_key))
    if "samples_sync_tech_pack_button" in self.widget_text:
        sync_text_key = "tech_pack_sync_button_running" if getattr(self, "sync_tech_pack_running", False) else "tech_pack_sync_button"
        self.widget_text["samples_sync_tech_pack_button"].configure(text=self.t(sync_text_key))
    if "tech_pack_search_image_button" in self.widget_text:
        self.widget_text["tech_pack_search_image_button"].configure(text=self.get_tech_pack_image_search_text("button"))
    if "po_filter_field_label" in self.widget_text:
        self.widget_text["po_filter_field_label"].configure(text=self.t("samples_filter_field_label"))
    if "po_filter_value_label" in self.widget_text:
        self.widget_text["po_filter_value_label"].configure(text=self.t("samples_filter_value_label"))
    if "costing_filter_field_label" in self.widget_text:
        self.widget_text["costing_filter_field_label"].configure(text=self.t("samples_filter_field_label"))
    if "costing_filter_value_label" in self.widget_text:
        self.widget_text["costing_filter_value_label"].configure(text=self.t("samples_filter_value_label"))
    if "tech_pack_filter_field_label" in self.widget_text:
        self.widget_text["tech_pack_filter_field_label"].configure(text=self.t("samples_filter_field_label"))
    if "tech_pack_filter_value_label" in self.widget_text:
        self.widget_text["tech_pack_filter_value_label"].configure(text=self.t("samples_filter_value_label"))
    if "po_date_from_label" in self.widget_text:
        self.widget_text["po_date_from_label"].configure(text=self.t("date_from_label"))
    if "po_date_to_label" in self.widget_text:
        self.widget_text["po_date_to_label"].configure(text=self.t("date_to_label"))
    if "costing_date_from_label" in self.widget_text:
        self.widget_text["costing_date_from_label"].configure(text=self.t("date_from_label"))
    if "costing_date_to_label" in self.widget_text:
        self.widget_text["costing_date_to_label"].configure(text=self.t("date_to_label"))
    if "tech_pack_date_from_label" in self.widget_text:
        self.widget_text["tech_pack_date_from_label"].configure(text=self.t("date_from_label"))
    if "tech_pack_date_to_label" in self.widget_text:
        self.widget_text["tech_pack_date_to_label"].configure(text=self.t("date_to_label"))
    if "tech_pack_sort_by_label" in self.widget_text:
        self.widget_text["tech_pack_sort_by_label"].configure(text=self.t("sort_by_label"))
    if "tech_pack_sort_order_label" in self.widget_text:
        self.widget_text["tech_pack_sort_order_label"].configure(text=self.t("sort_order_label"))
    if "tech_pack_styles_section" in self.widget_text:
        self.widget_text["tech_pack_styles_section"].configure(text=self.t("tech_pack_styles_section"))
    if "samples_import_button" in self.widget_text:
        self.widget_text["samples_import_button"].configure(text=self.t("new_button"))
    if "samples_copy_button" in self.widget_text:
        self.widget_text["samples_copy_button"].configure(text="Copy")
    if "samples_import_folder_button" in self.widget_text:
        self.widget_text["samples_import_folder_button"].configure(text=self.t("samples_import_report_button"))
    if "samples_export_button" in self.widget_text:
        self.widget_text["samples_export_button"].configure(text=self.t("export_report_toolbar"))
    if "samples_delete_button" in self.widget_text:
        self.widget_text["samples_delete_button"].configure(text="Delete Sample")
    if "samples_edit_button" in self.widget_text:
        self.widget_text["samples_edit_button"].configure(text=self.t("edit"))
    if "samples_filter_field_label" in self.widget_text:
        self.widget_text["samples_filter_field_label"].configure(text=self.t("samples_filter_field_label"))
    if "samples_filter_value_label" in self.widget_text:
        self.widget_text["samples_filter_value_label"].configure(text=self.t("samples_filter_value_label"))
    if "samples_date_from_label" in self.widget_text:
        self.widget_text["samples_date_from_label"].configure(text=self.t("date_from_label"))
    if "samples_date_to_label" in self.widget_text:
        self.widget_text["samples_date_to_label"].configure(text=self.t("date_to_label"))
    if "samples_sort_by_label" in self.widget_text:
        self.widget_text["samples_sort_by_label"].configure(text=self.t("sort_by_label"))
    if "samples_sort_order_label" in self.widget_text:
        self.widget_text["samples_sort_order_label"].configure(text=self.t("sort_order_label"))
    if "samples_styles_section" in self.widget_text:
        self.widget_text["samples_styles_section"].configure(text=self.t("samples_styles_section"))
    if "bom_new_button" in self.widget_text:
        self.widget_text["bom_new_button"].configure(text=self.t("new_button"))
    if "bom_copy_button" in self.widget_text:
        self.widget_text["bom_copy_button"].configure(text=self.t("copy_button"))
    if "bom_edit_button" in self.widget_text:
        self.widget_text["bom_edit_button"].configure(text=self.t("edit"))
    if "bom_delete_button" in self.widget_text:
        self.widget_text["bom_delete_button"].configure(text=self.t("delete_button"))
    if "bom_export_button" in self.widget_text:
        self.widget_text["bom_export_button"].configure(text=self.t("export_report_toolbar"))
    if "bom_generate_button" in self.widget_text:
        self.widget_text["bom_generate_button"].configure(text=self.t("bom_generate_button"))
    if "bom_import_fds_button" in self.widget_text:
        self.widget_text["bom_import_fds_button"].configure(text=self.t("bom_import_fds_button"))
    if "bom_supplier_list_button" in self.widget_text:
        self.widget_text["bom_supplier_list_button"].configure(text=self.t("bom_supplier_list_button"))
    if "bom_filter_field_label" in self.widget_text:
        self.widget_text["bom_filter_field_label"].configure(text=self.t("samples_filter_field_label"))
    if "bom_filter_value_label" in self.widget_text:
        self.widget_text["bom_filter_value_label"].configure(text=self.t("samples_filter_value_label"))
    if "bom_date_from_label" in self.widget_text:
        self.widget_text["bom_date_from_label"].configure(text=self.t("date_from_label"))
    if "bom_date_to_label" in self.widget_text:
        self.widget_text["bom_date_to_label"].configure(text=self.t("date_to_label"))
    if "bom_sort_by_label" in self.widget_text:
        self.widget_text["bom_sort_by_label"].configure(text=self.t("sort_by_label"))
    if "bom_sort_order_label" in self.widget_text:
        self.widget_text["bom_sort_order_label"].configure(text=self.t("sort_order_label"))
    if "bom_styles_section" in self.widget_text:
        self.widget_text["bom_styles_section"].configure(text=self.t("bom_styles_section"))
    if "hr_employee_new_button" in self.widget_text:
        self.widget_text["hr_employee_new_button"].configure(text=self.t("hr_employee_new_button"))
    if "hr_employee_edit_button" in self.widget_text:
        self.widget_text["hr_employee_edit_button"].configure(text=self.t("hr_employee_edit_button"))
    if "hr_employee_delete_button" in self.widget_text:
        self.widget_text["hr_employee_delete_button"].configure(text=self.t("hr_employee_delete_button"))
    if "hr_employee_toggle_button" in self.widget_text:
        self.widget_text["hr_employee_toggle_button"].configure(text=self.t("hr_employee_toggle_button"))
    if "hr_shared_lists_open_button" in self.widget_text:
        self.widget_text["hr_shared_lists_open_button"].configure(text=self.t("hr_shared_lists_open_button"))
    if "hr_attendance_new_button" in self.widget_text:
        self.widget_text["hr_attendance_new_button"].configure(text=self.t("hr_attendance_new_button"))
    if "hr_attendance_delete_button" in self.widget_text:
        self.widget_text["hr_attendance_delete_button"].configure(text=self.t("hr_attendance_delete_button"))
    if "hr_attendance_quick_button" in self.widget_text:
        self.widget_text["hr_attendance_quick_button"].configure(text=self.t("hr_attendance_quick_button"))
    if "hr_attendance_overview_button" in self.widget_text:
        self.widget_text["hr_attendance_overview_button"].configure(text=self.t("hr_attendance_overview_button"))
    if "hr_attendance_edit_button" in self.widget_text:
        self.widget_text["hr_attendance_edit_button"].configure(text=self.t("hr_attendance_edit_button"))
    if "hr_check_in_button" in self.widget_text:
        self.widget_text["hr_check_in_button"].configure(text=self.t("hr_check_in_button"))
    if "hr_check_out_button" in self.widget_text:
        self.widget_text["hr_check_out_button"].configure(text=self.t("hr_check_out_button"))
    if "hr_attendance_export_button" in self.widget_text:
        self.widget_text["hr_attendance_export_button"].configure(text=self.t("hr_attendance_export_button"))
    if "hr_attendance_employee_label" in self.widget_text:
        self.widget_text["hr_attendance_employee_label"].configure(text=self.t("hr_attendance_employee_label"))
    if "hr_employee_search_label" in self.widget_text:
        self.widget_text["hr_employee_search_label"].configure(text=self.t("hr_employee_search_label"))
    if "hr_attendance_search_label" in self.widget_text:
        self.widget_text["hr_attendance_search_label"].configure(text=self.t("hr_attendance_search_label"))
    self.widget_text["costing_source_file_label"].configure(text=self.t("costing_source_file") + ":")
    self.widget_text["costing_sort_by_label"].configure(text=self.t("sort_by_label"))
    self.widget_text["costing_sort_order_label"].configure(text=self.t("sort_order_label"))
    self.widget_text["costing_styles_section"].configure(text=self.t("costing_styles_section"))
    self.widget_text["costing_preview_section"].configure(text=self.t("costing_preview_section"))
    self.widget_text["costing_description_label"].configure(text=self.t("costing_description_label") + ":")
    self.widget_text["costing_colors_label"].configure(text=self.t("costing_colors_label") + ":")
    if "costing_adjust_help_label" in self.widget_text:
        self.widget_text["costing_adjust_help_label"].configure(text=self.t("costing_adjust_help"))
    if "costing_adjust_save_button" in self.widget_text:
        self.widget_text["costing_adjust_save_button"].configure(text=self.t("costing_adjust_save"))
    if "costing_adjust_paste_button" in self.widget_text:
        self.widget_text["costing_adjust_paste_button"].configure(text=self.t("costing_adjust_paste"))
    if "costing_adjust_close_button" in self.widget_text:
        self.widget_text["costing_adjust_close_button"].configure(text=self.t("close"))
    self.widget_text["orders_section"].configure(text=self.t("orders_section"))
    if "detail_section" in self.widget_text:
        try:
            self.widget_text["detail_section"].configure(text=self.t("detail_section"))
        except Exception:
            pass
    self.widget_text["sort_by_label"].configure(text=self.t("sort_by_label"))
    self.widget_text["sort_order_label"].configure(text=self.t("sort_order_label"))
    if "notes_title" in self.widget_text:
        try:
            self.widget_text["notes_title"].configure(text=self.t("notes_title"))
        except Exception:
            pass

    if getattr(self, "notebook", None) is not None and all(key in self.notebook_tabs for key in ("header", "items", "notes")):
        try:
            self.notebook.tab(self.notebook_tabs["header"], text=self.t("tab_header"))
            self.notebook.tab(self.notebook_tabs["items"], text=self.t("tab_items"))
            self.notebook.tab(self.notebook_tabs["notes"], text=self.t("tab_notes"))
        except Exception:
            pass

    self.widget_text["report_title"].configure(text=self.t("report_title"))
    subtitle_key = "report_subtitle"
    if self.report_scope_key == "costing":
        subtitle_key = "report_subtitle_costing"
    elif self.report_scope_key == "bom":
        subtitle_key = "report_subtitle_bom"
    elif self.report_scope_key == "tech_pack":
        subtitle_key = "report_subtitle_tech_pack"
    elif self.report_scope_key == "all":
        subtitle_key = "report_subtitle_all"
    self.widget_text["report_subtitle"].configure(text=self.t(subtitle_key))
    self.widget_text["available_fields"].configure(text=self.t("available_fields"))
    self.widget_text["selected_fields"].configure(text=self.t("selected_fields"))
    self.widget_text["add_field"].configure(text=self.t("add_field"))
    self.widget_text["add_all_fields"].configure(text=self.t("add_all_fields"))
    self.widget_text["remove_field"].configure(text=self.t("remove_field"))
    self.widget_text["remove_all_fields"].configure(text=self.t("remove_all_fields"))
    self.widget_text["move_up"].configure(text=self.t("move_up"))
    self.widget_text["move_down"].configure(text=self.t("move_down"))
    self.widget_text["export_combine_button"].configure(text=self.t("export_combine"))
    self.widget_text["save_template"].configure(text=self.t("save_template"))
    self.widget_text["reset_template"].configure(text=self.t("reset_template"))
    self.widget_text["report_target"].configure(text=self.t("report_target") + ":")
    self.widget_text["report_scope_label"].configure(text=self.t("report_scope") + ":")
    self.report_scope_combo.configure(values=self.report_scope_display_values())
    self.report_scope_var.set(self.report_scope_label(self.report_scope_key))
    self.update_report_target_summary()

    for module_key in ("tool", "admin", "moderate", "user", "bom", "tech_pack", "samples", "hr"):
        if f"{module_key}_title" in self.widget_text:
            self.widget_text[f"{module_key}_title"].configure(text=self.t(self.module_title_keys[module_key]))
        if f"{module_key}_subtitle" in self.widget_text:
            self.widget_text[f"{module_key}_subtitle"].configure(text=self.t(self.module_subtitle_keys[module_key]))
        if f"{module_key}_placeholder" in self.widget_text:
            self.widget_text[f"{module_key}_placeholder"].configure(text=self.t("module_coming_soon"))

    for text_key, label_widget in self.header_label_widgets.items():
        label_widget.configure(text=self.t(text_key) + ":")

    order_headings = {
        "export_flag": self.t("tree_export"),
        "po_number": self.t("tree_po_number"),
        "dept_number": self.t("dept_number"),
        "po_date": self.t("tree_po_date"),
        "po_time": self.t("tree_po_time"),
        "order_date": self.t("order_date"),
        "ship_date": self.t("ship_date"),
        "cancel_date": self.t("cancel_date"),
        "simple_domestic_pt": self.t("simple_domestic_pt"),
        "buyer": self.t("buyer_name"),
        "style": self.t("tree_style"),
        "season": self.t("tech_pack_season_column"),
        "vendor": self.t("tree_vendor"),
        "total_packs_ordered": self.t("total_packs_ordered"),
        "store_number": self.t("store_number"),
        "size_range": self.t("costing_size_range_column"),
        "size_scale": self.t("costing_size_scale_column"),
        "line_cost": self.t("item_cost"),
        "line_comp": self.t("item_comp"),
        "line_retail": self.t("item_retail"),
        "total_cost": self.t("tree_total_cost"),
        "total_qty": self.t("tree_total_qty"),
        "active": self.t("active_status"),
        "status": self.t("status"),
        "created_at": self.t("create_time_column"),
        "updated_at": self.t("revised_time_column"),
        "version_number": self.t("po_version_column"),
        "latest_user": self.t("latest_user_column"),
    }
    for key, label in order_headings.items():
        self.orders_tree.heading(key, text=label, command=lambda column=key: self.on_tree_heading_click(column))

    item_headings = {
        "sku": self.t("item_sku"),
        "style": self.t("item_style"),
        "color": self.t("item_color"),
        "size": self.t("item_size"),
        "description": self.t("item_description"),
        "upc": self.t("item_upc"),
        "cost": self.t("item_cost"),
        "comp": self.t("item_comp"),
        "retail": self.t("item_retail"),
        "pack_qty": self.t("item_pack_qty"),
        "total_packs": self.t("item_total_packs"),
        "total_units": self.t("item_total_units"),
    }
    if getattr(self, "items_tree", None) is not None:
        try:
            for key, label in item_headings.items():
                self.items_tree.heading(key, text=label)
        except Exception:
            pass

    self.costing_tree.heading("#0", text=self.t("costing_thumbnail_column"))
    self.costing_tree.heading("select_flag", text=self.t("export"))
    self.costing_tree.heading("created_at", text=self.t("create_time_column"))
    self.costing_tree.heading("collection", text=self.t("costing_collection_column"))
    self.costing_tree.heading("season", text=self.t("costing_season_column"))
    self.costing_tree.heading("dept_number", text=self.t("costing_dept_column"))
    self.costing_tree.heading("mj_class", text=self.t("costing_mj_class_column"))
    self.costing_tree.heading("sub_class", text=self.t("costing_sub_class_column"))
    self.costing_tree.heading("buyer", text=self.t("buyer_name"))
    self.costing_tree.heading("style_code", text=self.t("costing_style_column"))
    self.costing_tree.heading("description", text=self.t("costing_description_column"))
    self.costing_tree.heading("fabrication", text=self.t("costing_fabrication_column"))
    self.costing_tree.heading("manf_color", text=self.t("costing_manf_color_column"))
    self.costing_tree.heading("color_code", text=self.t("costing_color_code_column"))
    self.costing_tree.heading("product_type", text=self.t("costing_product_type_column"))
    self.costing_tree.heading("ticket_type", text=self.t("costing_ticket_type_column"))
    self.costing_tree.heading("packing_type", text=self.t("costing_packing_type_column"))
    self.costing_tree.heading("size_range", text=self.t("costing_size_range_column"))
    self.costing_tree.heading("size_scale", text=self.t("costing_size_scale_column"))
    self.costing_tree.heading("allocation_multiple", text=self.t("costing_allocation_multiple_column"))
    self.costing_tree.heading("min_qty", text=self.t("costing_min_column"))
    self.costing_tree.heading("max_qty", text=self.t("costing_max_column"))
    self.costing_tree.heading("inner_pack", text=self.t("costing_inner_column"))
    self.costing_tree.heading("outer_pack", text=self.t("costing_master_pack_column"))
    self.costing_tree.heading("hanger_supplier", text=self.t("costing_hanger_supplier_column"))
    self.costing_tree.heading("hanger_type", text=self.t("costing_hanger_type_column"))
    self.costing_tree.heading("item_retail", text=self.t("costing_item_retail_column"))
    self.costing_tree.heading("po_number", text=self.t("costing_po_number_column"))
    self.costing_tree.heading("total_units", text=self.t("costing_total_units_column"))
    self.costing_tree.heading("is_active", text=self.t("active_status"))
    self.costing_tree.heading("status", text=self.t("status"))
    self.costing_tree.heading("updated_at", text=self.t("revised_time_column"))
    self.costing_tree.heading("latest_user", text=self.t("latest_user_column"))
    if hasattr(self, "tech_pack_tree"):
        self.tech_pack_tree.heading("#0", text=self.t("tech_pack_image_column"))
        self.tech_pack_tree.heading("select_flag", text=self.t("export"))
        self.tech_pack_tree.heading("created_at", text=self.t("create_time_column"))
        self.tech_pack_tree.heading("buyer", text=self.t("buyer_name"))
        self.tech_pack_tree.heading("style_code", text=self.t("tech_pack_style_column"))
        self.tech_pack_tree.heading("parent_summary", text=self.t("tech_pack_parent_column"))
        self.tech_pack_tree.heading("season", text=self.t("tech_pack_season_column"))
        self.tech_pack_tree.heading("po_summary", text=self.t("tech_pack_po_column"))
        self.tech_pack_tree.heading("bom_summary", text=self.t("tech_pack_bom_column"))
        self.tech_pack_tree.heading("construction_summary", text=self.t("tech_pack_construction_column"))
        self.tech_pack_tree.heading("size_summary", text=self.t("samples_size_column"))
        self.tech_pack_tree.heading("sample_summary", text=self.t("tech_pack_sample_column"))
        self.tech_pack_tree.heading("production_summary", text=self.t("tech_pack_production_column"))
        self.tech_pack_tree.heading("quality_summary", text=self.t("tech_pack_quality_column"))
        self.tech_pack_tree.heading("updated_at", text=self.t("revised_time_column"))
        self.tech_pack_tree.heading("latest_user", text=self.t("latest_user_column"))
    if hasattr(self, "samples_tree"):
        self.samples_tree.heading("#0", text=self.t("samples_image_column"))
        sample_headings = {
            "select_flag": self.t("export"),
            "created_at": self.t("create_time_column"),
            "request_number": self.t("samples_request_column"),
            "buyer": self.t("buyer_name"),
            "style": self.t("samples_style_column"),
            "season": self.t("tech_pack_season_column"),
            "description": self.t("samples_description_column"),
            "mr_pic": self.t("samples_mr_pic_column"),
            "pattern_pic": self.t("samples_pattern_pic_column"),
            "sewer_pic": self.t("samples_sewer_pic_column"),
            "ie_pic": self.t("samples_ie_pic_column"),
            "material_date": self.t("samples_material_date_column"),
            "deadline": self.t("samples_deadline_column"),
            "sample_type": self.t("samples_type_column"),
            "color": self.t("samples_color_column"),
            "size": self.t("samples_size_column"),
            "quantity": self.t("samples_quantity_column"),
            "cutting_date": self.t("samples_cutting_date_column"),
            "sewing_date": self.t("samples_sewing_date_column"),
            "finishing_date": self.t("samples_finishing_date_column"),
            "sending_date": self.t("samples_sending_date_column"),
            "fit_evals_status": self.t("samples_fit_status_column"),
            "upload_comment_date": self.t("samples_upload_comment_date_column"),
            "buyer_comment_date": self.t("samples_buyer_comment_date_column"),
            "updated_at": self.t("revised_time_column"),
            "latest_user": self.t("latest_user_column"),
            "remark": self.t("samples_remark_column"),
        }
        for key, label in sample_headings.items():
            self.samples_tree.heading(key, text=label, command=lambda column=key: self.on_samples_heading_click(column))
    if hasattr(self, "hr_notebook"):
        self.hr_notebook.tab(0, text=self.t("hr_employees_tab"))
        self.hr_notebook.tab(1, text=self.t("hr_attendance_tab"))
    if hasattr(self, "hr_employee_tree"):
        employee_headings = {
            "employee_id": self.t("hr_employee_id_label"),
            "full_name": self.t("hr_full_name_label"),
            "display_name": self.t("hr_display_name_label"),
            "user_email": self.t("hr_linked_account_label"),
            "department": self.t("hr_department_label"),
            "position": self.t("hr_position_label"),
            "phone": self.t("hr_phone_label"),
            "email": self.t("hr_email_label"),
            "join_date": self.t("hr_join_date_label"),
            "shift_name": self.t("hr_shift_name_label"),
            "shift_start": self.t("hr_shift_start_label"),
            "shift_end": self.t("hr_shift_end_label"),
            "break_time_out": self.t("hr_break_time_out_label"),
            "break_time_in": self.t("hr_break_time_in_label"),
            "is_active": self.t("active_status"),
            "updated_at": self.t("revised_time_column"),
        }
        for key, label in employee_headings.items():
            self.hr_employee_tree.heading(key, text=label)
    if hasattr(self, "hr_attendance_tree"):
        attendance_headings = {
            "work_date": self.t("hr_work_date_label"),
            "employee_id": self.t("hr_employee_id_label"),
            "employee_name": self.t("hr_full_name_label"),
            "department": self.t("hr_department_label"),
            "check_in_at": self.t("hr_check_in_label"),
            "check_out_at": self.t("hr_check_out_label"),
            "break_out_at": self.t("hr_break_out_label"),
            "break_in_at": self.t("hr_break_in_label"),
            "worked_minutes": self.t("hr_worked_minutes_label"),
            "working_day": self.t("hr_working_day_label"),
            "late_minutes": self.t("hr_late_minutes_label"),
            "early_leave_minutes": self.t("hr_early_leave_minutes_label"),
            "status": self.t("hr_status_label"),
            "updated_at": self.t("revised_time_column"),
        }
        for key, label in attendance_headings.items():
            self.hr_attendance_tree.heading(key, text=label)

    self.po_filter_field_combo.configure(values=[self.t(text_key) for _, text_key in PO_FILTER_FIELD_OPTIONS])
    self.po_filter_field_var.set(self.translate_po_filter_field(self.po_filter_field_internal))
    self.update_po_filter_input_state()
    self.sort_key_combo.configure(values=[self.t(text_key) for _, text_key in PO_SORT_KEY_OPTIONS])
    self.sort_order_combo.configure(values=[self.t(text_key) for _, text_key in SORT_ORDER_OPTIONS])
    self.sort_key_var.set(self.translate_sort_key(self.sort_key_internal))
    self.sort_order_var.set(self.translate_sort_order(self.sort_order_internal))
    self.costing_filter_field_combo.configure(values=[self.t(text_key) for _, text_key in COSTING_FILTER_FIELD_OPTIONS])
    self.costing_filter_field_var.set(self.translate_costing_filter_field(self.costing_filter_field_internal))
    self.update_costing_filter_input_state()
    self.costing_sort_key_combo.configure(values=[self.t(text_key) for _, text_key in COSTING_SORT_KEY_OPTIONS])
    self.costing_sort_order_combo.configure(values=[self.t(text_key) for _, text_key in SORT_ORDER_OPTIONS])
    self.costing_sort_key_var.set(self.translate_costing_sort_key(self.costing_sort_key_internal))
    self.costing_sort_order_var.set(self.translate_sort_order(self.costing_sort_order_internal))
    if hasattr(self, "tech_pack_sort_key_combo"):
        self.tech_pack_filter_field_combo.configure(values=[self.t(text_key) for _, text_key in TECH_PACK_FILTER_FIELD_OPTIONS])
        self.tech_pack_filter_field_var.set(self.translate_tech_pack_filter_field(self.tech_pack_filter_field_internal))
        self.update_tech_pack_filter_input_state()
        self.tech_pack_sort_key_combo.configure(values=[self.t(text_key) for _, text_key in TECH_PACK_SORT_KEY_OPTIONS])
        self.tech_pack_sort_order_combo.configure(values=[self.t(text_key) for _, text_key in SORT_ORDER_OPTIONS])
        self.tech_pack_sort_key_var.set(self.translate_tech_pack_sort_key(self.tech_pack_sort_key_internal))
        self.tech_pack_sort_order_var.set(self.translate_sort_order(self.tech_pack_sort_order_internal))
    if hasattr(self, "samples_sort_key_combo"):
        self.samples_filter_field_combo.configure(values=[self.t(text_key) for _, text_key in SAMPLES_FILTER_FIELD_OPTIONS])
        self.samples_sort_key_combo.configure(values=[self.t(text_key) for _, text_key in SAMPLES_SORT_KEY_OPTIONS])
        self.samples_sort_order_combo.configure(values=[self.t(text_key) for _, text_key in SORT_ORDER_OPTIONS])
        self.samples_filter_field_var.set(self.translate_samples_filter_field(self.samples_filter_field_internal))
        self.samples_sort_key_var.set(self.translate_samples_sort_key(self.samples_sort_key_internal))
        self.samples_sort_order_var.set(self.translate_sort_order(self.samples_sort_order_internal))
        self.update_samples_filter_input_state()
    if hasattr(self, "bom_sort_key_combo"):
        self.bom_filter_field_combo.configure(values=[self.t(text_key) for _, text_key in BOM_FILTER_FIELD_OPTIONS])
        self.bom_sort_key_combo.configure(values=[self.t(text_key) for _, text_key in BOM_SORT_KEY_OPTIONS])
        self.bom_sort_order_combo.configure(values=[self.t(text_key) for _, text_key in SORT_ORDER_OPTIONS])
        self.bom_filter_field_var.set(self.translate_bom_filter_field(self.bom_filter_field_internal))
        self.bom_sort_key_var.set(self.translate_bom_sort_key(self.bom_sort_key_internal))
        self.bom_sort_order_var.set(self.translate_sort_order(self.bom_sort_order_internal))
        self.update_bom_filter_input_state()
    if hasattr(self, "bom_tree"):
        bom_headings = {
            "created_at": self.t("create_time_column"),
            "item_code": self.t("bom_item_code_column"),
            "item_name": self.t("bom_item_name_column"),
            "item_type": self.t("bom_item_type_column"),
            "article_ref": self.t("bom_article_column"),
            "color": self.t("samples_color_column"),
            "spec_summary": self.t("bom_spec_summary_column"),
            "uom": self.t("bom_uom_column"),
            "default_supplier": self.t("bom_supplier_column"),
            "status": self.t("active_status"),
            "updated_at": self.t("revised_time_column"),
            "latest_user": self.t("latest_user_column"),
        }
        for key, label in bom_headings.items():
            self.bom_tree.heading(key, text=label, command=lambda column=key: self.on_bom_heading_click(column))
    if hasattr(self, "warehouse_tree"):
        self.warehouse_tree.heading("status", text=self.t("status"))
        self.warehouse_tree.heading("updated_at", text=self.t("revised_time_column"))
    if hasattr(self, "production_tree"):
        self.production_tree.heading("updated_at", text=self.t("revised_time_column"))
        self.production_tree.heading("latest_user", text=self.t("latest_user_column"))
    if hasattr(self, "production_factory_tree"):
        self.production_factory_tree.heading("status", text=self.t("status"))
        self.production_factory_tree.heading("updated_at", text=self.t("revised_time_column"))
    self.refresh_role_modules()
    self.apply_permission_state()
    self.refresh_report_listboxes()
    self.refresh_saved_templates_listbox()
    if not getattr(self, "costing_items", []):
        self.costing_image_label.configure(text=self.t("costing_no_preview"))
        self.costing_description_text.delete("1.0", tk.END)
        self.costing_description_text.insert("1.0", self.t("costing_empty"))
    self.switch_module(self.current_module)


def bind_translation_module(app_cls: type) -> None:
    setattr(app_cls, "update_translations", update_translations)
