/**
 * Test script for the external tool wrapper functionality
 * Run with: pnpm tsx src/scripts/test-external-tool-wrapper.ts
 */

import { examples } from '../examples/external-tool-wrapper-example';

async function testExternalToolWrapper(): Promise<void> {
  console.log('🧪 Testing External Tool Wrapper\n');

  try {

    console.log('📝 Test 1: Basic external tool wrapping');
    const basicTool = examples.basicWrapping();
    console.log(`✅ Tool "${basicTool.name}" wrapped successfully`);
    console.log(`   Description: ${basicTool.description}`);
    console.log(`   Schema type: ${basicTool.schema.constructor.name}`);
    
    const testInput = {
      fromAccountId: '0.0.123',
      toAccountId: '0.0.456',
      amount: 1000000,
      memo: 'Test transfer'
    };
    
    const result = await (basicTool as { _call: (input: unknown) => Promise<string> })._call(testInput);
    console.log(`   Execution result: ${result}\n`);

    console.log('📝 Test 2: Preset configurations');
    const presetTool = examples.presetConfigurations();
    console.log(`✅ Tool "${presetTool.name}" wrapped with preset config`);
    console.log(`   Description: ${presetTool.description}\n`);

    console.log('📝 Test 3: Batch wrapping multiple tools');
    const batchTools = examples.batchWrapping();
    console.log(`✅ Wrapped ${batchTools.length} tools in batch`);
    batchTools.forEach((tool, index) => {
      console.log(`   Tool ${index + 1}: ${tool.name}`);
    });
    console.log();

    console.log('📝 Test 4: Form validation integration');
    const formTool = examples.formValidationIntegration();
    console.log(`✅ Tool "${formTool.name}" wrapped with form validation`);
    console.log(`   Tool type: ${formTool.constructor.name}`);
    
    const incompleteInput = {
      fromAccountId: '0.0.123'

    } as { fromAccountId: string };
    
    try {
      const formResult = await (formTool as { _call: (input: unknown) => Promise<string> })._call(incompleteInput);
      const parsedResult = JSON.parse(formResult);
      if (parsedResult.requiresForm) {
        console.log(`   ✅ Form generation triggered for missing fields`);
        console.log(`   Form ID: ${parsedResult.formMessage.id}`);
        console.log(`   Form title: ${parsedResult.formMessage.formConfig.title}`);
        console.log(`   Fields in form: ${parsedResult.formMessage.formConfig.fields.length}`);
      }
    } catch (error) {
      console.log(`   ⚠️  Form generation test encountered error: ${error}`);
    }
    console.log();

    console.log('📝 Test 5: Custom field configurations');
    const customTool = examples.customFieldConfigs();
    console.log(`✅ Tool "${customTool.name}" wrapped with custom configs`);
    console.log(`   Description: ${customTool.description}`);
    
    const schema = customTool.schema as { _def: { shape: () => Record<string, { _renderConfig?: unknown }> } };
    const shape = schema._def.shape();
    let fieldConfigCount = 0;
    for (const [, fieldSchema] of Object.entries(shape)) {
      if (fieldSchema._renderConfig) {
        fieldConfigCount++;
      }
    }
    console.log(`   Fields with render configs: ${fieldConfigCount}`);
    console.log();

    console.log('📝 Test 6: Render config helpers');
    const { renderConfigs } = await import('../langchain/external-tool-wrapper');
    
    const textConfig = renderConfigs.text('Test Field', 'placeholder', 'help text');
    console.log(`✅ Text config: ${JSON.stringify(textConfig, null, 2)}`);
    
    const numberConfig = renderConfigs.number('Amount', 0, 100, 'Enter amount');
    console.log(`✅ Number config: ${JSON.stringify(numberConfig, null, 2)}`);
    
    const accountConfig = renderConfigs.accountId('Account ID');
    console.log(`✅ Account ID config: ${JSON.stringify(accountConfig, null, 2)}`);
    console.log();

    console.log('🎉 All tests completed successfully!');
    
  } catch (error) {
    console.error('❌ Test failed:', error);
    process.exit(1);
  }
}

testExternalToolWrapper().catch(console.error);