"""确定性快照与字段检查；不是生成式摘要，也不能发现所有语义错误。"""
from state_transitions import TaskState


def snapshot(state: TaskState, covered_through: int) -> dict:
    # 来源是已校验状态；关键授权不能通过对聊天文本自由总结获得。
    return {
        "task_id": state.task_id, "state_revision": state.revision,
        "order_id": state.order_id, "pickup": state.pickup,
        "submission_authorized": state.submission_authorized,
        "quality_verified": state.quality_verified,
        "source_events": [state.source_event],
        "covered_through": covered_through,
        "open_items": ([] if state.quality_verified else ["核实质量问题"]),
    }


def validate_summary(summary: dict, state: TaskState) -> list[str]:
    problems = []
    # 检查字段存在性，防止缺失 False 被错误当成默认正常值。
    for key in ("task_id", "state_revision", "order_id", "pickup",
                "submission_authorized", "quality_verified"):
        # 快照字段 state_revision 对应任务对象的 revision，显式映射命名差异。
        attribute = "revision" if key == "state_revision" else key
        if key not in summary or summary[key] != getattr(state, attribute):
            problems.append(f"字段缺失或与当前状态不符：{key}")
    if state.source_event not in summary.get("source_events", []):
        problems.append("缺少最新状态的来源事件")
    return problems


if __name__ == "__main__":
    state = TaskState(source_event="U19", pickup="2026-09-13 上午")
    good = snapshot(state, covered_through=19)
    bad = {**good, "submission_authorized": True}
    print(validate_summary(good, state))
    print(validate_summary(bad, state))
