package org.dromara.knowledge.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.knowledge.domain.bo.KnowledgeHitLogBo;
import org.dromara.knowledge.domain.vo.KnowledgeHitLogVo;
import org.dromara.knowledge.service.KnowledgeHitLogService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 知识库命中日志控制器
 *
 * @author pengles
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/knowledge/hitlog")
public class KnowledgeHitLogController extends BaseController {

    private final KnowledgeHitLogService knowledgeHitLogService;

    /**
     * 查询知识库命中日志列表
     */
    @SaCheckPermission("knowledge:hitlog:list")
    @GetMapping("/list")
    public TableDataInfo<KnowledgeHitLogVo> list(KnowledgeHitLogBo bo, PageQuery pageQuery) {
        return knowledgeHitLogService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出知识库命中日志列表
     */
    @SaCheckPermission("knowledge:hitlog:export")
    @Log(title = "知识库命中日志", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(KnowledgeHitLogBo bo, HttpServletResponse response) {
        List<KnowledgeHitLogVo> list = knowledgeHitLogService.queryList(bo);
        ExcelUtil.exportExcel(list, "知识库命中日志", KnowledgeHitLogVo.class, response);
    }

    /**
     * 获取知识库命中日志详细信息
     *
     * @param logId 日志ID
     */
    @SaCheckPermission("knowledge:hitlog:query")
    @GetMapping("/{logId}")
    public R<KnowledgeHitLogVo> getInfo(@PathVariable Long logId) {
        return R.ok(knowledgeHitLogService.queryById(logId));
    }

    /**
     * 新增知识库命中日志
     */
    @SaCheckPermission("knowledge:hitlog:add")
    @Log(title = "知识库命中日志", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Void> add(@Validated @RequestBody KnowledgeHitLogBo bo) {
        return toAjax(knowledgeHitLogService.insertByBo(bo));
    }

    /**
     * 修改知识库命中日志
     */
    @SaCheckPermission("knowledge:hitlog:edit")
    @Log(title = "知识库命中日志", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@Validated @RequestBody KnowledgeHitLogBo bo) {
        return toAjax(knowledgeHitLogService.updateByBo(bo));
    }

    /**
     * 删除知识库命中日志
     *
     * @param logIds 日志ID数组
     */
    @SaCheckPermission("knowledge:hitlog:remove")
    @Log(title = "知识库命中日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/{logIds}")
    public R<Void> remove(@PathVariable Long[] logIds) {
        return toAjax(knowledgeHitLogService.deleteWithValidByIds(List.of(logIds), true));
    }

    /**
     * 根据知识库ID查询命中日志列表
     *
     * @param knowledgeId 知识库ID
     */
    @SaCheckPermission("knowledge:hitlog:query")
    @GetMapping("/knowledge/{knowledgeId}")
    public R<List<KnowledgeHitLogVo>> getByKnowledgeId(@PathVariable String knowledgeId) {
        return R.ok(knowledgeHitLogService.queryByKnowledgeId(knowledgeId));
    }

    /**
     * 根据会话ID查询命中日志列表
     *
     * @param sessionId 会话ID
     */
    @SaCheckPermission("knowledge:hitlog:query")
    @GetMapping("/session/{sessionId}")
    public R<List<KnowledgeHitLogVo>> getBySessionId(@PathVariable String sessionId) {
        return R.ok(knowledgeHitLogService.queryBySessionId(sessionId));
    }

    /**
     * 根据片段ID查询命中日志列表
     *
     * @param fragmentId 片段ID
     */
    @SaCheckPermission("knowledge:hitlog:query")
    @GetMapping("/fragment/{fragmentId}")
    public R<List<KnowledgeHitLogVo>> getByFragmentId(@PathVariable String fragmentId) {
        return R.ok(knowledgeHitLogService.queryByFragmentId(fragmentId));
    }

    /**
     * 根据查询文本查询命中日志列表
     *
     * @param queryText 查询文本
     */
    @SaCheckPermission("knowledge:hitlog:query")
    @GetMapping("/query/{queryText}")
    public R<List<KnowledgeHitLogVo>> getByQueryText(@PathVariable String queryText) {
        return R.ok(knowledgeHitLogService.queryByQueryText(queryText));
    }

    /**
     * 获取热门查询列表
     *
     * @param knowledgeId 知识库ID
     * @param limit       限制数量
     */
    @SaCheckPermission("knowledge:hitlog:query")
    @GetMapping("/popular")
    public R<java.util.List<java.util.Map<String, Object>>> getPopularQueries(
            @RequestParam(required = false) String knowledgeId,
            @RequestParam(defaultValue = "10") Integer limit) {
        return R.ok(knowledgeHitLogService.getPopularQueries(knowledgeId, limit));
    }

    /**
     * 获取平均响应时间
     *
     * @param knowledgeId 知识库ID
     */
    @SaCheckPermission("knowledge:hitlog:query")
    @GetMapping("/avg-response-time")
    public R<Double> getAverageResponseTime(@RequestParam(required = false) String knowledgeId) {
        return R.ok(knowledgeHitLogService.getAverageResponseTime(knowledgeId));
    }
}