"""Vaultfire AgentKit — LangChain Agent Example

This example creates a conversational AI agent that can register itself
on-chain, create bonds, and verify other agents — all through natural language.

Prerequisites:
  pip install coinbase-agentkit coinbase-agentkit-langchain vaultfire-agentkit langchain-openai

Set environment variables:
  CDP_API_KEY_ID=your-key-id
  CDP_API_KEY_SECRET=your-key-secret
  CDP_WALLET_SECRET=your-wallet-secret
  OPENAI_API_KEY=your-openai-key
"""

import os

from coinbase_agentkit import (
    AgentKit,
    AgentKitConfig,
    CdpEvmWalletProvider,
    CdpEvmWalletProviderConfig,
    wallet_action_provider,
)
from coinbase_agentkit_langchain import CoinbaseAgentKitToolkit
from langchain_openai import ChatOpenAI
from langgraph.prebuilt import create_react_agent
from vaultfire_agentkit import vaultfire_action_provider


SYSTEM_PROMPT = """You are an AI agent with on-chain trust capabilities powered by the Vaultfire Protocol.

You can:
- Register your identity on-chain (ERC-8004 standard)
- Stake accountability bonds to prove your trustworthiness
- Create partnership bonds with other agents
- Verify any agent's trust profile before interacting
- Resolve VNS (Vaultfire Name Service) names

Always register yourself first before creating bonds.
When asked to interact with another agent, check their trust profile first.
"""


def main():
    # Set up wallet
    wallet_provider = CdpEvmWalletProvider(CdpEvmWalletProviderConfig(
        api_key_id=os.environ["CDP_API_KEY_ID"],
        api_key_secret=os.environ["CDP_API_KEY_SECRET"],
        wallet_secret=os.environ["CDP_WALLET_SECRET"],
        network_id="base-mainnet",
    ))

    # Create AgentKit with Vaultfire trust layer
    agentkit = AgentKit(AgentKitConfig(
        wallet_provider=wallet_provider,
        action_providers=[
            wallet_action_provider(),
            vaultfire_action_provider(),
        ],
    ))

    # Create LangChain tools
    toolkit = CoinbaseAgentKitToolkit.from_agentkit(agentkit)
    tools = toolkit.get_tools()

    # Create the agent
    llm = ChatOpenAI(model="gpt-4o")
    agent = create_react_agent(llm, tools, prompt=SYSTEM_PROMPT)

    # Interactive loop
    print("Vaultfire Agent ready. Type 'quit' to exit.\n")
    while True:
        user_input = input("You: ").strip()
        if user_input.lower() in ("quit", "exit"):
            break

        response = agent.invoke({"messages": [("user", user_input)]})
        last_message = response["messages"][-1].content
        print(f"Agent: {last_message}\n")


if __name__ == "__main__":
    main()
