/**
 * Vaultfire Trust Tool for LangChain
 *
 * A complete LangChain-compatible tool that lets any LLM agent verify
 * trust profiles on the Vaultfire Protocol.
 *
 * Usage:
 *   import { VaultfireTrustTool } from './langchain-tool';
 *   const tool = new VaultfireTrustTool('base');
 *   const result = await tool.call('0x...');
 *
 * Works with LangChain JS/TS agents, chains, and tool executors.
 */

import { Tool } from 'langchain/tools';
import { VaultfireAgentSDK } from '@vaultfire/agent-sdk';

export class VaultfireTrustTool extends Tool {
  name = 'vaultfire_verify_trust';
  description =
    'Verify the trust profile of any AI agent or address on Vaultfire Protocol. ' +
    'Input should be an Ethereum address (0x...). Returns trust grade, reputation score, ' +
    'bond status, and accountability data as JSON.';

  private sdk: VaultfireAgentSDK;

  constructor(chain: 'base' | 'avalanche' | 'ethereum' = 'base') {
    super();
    this.sdk = new VaultfireAgentSDK({ chain });
  }

  async _call(address: string): Promise<string> {
    try {
      const trust = await this.sdk.verifyTrust(address.trim());
      return JSON.stringify(trust, null, 2);
    } catch (err) {
      return JSON.stringify({
        error: true,
        message: err instanceof Error ? err.message : 'Verification failed',
        address,
      });
    }
  }
}

export class VaultfireReputationTool extends Tool {
  name = 'vaultfire_get_reputation';
  description =
    'Get the reputation score and accountability history for any address on Vaultfire Protocol. ' +
    'Input should be an Ethereum address (0x...). Returns score, tier, endorsements, and violations.';

  private sdk: VaultfireAgentSDK;

  constructor(chain: 'base' | 'avalanche' | 'ethereum' = 'base') {
    super();
    this.sdk = new VaultfireAgentSDK({ chain });
  }

  async _call(address: string): Promise<string> {
    try {
      const rep = await this.sdk.getReputation(address.trim());
      return JSON.stringify(rep, null, 2);
    } catch (err) {
      return JSON.stringify({
        error: true,
        message: err instanceof Error ? err.message : 'Reputation lookup failed',
        address,
      });
    }
  }
}

export class VaultfireBondCheckTool extends Tool {
  name = 'vaultfire_check_bond';
  description =
    'Check if two addresses have an active accountability bond on Vaultfire Protocol. ' +
    'Input should be two Ethereum addresses separated by a comma (0x...,0x...).';

  private sdk: VaultfireAgentSDK;

  constructor(chain: 'base' | 'avalanche' | 'ethereum' = 'base') {
    super();
    this.sdk = new VaultfireAgentSDK({ chain });
  }

  async _call(input: string): Promise<string> {
    try {
      const [agent, partner] = input.split(',').map((s) => s.trim());
      if (!agent || !partner) {
        return JSON.stringify({ error: true, message: 'Provide two addresses separated by comma' });
      }
      const bond = await this.sdk.checkBond(agent, partner);
      return JSON.stringify(bond, null, 2);
    } catch (err) {
      return JSON.stringify({
        error: true,
        message: err instanceof Error ? err.message : 'Bond check failed',
      });
    }
  }
}
