"use strict";

const { IndexesService } = require('./indexes.service');

const router = require('express').Router();
/**
 * 存活统计：用户存活情况统计接口
 * @see
 */
/**
 * @api {get} /aiagent/indexes/loader AGENT：文件加载器
 * @apiSampleRequest /api/aiagent/indexes/loader
 * @apiVersion 0.5.0
 * @apiName agentIndexesLoader
 * @apiGroup apig
 * @apiParam {string} [story] 故事ID
 * @apiParam {string} [attach] 附件ID
 * @apiParam {string} [url] 可访问文件地址
 * @apiParam {string} [md5] 当前文件MD5校验码
 * @apiParam {string} [extend] 文件后缀名
 * @apiSuccess {data} data 结果
 * @apiSuccessExample Success-Response:
 *     HTTP/11 200 OK
 *     {
 *      code:200,
 *      data:[
 *          docsIdList:["xxxxxxxxx1","xxxxxxxxx2","xxxxxxxxx3","xxxxxxxxx4"],
 *          docs:[{pageContent:"",metadata:""}]
 *      ]
 * }
 */
 router.post("/loader",async (request,response)=>{
    // 仅URL直接返回分割结果 
    let url = request.body.url || request.params.url || request.query.url 
    let md5 = request.body.md5 || request.params.md5 || request.query.md5 
    let extend = request.body.extend || request.params.extend || request.query.extend 
    if(!url){
        response.status(500)
        response.json({
            code:500,
            error:"请传入参数：url,md5"
        })
        return
    }
    // 含Story存储Document后，返回分割文档ID
    let story = request.body.story || request.params.story || request.query.story 
    let attach = request.body.attach || request.params.attach || request.query.attach 

    // 加载Indexes，开始分割文档
    let indexes = new IndexesService();
    let docs
    let splitDocs
    try{
        docs = await indexes.load(url,extend,md5);
        let splitCount = 0
        if(docs?.length>0){
            splitDocs = await indexes.split(docs)
            splitCount = splitDocs?.length
        }

        response.json({
            code:200,
            data:{
                doc:docs?.[0],
                split:splitDocs,
                docsIdList:[],
                splitCount:splitCount
            }
        })
    }catch(err){
        console.error(err)
    	response.status(500)
        response.json({
            code:500,
            err:err
        })
    }

})

module.exports = router;
