"""Vaultfire Key Protocol — Agent-to-Agent Delegation Flow

This example shows how Agent A can delegate scoped signing authority
to Agent B, gated by on-chain trust requirements (bonds + reputation).
If Agent B's bond gets slashed or reputation drops, the delegation
auto-revokes.

This is the "XMTP for keys" flow.
"""

from vkp import VKPClient

# Connect to the network
client = VKPClient(
    rpc_url="https://mainnet.base.org",
    network_id="base-mainnet",
    delegation_registry_address="0x...",  # Replace with deployed address
)

# ─────────────────────────────────────────────────────────
# AGENT A (Delegator): Grant scoped authority to Agent B
# ─────────────────────────────────────────────────────────

# Agent A creates a delegation with strict trust requirements
tx = client.delegations.create_delegation(
    from_address="0xAgentA",
    delegate="0xAgentB",
    max_value="0.1",             # Agent B can spend max 0.1 ETH per tx
    total_limit="1.0",           # Max 1.0 ETH total over the delegation
    duration_hours=48,           # Expires in 48 hours
    min_bond="0.01",             # Agent B must have >= 0.01 ETH bonded
    min_reputation=50,           # Agent B must have >= 50/100 reputation
    allowed_contracts=[          # Agent B can ONLY call these contracts
        "0xUniswapRouter",
    ],
)
print("Agent A: Sign and send this to create delegation:")
print(tx)

# ─────────────────────────────────────────────────────────
# AGENT B (Delegate): Accept the delegation
# ─────────────────────────────────────────────────────────

# Agent B accepts — contract verifies their bond and reputation on-chain
# tx = client.delegations.accept_delegation(
#     from_address="0xAgentB",
#     delegation_id=0,  # The ID returned from createDelegation
# )
# print("Agent B: Sign and send this to accept delegation:")
# print(tx)

# ─────────────────────────────────────────────────────────
# CHECK STATUS
# ─────────────────────────────────────────────────────────

# Check if a delegation is still valid
# valid = client.delegations.is_valid(delegation_id=0)
# print(f"Delegation valid: {valid}")

# Check remaining spend allowance
# info = client.delegations.get_delegation(delegation_id=0)
# print(f"Remaining: {info.remaining_wei} wei")
# print(f"Accepted: {info.accepted}")

# ─────────────────────────────────────────────────────────
# EMERGENCY REVOKE
# ─────────────────────────────────────────────────────────

# Agent A can revoke at any time
# tx = client.delegations.revoke_delegation(
#     from_address="0xAgentA",
#     delegation_id=0,
# )

# Or revoke EVERYTHING in one call
# tx = client.delegations.revoke_all(from_address="0xAgentA")
