import 'dotenv/config';

import { CreateKnowledgeResponse, InfoKnowledgeResponse, ReAIVolcKnowledge } from '../src';
import { logger } from '../src/utils/logger';
import { AddDocumentRequest, AddDocumentResponse, InfoDocumentResponse, ListDocumentResponse } from '../src/types/doc';
import { AddPointResponse, InfoPointResponse, ListPointResponse } from '../src/types/point';
import { SearchKnowledgeResponse } from '../src/types/query';

(async () => {
  
  // 列表实测
//   try {
//     const result = await ReAIVolcKnowledge.knowledge.list();
//     logger.info('✓ Test passed:', JSON.stringify(result, null, 2));
//   } catch (error) {
//     console.error('Test failed:', error);
//     process.exit(1);
//   }

  // 详情测试
  // try {
  //   const result = await ReAIVolcKnowledge.doRequest<InfoKnowledgeResponse>({
  //     endpoint: '/api/knowledge/collection/info',
  //     method: 'POST',
  //     body: {
  //       name: 'reai_test'
  //     }
  //   })
  //   logger.info('✓ Test passed:', JSON.stringify(result, null, 2));
  // } catch (error) {
  //   console.error('Test failed:', error);
  //   process.exit(1);
  // }

  // 创建测试
  // try {
  //   const result = await ReAIVolcKnowledge.doRequest<CreateKnowledgeResponse>({
  //     endpoint: '/api/knowledge/collection/create',
  //     method: 'POST',
  //     body: {
  //       name: 'reai_dev_test',
  //       description: '开发测试'
  //     }
  //   })
  //   logger.info('✓ Test passed:', JSON.stringify(result, null, 2));
  // } catch (error) {
  //   console.error('Test failed:', error);
  //   process.exit(1);
  // }

  // 更新测试
  // try {
  //   const result = await ReAIVolcKnowledge.doRequest<any>({
  //     endpoint: '/api/knowledge/collection/update',
  //     method: 'POST',
  //     body: {
  //       name: 'reai_dev_test',
  //       description: '开发测试更新一下',
  //       // fields: [
  //       //   {
  //       //     field_name: 'test',
  //       //     field_type: "string",
  //       //     default_val: 'test',
  //       //   }
  //       // ]
  //     }
  //   })
  //   logger.info('✓ Test passed:', JSON.stringify(result, null, 2));
  // } catch (error) {
  //   console.error('Test failed:', error);
  //   process.exit(1);
  // }

  // 删除测试
  // try {
  //   await ReAIVolcKnowledge.doRequest<any>({
  //     endpoint: '/api/knowledge/collection/delete',
  //     method: 'POST',
  //     body: {
  //       name: 'reai_dev_test',
  //       // project: 'default',
  //     }
  //   })
  // } catch (error) {
  //   console.error('Test failed:', error);
  //   process.exit(1);
  // }

  // 文档添加测试
  // try {
  //  const result = await ReAIVolcKnowledge.doRequest<AddDocumentResponse>({
  //    endpoint: '/api/knowledge/doc/add',
  //    method: 'POST',
  //    body: {
  //      collection_name: 'reai_test',
  //      project: '',
  //      add_type: 'url',
  //      doc_id: 'reai_123456',
  //      doc_name: '测试文档',
  //      doc_type: 'pdf',
  //      url: 'https://files.seeedstudio.com/wiki/XIAO-BLE/Seeed-Studio-XIAO-nRF52840-Sense-v1.1.pdf',
  //      meta: []
  //    }
  //  }) 
  //  logger.info('✓ Test passed:', JSON.stringify(result, null, 2));
  // } catch (error) {
  //   console.error('Test failed:', error);
  //   process.exit(1);
  // }

  // 文档添加测试 lark
  // try {
  //   const result = await ReAIVolcKnowledge.doRequest<AddDocumentResponse>({
  //     endpoint: '/api/knowledge/doc/add',
  //     method: 'POST',
  //     body: {
  //       collection_name:'reai_test',
  //       project: '',
  //       doc_id:'reai_12345678',
  //       add_type: 'lark',
  //       lark_file: {
  //         url: 'https://ixydfoqux8c.feishu.cn/docx/QKz5dlaIXo23JTxweW9cIq4Cnwc?from=from_copylink',
  //       }
  //     }
  //   })
  //   logger.info('✓ Test passed:', JSON.stringify(result, null, 2));
  // } catch (error) {
  //   console.error('Test failed:', error);
  //   process.exit(1);
  // }

  // 文档详情测试
  // try {
  //   const result = await ReAIVolcKnowledge.doRequest<InfoDocumentResponse>({
  //     endpoint: '/api/knowledge/doc/info',
  //     method: 'POST',
  //     body: {
  //       collection_name: 'reai_test',
  //       doc_id: 'reai_123456'
  //     }
  //   });
  //   logger.info('✓ Test passed:', JSON.stringify(result, null, 2));
  // } catch (error) {
  //   console.error('Test failed:', error);
  //   process.exit(1);
  // }

  // 文档列表测试
  // try {
  //  const result = await ReAIVolcKnowledge.doRequest<ListDocumentResponse>({
  //    endpoint: '/api/knowledge/doc/list',
  //    method: 'POST',
  //    body: {
  //      collection_name:'reai_test',
  //      project: '',
  //      offset: 0,
  //      limit: 100,
  //      return_token_usage: true
  //    }
  //  }) 
  //  logger.info('✓ Test passed:', JSON.stringify(result, null, 2));
  // } catch (error) {
  //   console.error('Test failed:', error);
  //   process.exit(1);
  // }

  // 更新meta测试
  // try {
  //   const result = await ReAIVolcKnowledge.doRequest<any>({
  //     endpoint: '/api/knowledge/doc/update_meta',
  //     method: 'POST',
  //     body: {
  //       collection_name:'reai_test',
  //       project: '',
  //       doc_id: 'reai_123456',
  //       meta: [
  //         {
  //           field_name: 'foo',
  //           field_type: 'string',
  //           field_value: 'bar'
  //         }
  //       ]
  //    }})
  // } catch (error) {
  //   console.error('Test failed:', error);
  //   process.exit(1);
  // }

  // 文档删除测试
  // try {
  //   const response = await ReAIVolcKnowledge.doRequest<any>({
  //     endpoint: '/api/knowledge/doc/delete',
  //     method: 'POST',
  //     body: {
  //       collection_name: 'reai_test',
  //       project: '',
  //       doc_id: 'reai_123456',
  //     },
  //   });
  //   console.log('Test passed:', response);
  // } catch (error) {
  //   console.error('Test failed:', error);
  //   process.exit(1);
  // }

  // 切片列表测试
  // try {
  //   const result = await ReAIVolcKnowledge.doRequest<ListPointResponse>({
  //     endpoint: '/api/knowledge/point/list',
  //     method: 'POST',
  //     body: {
  //       collection_name: 'reai_test',
  //       project: '',
  //       offset: 0,
  //       limit: 100,
  //       doc_ids: ['reai_123456'],
  //     },
  //  });
  //  logger.info('✓ Test passed:', JSON.stringify(result, null, 2));
  
  // } catch (error) {
  //   console.error('Test failed:', error);
  //   process.exit(1);
  // }

  // 切片更新测试
  // try {
  //   const result = await ReAIVolcKnowledge.doRequest<any>({
  //     endpoint: '/api/knowledge/point/update',
  //     method: 'POST',
  //     body: {
  //       collection_name: 'reai_test',
  //       project: '',
  //       point_id: 'reai_123456-0',
  //       content: ' Seeed Studio Seeed Studio XIAO nRF52848',
  //     },
  //   });
  //   logger.info('✓ Test passed:', JSON.stringify(result, null, 2));
  // } catch (error) {
  //   console.error('Test failed:', error);
  //   process.exit(1);
  // }

  // 切片详情测试
  // try {
  //   const result = await ReAIVolcKnowledge.doRequest<InfoPointResponse>({
  //     endpoint: '/api/knowledge/point/info',
  //     method: 'POST',
  //     body: {
  //       collection_name: 'reai_test',
  //       point_id: 'reai_123456-0',
  //       get_attachment_link: true,
  //     },
  //   });
  //   logger.info('✓ Test passed:', JSON.stringify(result, null,2)); 
  // } catch (error) {
  //   console.error('Test failed:', error);
  //   process.exit(1);
  // }

  // 切片添加测试
  // try {
  //   const result = await ReAIVolcKnowledge.doRequest<AddPointResponse>({
  //     endpoint: '/api/knowledge/point/add',
  //     method: 'POST',
  //     body: {
  //       collection_name: 'reai_test',
  //       project: '',
  //       doc_id: 'reai_123456',
  //       chunk_type: 'text',
  //       content: ' Seeed Studio Seeed Studio XIAO nRF52848 1.1',
  //     }
  //   })
  //   logger.info('✓ Test passed:', JSON.stringify(result, null, 2));
  // } catch (error) {
  //   console.error('Test failed:', error);
  //   process.exit(1);
  // }

  // 切片删除测试
  // try {
  //   const result = await ReAIVolcKnowledge.doRequest<any>({
  //     endpoint: '/api/knowledge/point/delete',
  //     method: 'POST',
  //     body: {
  //       collection_name: 'reai_test',
  //       project: '',
  //       point_id: 'reai_123456-1',
  //     },
  //   });
  //   logger.info('✓ Test passed:', JSON.stringify(result, null, 2));
  // } catch (error) {
  //   console.error('Test failed:', error);
  //   process.exit(1);
  // }

  // 搜索测试
  try {
    const result = await ReAIVolcKnowledge.doRequest<SearchKnowledgeResponse>({
      endpoint: '/api/knowledge/collection/search_knowledge',
      method: 'POST',
      body: {
        name: 'reai_test',
        project: '',
        query: 'switch2隐私政策',
        // limit: 10,
        // query_param: {
        //   doc_filter: {}
        // }
      }
    }) 
    logger.info('✓ Test passed:', JSON.stringify(result, null, 2));
  } catch (error) {
    console.error('Test failed:', error);
    process.exit(1);
  }
})();