from __future__ import annotations

import re
from typing import Dict


def parse_order_info(msg: str) -> Dict[str, str]:
    """从系统订单消息中提取字段。"""
    info: Dict[str, str] = {}
    m = re.search(r"订单号[：:]\s*(\d+)", msg or "")
    if m:
        info["order_id"] = m.group(1)
    m = re.search(r"订单状态[：:]\s*([^\s\[]+)", msg or "")
    if m:
        info["order_status"] = m.group(1).strip()
    m = re.search(r"\[状态[：:]\s*([^\]]+)\]", msg or "")
    if m:
        info["pay_status"] = m.group(1).strip()
    m = re.search(r"金额[：:]\s*([\d.]+)元", msg or "")
    if m:
        info["amount"] = m.group(1)
    m = re.search(r"数量[：:]\s*(\d+)", msg or "")
    if m:
        info["quantity"] = m.group(1)
    m = re.search(r"(\d{4}-\d{1,2}-\d{1,2}\s+\d{1,2}:\d{2}:\d{2})", msg or "")
    if m:
        info["order_time"] = m.group(1).strip()
    m = re.search(r"买家备注[：:]\s*([^\n]+)", msg or "")
    if m and m.group(1).strip():
        info["buyer_note"] = m.group(1).strip()
    return info


def order_instruction(pay_status: str, order_status: str) -> str:
    paid_keywords = ["等待发货", "已付款", "付款成功", "买家已付款"]
    if any(kw in (pay_status or "") or kw in (order_status or "") for kw in paid_keywords):
        return "【已付款-必须回复】客户已付款，立刻自然回复确认收款并告知马上安排。"
    return "【仅系统通知-无需回复客户】这是系统订单通知，不需要回复客户任何内容，直接跳过。"
