import sys, io, json, subprocess, 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)

DWS = r"C:\Users\admin\.real\.bin\dws\bin\dws.exe"

def fetch(group_id, time_str, forward="true"):
    cmd = [DWS, "chat", "message", "list", "--group", group_id, "--time", time_str, "--forward", forward, "--format", "json", "--limit", "200"]
    result = subprocess.run(cmd, capture_output=True, timeout=60)
    output = result.stdout.decode("utf-8", errors="replace")
    json_start = output.find("{")
    if json_start < 0: return [], False
    data = json.loads(output[json_start:])
    msgs = data.get("result", {}).get("messages", [])
    has_more = data.get("result", {}).get("hasMore", False)
    return msgs, has_more

dc_id = "cidoUneRB4Db8TAXaTrKxkQAw=="

# Fetch dc 05-24+
msgs, hm = fetch(dc_id, "2026-05-24 00:00:00", "true")
print("dc 05-24+: %d msgs, hasMore=%s" % (len(msgs), hm), flush=True)
if msgs:
    t0 = msgs[-1]["createTime"]
    t1 = msgs[0]["createTime"]
    print("  Range: %s ~ %s" % (t0, t1), flush=True)

# Fetch backwards from 06-03
msgs2, hm2 = fetch(dc_id, "2026-06-03 00:00:00", "false")
print("dc before 06-03: %d msgs, hasMore=%s" % (len(msgs2), hm2), flush=True)
if msgs2:
    t0 = msgs2[-1]["createTime"]
    t1 = msgs2[0]["createTime"]
    print("  Range: %s ~ %s" % (t0, t1), flush=True)

# Now combine ALL dc messages with dedup
all_dc = {}

# Segment 1: 05-01 forward
msgs1, _ = fetch(dc_id, "2026-05-01 00:00:00", "true")
for m in msgs1: all_dc[m["openMessageId"]] = m

# Segment 2: 05-19 forward
msgs2, _ = fetch(dc_id, "2026-05-19 00:00:00", "true")
for m in msgs2: all_dc[m["openMessageId"]] = m

# Segment 3: 05-24 forward
msgs3, _ = fetch(dc_id, "2026-05-24 00:00:00", "true")
for m in msgs3: all_dc[m["openMessageId"]] = m

# Segment 4: backwards from 06-03
msgs4, _ = fetch(dc_id, "2026-06-03 00:00:00", "false")
for m in msgs4: all_dc[m["openMessageId"]] = m

print("\nTotal unique dc messages: %d" % len(all_dc), flush=True)
all_msgs = list(all_dc.values())
if all_msgs:
    times = sorted([m["createTime"] for m in all_msgs])
    print("Full range: %s ~ %s" % (times[0], times[-1]), flush=True)

# Also get cx
cx_id = "cidMuM+itt5PeY7xNSWsv3M0g=="
all_cx = {}
msgs, _ = fetch(cx_id, "2026-05-01 00:00:00", "true")
for m in msgs: all_cx[m["openMessageId"]] = m
print("Total unique cx messages: %d" % len(all_cx), flush=True)

# Save
output = {"dc": list(all_dc.values()), "cx": list(all_cx.values())}
with open(os.path.join(PROJECT_ROOT, "data", "raw-messages", "all_messages_combined.json"), "w", encoding="utf-8") as f:
    json.dump(output, f, ensure_ascii=False, indent=2)
print("Saved!")

