from __future__ import annotations

REQUIRED_RECORD_FIELDS = [
    "context_item", "trust_level", "may_inform_content", "may_grant_authority",
    "validation", "allowed_tool_scope", "approval_boundary", "observed_decision", "residual_risk"
]

def classify_context(item: dict, policy: dict) -> dict:
    kind=item.get("kind")
    if kind in policy.get("trusted_authority_kinds", []):
        expected="trusted_authority"
    elif kind in policy.get("untrusted_kinds", []):
        expected="untrusted_data"
    elif kind=="user_intent":
        expected="declared_intent"
    else:
        expected="untrusted_data"
    claimed=item.get("trust_level", expected)
    if kind in policy.get("untrusted_kinds", []) and claimed=="trusted_authority":
        return {"status":"REJECT_CONTEXT_AUTHORITY","trust_level":"untrusted_data","may_grant_authority":False}
    may_authority = kind in policy.get("trusted_authority_kinds", [])
    return {"status":"CLASSIFIED","trust_level":expected,"may_grant_authority":may_authority,"may_inform_content":bool(item.get("may_inform_content", True))}

def validate_model_proposal(proposal: dict, policy: dict, user_intent: dict, human_approved: bool=False) -> dict:
    required={"action","arguments","source_context","declared_intent"}
    if not required.issubset(proposal) or not isinstance(proposal.get("arguments"),dict):
        return {"status":"REJECT_OUTPUT_SCHEMA","executed":False}
    action=proposal["action"]
    actions=policy.get("actions",{})
    if action not in actions:
        return {"status":"DENY_OUT_OF_SCOPE_ACTION","executed":False,"guardrail":proposal.get("guardrail")}
    declared=user_intent.get("requested_action")
    if proposal.get("declared_intent") != declared or action != declared:
        return {"status":"DENY_INTENT_MISMATCH","executed":False}
    allowed=set(actions[action].get("allowed_fields",[]))
    if policy.get("unknown_fields_rejected",True) and not set(proposal["arguments"]).issubset(allowed):
        return {"status":"REJECT_OUTPUT_SCHEMA","executed":False}
    if actions[action].get("approval_required") and not human_approved:
        return {"status":"DENY_APPROVAL_REQUIRED","executed":False,"approval_boundary":"required"}
    return {"status":"ALLOW_LOCAL_STUB","executed":True,"action":action,"approval_boundary":"satisfied" if actions[action].get("approval_required") else "none"}

def guardrail_is_final_authority(policy: dict) -> bool:
    return bool(policy.get("guardrail_is_authority", False))

def evidence_row_complete(row: dict) -> bool:
    return all(k in row and row[k] not in (None, "", []) for k in REQUIRED_RECORD_FIELDS)

def classify_transfer(helper: str) -> dict:
    if helper=="document_summarizer":
        return {"status":"REBUILD_BOUNDARY","may_use_external_content":True,"may_grant_action_authority":False,"allowed_actions":[]}
    if helper=="scheduling_helper":
        return {"status":"REBUILD_BOUNDARY","may_use_external_content":True,"may_grant_action_authority":False,"allowed_actions":["create_local_reminder"],"approval_required":True}
    return {"status":"UNKNOWN_HELPER","allowed_actions":[]}
