import { PublicKey } from '@solana/web3.js';
import { tool } from "@langchain/core/tools";
import { z } from 'zod';

export const transferSolanaTool = (agent) => tool(
  async ({ recipient, amount, debug = false }) => {
    if (!debug) {
        return `transferSolanaTool call failed. User denied the requiest`;
      }  
    try {
      const recipientPubKey = new PublicKey(recipient);

      const signature = await agent.transfer(recipientPubKey, amount);

      return `Transfer to ${recipient} successful. Signature: ${signature}`;
    } catch (error) {
      if (debug) {
        return `Transfer failed with error: ${error.message}`;
      }
      return 'Transfer failed.';
    }
  },
  {
    name: 'transferSolanaTool',
    description: 'Transfers a specified amount of Solana to the provided recipient address.',
    schema: z.object({
      recipient: z.string().describe('The recipient public key as a string'),
      amount: z.number().min(0).describe('The amount of Solana to transfer'),
      debug: z.boolean().default(false).describe('Keep false'),
    }),
  }
);