package com.wok.supportbot.controller;
import com.wok.supportbot.entity.LlmCallTrace;
import com.wok.supportbot.service.LlmCallTraceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* LLM 调用追踪接口(仅 admin 可访问)。
*
* 提供调用记录的分页查询、详情、聚合统计与清理。
* 端点仅挂管理路径(/llm-trace),绝不进入 /ai/** 或 /open-api/**(SDK 开放路径),
* 防止 conversation_id 跨租户越权。
*/
@Slf4j
@RestController
@RequestMapping("/llm-trace")
@PreAuthorize("hasRole('admin')")
public class LlmCallTraceController {
@Autowired
private LlmCallTraceService llmCallTraceService;
/** 清理的最小保留天数(防止误清全部) */
private static final int MIN_KEEP_DAYS = 1;
/** 清理的最大保留天数(防止超过 PostgreSQL make_interval 合理范围) */
private static final int MAX_KEEP_DAYS = 3650;
/**
* 分页查询调用记录(列表不含大 TEXT 字段)。
*/
@GetMapping("/list")
public ResponseEntity