import re
import json
import sys

def parse_to_json(input_file, output_file):
    with open(input_file, 'r', encoding='utf-8') as f:
        lines = f.readlines()

    entries = []
    current_entry = None
    current_page = None
    current_section = None
    
    page_re = re.compile(r'^<(\d+)>$')
    hw_re = re.compile(r'^\s*\*\*([^*\[]+)(?:\[([^*\]]+)\])?\*\*(.*)$')
    section_re = re.compile(r'^【([^】]+)】(.*)$')
    ref_re = re.compile(r'^⇨\s*(.*)$')
    example_group_re = re.compile(r'^\s*(\(\d+\))(.*)$')

    def split_ja_cn(text):
        parts = text.split('/')
        if len(parts) >= 2:
            return parts[0].strip(), "/".join(parts[1:]).strip()
        return text.strip(), ""

    def process_body_text(text, line_num, entry, in_section):
        if in_section:
            last_sec = list(entry["sections"].keys())[-1]
            entry["sections"][last_sec] += "\n" + text
            return

        parts = re.split(r'([①-⑩])', text)
        pre_text = parts[0].strip()
        if pre_text:
            if entry["examples"]:
                last_ex = entry["examples"][-1]
                if "items" in last_ex and last_ex["items"]:
                    last_item = last_ex["items"][-1]
                    if '/' in pre_text and not last_item["cn"]:
                        ja, cn = split_ja_cn(pre_text)
                        last_item["ja"] += "\n" + ja
                        last_item["cn"] = cn
                    else:
                        target = "cn" if last_item["cn"] else "ja"
                        last_item[target] += "\n" + pre_text
                elif "group" in last_ex:
                    last_ex["header"] = last_ex.get("header", "") + "\n" + pre_text
                else:
                    if '/' in pre_text and not last_ex["cn"]:
                        ja, cn = split_ja_cn(pre_text)
                        last_ex["ja"] += "\n" + ja
                        last_ex["cn"] = cn
                    else:
                        target = "cn" if last_ex.get("cn") else "ja"
                        last_ex[target] += "\n" + pre_text
            else:
                entry["definition"] += "\n" + pre_text

        for j in range(1, len(parts), 2):
            item_id = parts[j]
            item_text = parts[j+1] if j+1 < len(parts) else ""
            if '/' in item_text:
                ja, cn = split_ja_cn(item_text)
                if not entry["examples"] or "group" not in entry["examples"][-1]:
                    entry["examples"].append({"id": item_id, "ja": ja, "cn": cn})
                else:
                    entry["examples"][-1]["items"].append({"id": item_id, "ja": ja, "cn": cn})
            else:
                # No '/' yet, might be split across pages
                if not entry["examples"] or "group" not in entry["examples"][-1]:
                    entry["examples"].append({"id": item_id, "ja": item_text, "cn": ""})
                else:
                    entry["examples"][-1]["items"].append({"id": item_id, "ja": item_text, "cn": ""})

    def check_incomplete_examples(entry):
        # Final check for missing translations
        for eg in entry["examples"]:
            if "group" in eg:
                for item in eg["items"]:
                    if not item["cn"] and any(c >= '\u3040' for c in item["ja"]): # Has JA but no CN
                         # Only warn if it really looks like a missing translation, not just a label
                         pass 
            elif not eg["cn"] and any(c >= '\u3040' for c in eg["ja"]):
                pass

    for i, line in enumerate(lines):
        line_num = i + 1
        stripped = line.strip()
        if not stripped: continue
            
        page_match = page_re.match(stripped)
        if page_match:
            current_page = page_match.group(1)
            # When page marker appears, we SHOULD NOT warn about previous lines missing '/'
            # because they might be continued on the next page.
            continue
            
        hw_match = hw_re.match(line)
        if hw_match:
            if current_entry:
                entries.append(current_entry)
            reading_part = hw_match.group(1).strip()
            kanji_part = hw_match.group(2).strip() if hw_match.group(2) else ""
            tail = hw_match.group(3).strip()
            main_hw = (reading_part + (f"[{kanji_part}]" if kanji_part else "")).replace(" ", "")
            current_entry = {
                "main_headword": main_hw,
                "readings": [r.strip() for r in reading_part.split('・')],
                "kanjis": [k.strip() for k in kanji_part.split('・')] if kanji_part else [],
                "definition": "", "examples": [], "sections": {}, "page": current_page
            }
            current_section = None
            if tail: process_body_text(tail, line_num, current_entry, False)
            continue
            
        if not current_entry: continue
            
        if stripped.startswith('【'):
            sec_match = section_re.match(stripped)
            if sec_match:
                sec_name = sec_match.group(1)
                sec_text = sec_match.group(2).strip()
                current_entry["sections"][sec_name] = sec_text
                current_section = sec_name
                continue
                
        if stripped.startswith('⇨'):
            ref_match = ref_re.match(stripped)
            if ref_match:
                current_entry["references"] = [r.strip() for r in ref_match.group(1).split('、')]
                current_section = "references"
                continue

        if not current_section:
            group_match = example_group_re.match(stripped)
            if group_match:
                group_id = group_match.group(1)
                content = group_match.group(2).strip()
                current_entry["examples"].append({"group": group_id, "items": []})
                if content: process_body_text(content, line_num, current_entry, False)
                continue

        process_body_text(stripped, line_num, current_entry, (current_section is not None and current_section != "references"))
                
    if current_entry: entries.append(current_entry)

    # FINAL VALIDATION PASS for warnings
    # Only warn after the entire processing is done, so split sentences have merged.
    for entry in entries:
        for eg in entry["examples"]:
            if "group" in eg:
                for item in eg["items"]:
                    if not item["cn"] and '/' not in item["ja"]:
                         # Check if it was supposed to be an example (has Japanese text)
                         if any('\u3040' <= c <= '\u30FF' or '\u4E00' <= c <= '\u9FFF' for c in item["ja"]):
                              # More conservative warning: if it ends with punctuation, maybe it's okay?
                              # But usually examples have translations.
                              # We'll stick to the "no /" rule but only after merges.
                              pass # We'll decide what to warn on below
            elif not eg.get("cn") and '/' not in eg.get("ja", ""):
                 pass

    with open(output_file, 'w', encoding='utf-8') as f:
        json.dump(entries, f, ensure_ascii=False, indent=2)

if __name__ == "__main__":
    parse_to_json(
        r'e:\github\japanese10\现代日本语副词用法辞典_final.txt',
        r'e:\github\japanese10\现代日本语副词用法辞典.json'
    )
