import sys, io, json, re, os, os
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8")

SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
PROJECT_ROOT = os.path.dirname(SCRIPT_DIR)

SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
PROJECT_ROOT = os.path.dirname(SCRIPT_DIR)

with open(os.path.join(PROJECT_ROOT, "data", "raw-messages", "all_messages_raw.json"), "r", encoding="utf-8") as f:
    data = json.load(f)

feishu_re = re.compile(r"https?://[a-zA-Z0-9.-]+\.feishu\.cn/(?:wiki|docx)/[A-Za-z0-9]+")
file_re = re.compile(r"\[文件\]\s+(.+?)\s+fileId:\s+(\S+)")
kimi_re = re.compile(r"https?://[a-zA-Z0-9.-]+\.ok\.kimi\.link/\S*")

all_feishu = set()
all_files = []
all_kimi = []

for group_name, msgs in data.items():
    print(f"\n=== {group_name} ({len(msgs)} msgs) ===")
    if msgs:
        print(f"Time: {msgs[-1]['createTime']} ~ {msgs[0]['createTime']}")
    
    senders = {}
    for msg in msgs:
        content = msg.get("content", "")
        sender = msg.get("sender", "unknown")
        senders[sender] = senders.get(sender, 0) + 1
        
        for link in feishu_re.findall(content):
            all_feishu.add(link)
        
        fm = file_re.search(content)
        if fm:
            all_files.append({"name": fm.group(1), "fileId": fm.group(2), "sender": sender, "time": msg.get("createTime",""), "group": group_name})
        
        for link in kimi_re.findall(content):
            desc = content.split("https")[0].strip()[:80]
            all_kimi.append({"url": link, "desc": desc, "sender": sender, "group": group_name})
    
    print(f"Feishu links: {len([l for l in all_feishu if any(m.get('openConversationId','') == ('cidoUneRB4Db8TAXaTrKxkQAw==' if group_name=='dc' else 'cidMuM+itt5PeY7xNSWsv3M0g==') for m in msgs)])}")
    print(f"Top senders: {sorted(senders.items(), key=lambda x: -x[1])[:5]}")

print(f"\n=== TOTALS ===")
print(f"Unique Feishu links: {len(all_feishu)}")
print(f"File attachments: {len(all_files)}")
print(f"Kimi links: {len(all_kimi)}")

print("\nAll Feishu links:")
for link in sorted(all_feishu):
    print(f"  {link}")

print("\nAll files:")
for f in all_files:
    print(f"  [{f['group']}] {f['name']} (by {f['sender']}, {f['time']})")

print("\nAll Kimi links:")
for k in all_kimi:
    print(f"  [{k['group']}] {k['desc']} -> {k['url']} (by {k['sender']})")




