"""可信审批入口的内存演示；不执行上传，也不生成生产凭据。"""
from dataclasses import asdict, dataclass
from contracts import digest

@dataclass(frozen=True)
class Action:
    # 主体应由认证会话注入；模型只提出候选目标，不能冒充用户。
    tenant: str
    user: str
    artifact_hash: str
    destination: str
    policy_version: str

@dataclass(frozen=True)
class Approval:
    action_hash: str
    expires_at: float
    approved: bool


def approve_from_trusted_ui(action, decision, now):
    if type(decision) is not bool:
        raise ValueError("decision_must_be_boolean")
    return Approval(digest(asdict(action)), now + 600, decision)


def may_upload(action, approval, now, currently_authorized):
    # 旧批准不能覆盖当前撤权；动作 hash 绑定产物和目的地。
    return (
        currently_authorized is True and approval.approved is True
        and now < approval.expires_at
        and approval.action_hash == digest(asdict(action))
    )

if __name__ == "__main__":
    action = Action("shop-a", "user-417", "demo-content-hash", "company-reports", "policy-1")
    # 固定时钟与批准仅用于教学，不模拟真实用户授权。
    approval = approve_from_trusted_ui(action, True, now=1000)
    print(may_upload(action, approval, now=1001, currently_authorized=True))
    print(may_upload(action, approval, now=1001, currently_authorized=False))
