#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""更正 id=291 回执里「三段单 wav 已在交换目录」一句：当时实际只在 zip 内，现已补齐根目录并自测。"""
import os, sys, json, subprocess, urllib.request

BASE = r"D:\Download\muse_exchange"
SEND_URL = "http://localhost:8801/send?box=huajiang"
OP = urllib.request.build_opener(urllib.request.ProxyHandler({}))


def gate(tag):
    r = subprocess.run([sys.executable, os.path.join(BASE, "wb_gate.py"), "--tag", tag],
                       cwd=BASE, capture_output=True, text=True)
    print(r.stdout[-500:], flush=True)
    return r.returncode


def main():
    if gate("ep03_fix6_correct_reply") != 0:
        print("GATE 非 GO，不发更正", flush=True)
        return
    text = (
        "【更正 id=291 一句交付路径错报｜验收结论不变】\n\n"
        "id=291 正文写「三段 wav 也已单文件在交换目录」——发出时那句不准：三段当时只在 "
        "ep03_vox_fix6_3seg.zip 内，没在 8800 根目录（下载接口只认根目录文件名）。\n\n"
        "现已补齐：ep03_vox_fix6_p04.wav / p61.wav / p107.wav 三个单文件已放到交换目录根目录，"
        "逐字节下载自测全部 PASS（字节 752684 / 2273324 / 414764，与源一致）。\n\n"
        "三段时长、读音锁验收、zip（sha256 07628d2f…1b17cd）与结论全部不变。仍停在检查点，"
        "等花酱验 + 刚耳检通过再发第2步。"
    )
    body = {"from": "workbuddy", "kind": "note", "text": text, "files": []}
    req = urllib.request.Request(SEND_URL, data=json.dumps(body, ensure_ascii=False).encode("utf-8"),
                                 headers={"Content-Type": "application/json"}, method="POST")
    resp = OP.open(req, timeout=30)
    print("REPLY_HTTP", resp.status, resp.read().decode("utf-8", "replace"), flush=True)
    rows = [json.loads(l) for l in open(os.path.join(BASE, "mailbox_data/huajiang.jsonl"),
                                        encoding="utf-8") if l.strip()]
    print("READBACK", rows[-1].get("id"), rows[-1].get("kind"), str(rows[-1].get("text"))[:80], flush=True)


if __name__ == "__main__":
    main()
