File size: 4,737 Bytes
86725d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# teacher_panel.py
from typing import List, Tuple, Any

import qa_store
from qa_store import normalize_question
from loader import (
    generate_new_manual_id,
    save_manual_qa_file,
    rebuild_combined_qa,
    manual_qa_table_data,
)


def _table() -> List[List[str]]:
    return manual_qa_table_data()


def teacher_save_add_overwrite(
    question: str, answer: str, current_id: str
) -> Tuple[str, str, str, List[List[str]], str]:
    """
    Add a new manual Q&A or overwrite existing one
    with the same normalized question.
    """
    question = (question or "").strip()
    answer = (answer or "").strip()
    current_id = current_id or ""

    if not question or not answer:
        msg = "❌ ກະລຸນາໃສ່ທັງຄໍາຖາມແລະຄໍາຕອບ."
        return question, answer, current_id, _table(), msg

    norm_q = normalize_question(question)
    entry = qa_store.MANUAL_QA_INDEX.get(norm_q)

    if entry:
        # overwrite existing manual Q&A
        entry["q"] = question
        entry["a"] = answer
        entry["norm_q"] = norm_q
        current_id = entry["id"]
        msg = "✅ ອັບເດດ Q&A ທີ່ມີຢູ່ແລ້ວ."
    else:
        # create new entry
        new_id = generate_new_manual_id()
        entry = {
            "id": new_id,
            "q": question,
            "a": answer,
            "norm_q": norm_q,
        }
        qa_store.MANUAL_QA_LIST.append(entry)
        qa_store.MANUAL_QA_INDEX[norm_q] = entry
        current_id = new_id
        msg = "✅ ບັນທຶກ Q&A ໃໝ່ສໍາເລັດ."

    save_manual_qa_file()
    rebuild_combined_qa()

    return question, answer, current_id, _table(), msg


def teacher_update_current(
    question: str, answer: str, current_id: str
) -> Tuple[str, str, str, List[List[str]], str]:
    """
    Update the currently selected manual Q&A by id.
    """
    question = (question or "").strip()
    answer = (answer or "").strip()
    current_id = current_id or ""

    if not current_id:
        msg = "❌ ກະລຸນາເລືອກ Q&A ຈາກຕາຕະລາງກ່ອນ."
        return question, answer, current_id, _table(), msg

    if not question or not answer:
        msg = "❌ ກະລຸນາໃສ່ທັງຄໍາຖາມແລະຄໍາຕອບ."
        return question, answer, current_id, _table(), msg

    entry = next((e for e in qa_store.MANUAL_QA_LIST if e["id"] == current_id), None)
    if not entry:
        msg = "⚠️ ບໍ່ພົບ Q&A ທີ່ເລືອກ."
        current_id = ""
        return question, answer, current_id, _table(), msg

    # remove old index
    qa_store.MANUAL_QA_INDEX.pop(entry["norm_q"], None)

    new_norm = normalize_question(question)
    entry["q"] = question
    entry["a"] = answer
    entry["norm_q"] = new_norm
    qa_store.MANUAL_QA_INDEX[new_norm] = entry

    save_manual_qa_file()
    rebuild_combined_qa()

    msg = "✅ ແກ້ໄຂ Q&A ສໍາເລັດ."
    return question, answer, current_id, _table(), msg


def teacher_delete_current(
    current_id: str,
) -> Tuple[str, str, str, List[List[str]], str]:
    """
    Delete the currently selected manual Q&A.
    """
    current_id = current_id or ""

    if not current_id:
        msg = "❌ ກະລຸນາເລືອກ Q&A ກ່ອນຈະລຶບ."
        return "", "", current_id, _table(), msg

    entry = next((e for e in qa_store.MANUAL_QA_LIST if e["id"] == current_id), None)
    if not entry:
        msg = "⚠️ ບໍ່ພົບ Q&A ທີ່ຈະລຶບ."
        return "", "", "", _table(), msg

    qa_store.MANUAL_QA_LIST = [e for e in qa_store.MANUAL_QA_LIST if e["id"] != current_id]
    qa_store.MANUAL_QA_INDEX.pop(entry["norm_q"], None)

    save_manual_qa_file()
    rebuild_combined_qa()

    msg = "🗑️ ລຶບ Q&A ສໍາເລັດ."
    return "", "", "", _table(), msg


def teacher_on_table_select(
    evt: Any, table: List[List[str]]
) -> Tuple[str, str, str, str]:
    """
    When teacher clicks a row in the Q&A table, load Q/A into textboxes.
    """
    try:
        # evt.index can be int or [row, col]
        idx = evt.index[0] if isinstance(evt.index, (list, tuple)) else evt.index
        row = table[idx]
        current_id = str(row[0])
        question = str(row[1])
        answer = str(row[2])
        msg = f"✏️ ເລືອກ Q&A ID {current_id} ສໍາລັບແກ້ໄຂ."
    except Exception:  # noqa: BLE001
        current_id = ""
        question = ""
        answer = ""
        msg = "⚠️ ບໍ່ສາມາດເລືອກແຖວໄດ້."

    return question, answer, current_id, msg