"""本地概念模拟：无网络、无钱包、无真实交易。金额单位为美分。"""
from dataclasses import dataclass, field

@dataclass
class Lab:
    budget: int = 1000
    approval_threshold: int = 300
    active: bool = True
    allowed_merchants: set = field(default_factory=lambda: {"data.example"})
    payments: dict = field(default_factory=dict)
    delivery: dict = field(default_factory=dict)

    def pay(self, order_id, merchant, amount, approved=False, outcome="success"):
        if order_id in self.payments:
            prior = self.payments[order_id]
            if (prior["merchant"], prior["amount"]) != (merchant, amount):
                return "订单标识冲突"
            return prior["status"]
        if not self.active: return "授权失效"
        if merchant not in self.allowed_merchants: return "商家不允许"
        if not isinstance(amount, int) or amount <= 0: return "金额无效"
        reserved = sum(p["amount"] for p in self.payments.values() if p["status"] in {"成功", "状态未知"})
        if amount > self.budget - reserved: return "预算不足"
        if amount > self.approval_threshold and not approved: return "需要审批"
        status = {"success": "成功", "timeout": "状态未知", "decline": "失败"}[outcome]
        self.payments[order_id] = {"merchant": merchant, "amount": amount, "status": status}
        return status

    def record_delivery(self, order_id, status):
        if self.payments.get(order_id, {}).get("status") != "成功":
            return "不能记录交付"
        if status not in {"已交付", "交付失败"}:
            raise ValueError("无效交付状态")
        self.delivery[order_id] = status
        return status
