import type { GustoClient } from '../clients/gusto.js';
import type { BankAccount } from '../types/index.js';

export function createBankAccountTools(client: GustoClient) {
  return {
    // List bank accounts for an employee
    list_bank_accounts: async (args: { employee_id: string }) => {
      const accounts = await client.get<BankAccount[]>(
        `/employees/${args.employee_id}/bank_accounts`
      );
      return {
        content: [
          {
            type: 'text',
            text: JSON.stringify(accounts, null, 2),
          },
        ],
      };
    },

    // Create a new bank account
    create_bank_account: async (args: {
      employee_id: string;
      routing_number: string;
      account_number: string;
      account_type: 'Checking' | 'Savings';
      name?: string;
    }) => {
      const account = await client.post<BankAccount>(
        `/employees/${args.employee_id}/bank_accounts`,
        {
          routing_number: args.routing_number,
          account_number: args.account_number,
          account_type: args.account_type,
          name: args.name,
        }
      );
      return {
        content: [
          {
            type: 'text',
            text: `Bank account created successfully:\n${JSON.stringify(account, null, 2)}`,
          },
        ],
      };
    },

    // Verify a bank account
    verify_bank_account: async (args: {
      bank_account_id: string;
      deposit_1: string;
      deposit_2: string;
    }) => {
      const account = await client.put<BankAccount>(
        `/bank_accounts/${args.bank_account_id}/verify`,
        {
          deposit_1: args.deposit_1,
          deposit_2: args.deposit_2,
        }
      );
      return {
        content: [
          {
            type: 'text',
            text: `Bank account verified successfully:\n${JSON.stringify(account, null, 2)}`,
          },
        ],
      };
    },
  };
}

export const bankAccountToolDefinitions = [
  {
    name: 'list_bank_accounts',
    description: 'List all bank accounts for an employee',
    inputSchema: {
      type: 'object',
      properties: {
        employee_id: { type: 'string', description: 'Employee ID' },
      },
      required: ['employee_id'],
    },
  },
  {
    name: 'create_bank_account',
    description: 'Create a new bank account for an employee',
    inputSchema: {
      type: 'object',
      properties: {
        employee_id: { type: 'string', description: 'Employee ID' },
        routing_number: { type: 'string', description: 'Bank routing number' },
        account_number: { type: 'string', description: 'Bank account number' },
        account_type: {
          type: 'string',
          enum: ['Checking', 'Savings'],
          description: 'Account type',
        },
        name: { type: 'string', description: 'Account nickname' },
      },
      required: ['employee_id', 'routing_number', 'account_number', 'account_type'],
    },
  },
  {
    name: 'verify_bank_account',
    description: 'Verify a bank account using micro-deposit amounts',
    inputSchema: {
      type: 'object',
      properties: {
        bank_account_id: { type: 'string', description: 'Bank Account ID' },
        deposit_1: { type: 'string', description: 'First micro-deposit amount' },
        deposit_2: { type: 'string', description: 'Second micro-deposit amount' },
      },
      required: ['bank_account_id', 'deposit_1', 'deposit_2'],
    },
  },
];
