/*
 * Copyright (c) 2024 LangChat. TyCoding All Rights Reserved.
 *
 * Licensed under the GNU Affero General Public License, Version 3 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.gnu.org/licenses/agpl-3.0.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cn.iocoder.yudao.module.langchat.controller.admin.aigc;


import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.module.langchat.entity.AigcDocs;
import cn.iocoder.yudao.module.langchat.entity.AigcEmbedStore;
import cn.iocoder.yudao.module.langchat.entity.AigcKnowledge;
import cn.iocoder.yudao.module.langchat.entity.AigcModel;
import cn.iocoder.yudao.module.langchat.mapper.AigcDocsMapper;
import cn.iocoder.yudao.module.langchat.provider.KnowledgeStoreFactory;
import cn.iocoder.yudao.module.langchat.service.aigc.AigcEmbedStoreService;
import cn.iocoder.yudao.module.langchat.service.aigc.AigcKnowledgeService;
import cn.iocoder.yudao.module.langchat.service.aigc.AigcModelService;
import cn.iocoder.yudao.module.langchat.annotation.ApiLog;
import cn.iocoder.yudao.module.langchat.utils.MybatisUtil;
import cn.iocoder.yudao.module.langchat.utils.QueryPage;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author tycoding
 * @since 2024/4/15
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/chat/aigc/knowledge")
public class AigcKnowledgeController {

    private final AigcKnowledgeService kbService;
    private final AigcDocsMapper docsMapper;
    private final AigcEmbedStoreService embedStoreService;
    private final AigcModelService modelService;
    private final KnowledgeStoreFactory knowledgeStore;

    @GetMapping("/list")
    @TenantIgnore
    public CommonResult<List<AigcKnowledge>> list(AigcKnowledge data) {
        List<AigcKnowledge> list = kbService.list(Wrappers.<AigcKnowledge>lambdaQuery().orderByDesc(AigcKnowledge::getCreateTime));
        build(list);
        return CommonResult.success(list);
    }

    private void build(List<AigcKnowledge> records) {
        Map<String, List<AigcEmbedStore>> embedStoreMap = embedStoreService.list().stream().collect(Collectors.groupingBy(AigcEmbedStore::getId));
        Map<String, List<AigcModel>> embedModelMap = modelService.list().stream().collect(Collectors.groupingBy(AigcModel::getId));
        Map<String, List<AigcDocs>> docsMap = docsMapper.selectList(Wrappers.lambdaQuery()).stream().collect(Collectors.groupingBy(AigcDocs::getKnowledgeId));
        records.forEach(item -> {
            List<AigcDocs> docs = docsMap.get(item.getId());
            if (docs != null) {
                item.setDocsNum(docs.size());
                item.setTotalSize(docs.stream().filter(d -> d.getSize() != null).mapToLong(AigcDocs::getSize).sum());
            }
            if (item.getEmbedModelId() != null) {
                List<AigcModel> list = embedModelMap.get(item.getEmbedModelId());
                item.setEmbedModel(list == null ? null : list.get(0));
            }
            if (item.getEmbedStoreId() != null) {
                List<AigcEmbedStore> list = embedStoreMap.get(item.getEmbedStoreId());
                item.setEmbedStore(list == null ? null : list.get(0));
            }
        });
    }

    @GetMapping("/page")
    @TenantIgnore
    public CommonResult list(AigcKnowledge data, QueryPage queryPage) {
        Page<AigcKnowledge> page = new Page<>(queryPage.getPageNo(), queryPage.getPageSize());
        LambdaQueryWrapper<AigcKnowledge> queryWrapper = Wrappers.<AigcKnowledge>lambdaQuery()
                .like(!StrUtil.isBlank(data.getName()), AigcKnowledge::getName, data.getName())
                .orderByDesc(AigcKnowledge::getCreateTime);
        Page<AigcKnowledge> iPage = kbService.page(page, queryWrapper);

        build(iPage.getRecords());

        return CommonResult.success(MybatisUtil.getData(iPage));
    }

    @GetMapping("/{id}")
    @TenantIgnore
    public CommonResult<AigcKnowledge> findById(@PathVariable String id) {
        AigcKnowledge knowledge = kbService.getById(id);
        if (knowledge.getEmbedStoreId() != null) {
            knowledge.setEmbedStore(embedStoreService.getById(knowledge.getEmbedStoreId()));
        }
        if (knowledge.getEmbedModelId() != null) {
            knowledge.setEmbedModel(modelService.getById(knowledge.getEmbedModelId()));
        }
        return CommonResult.success(knowledge);
    }

    @PostMapping
    @ApiLog("新增知识库")
    @TenantIgnore
    @PreAuthorize("@ss.hasPermission('aigc:knowledge:add')")
    public CommonResult add(@RequestBody AigcKnowledge data) {
        data.setCreateTime(String.valueOf(System.currentTimeMillis()));
        kbService.save(data);
        knowledgeStore.init();
        return CommonResult.success("success");
    }

    @PutMapping
    @ApiLog("更新知识库")
    @TenantIgnore
    @PreAuthorize("@ss.hasPermission('aigc:knowledge:update')")
    public CommonResult update(@RequestBody AigcKnowledge data) {
        kbService.updateById(data);
        knowledgeStore.init();
        return CommonResult.success("success");
    }

    @DeleteMapping("/{id}")
    @ApiLog("删除知识库")
    @TenantIgnore
    @PreAuthorize("@ss.hasPermission('aigc:knowledge:delete')")
    public CommonResult delete(@PathVariable String id) {
        kbService.removeKnowledge(id);
        knowledgeStore.init();
        return CommonResult.success("success");
    }
}

