package com.lingyue.graph.controller;

import com.lingyue.common.domain.AjaxResult;
import com.lingyue.graph.dto.KnowledgeGraphDTO;
import com.lingyue.graph.service.KnowledgeGraphService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * 知识图谱控制器
 * 
 * 提供原型中"标记要素关系图谱"功能所需的 API
 * - 图谱视图：节点 + 关系的可视化数据
 * - 列表视图：按类型分组的实体列表
 * 
 * @author lingyue
 * @since 2026-01-21
 */
@Slf4j
@RestController
@RequestMapping("/api/v1/graph")
@RequiredArgsConstructor
@Tag(name = "知识图谱", description = "知识图谱可视化接口")
public class KnowledgeGraphController {
    
    private final KnowledgeGraphService knowledgeGraphService;
    
    /**
     * 获取文档的知识图谱数据
     * 用于图谱视图渲染
     */
    @GetMapping("/documents/{documentId}")
    @Operation(summary = "获取文档图谱", description = "获取文档的知识图谱数据（节点+关系）")
    public AjaxResult<?> getDocumentGraph(
            @Parameter(description = "文档ID", required = true)
            @PathVariable String documentId) {
        
        KnowledgeGraphDTO graph = knowledgeGraphService.getDocumentGraph(documentId);
        if (graph == null) {
            return AjaxResult.error("文档不存在或无图谱数据");
        }
        
        return AjaxResult.success(graph);
    }
    
    /**
     * 获取文档的实体列表（按类型分组）
     * 用于列表视图渲染
     */
    @GetMapping("/documents/{documentId}/entities")
    @Operation(summary = "获取实体列表", description = "获取文档的实体列表，按类型分组")
    public AjaxResult<?> getEntityList(
            @Parameter(description = "文档ID", required = true)
            @PathVariable String documentId,
            @Parameter(description = "类型筛选")
            @RequestParam(required = false) String type) {
        
        var entities = knowledgeGraphService.getEntityListGroupedByType(documentId, type);
        return AjaxResult.success(entities);
    }
    
    /**
     * 获取实体详情（包含关联实体）
     */
    @GetMapping("/entities/{entityId}")
    @Operation(summary = "获取实体详情", description = "获取实体详情及其关联实体")
    public AjaxResult<?> getEntityDetail(
            @Parameter(description = "实体ID", required = true)
            @PathVariable String entityId) {
        
        var detail = knowledgeGraphService.getEntityDetail(entityId);
        if (detail == null) {
            return AjaxResult.error("实体不存在");
        }
        
        return AjaxResult.success(detail);
    }
    
    /**
     * 获取用户的全局知识图谱（跨文档）
     */
    @GetMapping("/users/{userId}")
    @Operation(summary = "获取用户图谱", description = "获取用户的全局知识图谱（跨文档）")
    public AjaxResult<?> getUserGraph(
            @Parameter(description = "用户ID", required = true)
            @PathVariable String userId,
            @Parameter(description = "限制节点数量")
            @RequestParam(defaultValue = "100") Integer limit) {
        
        KnowledgeGraphDTO graph = knowledgeGraphService.getUserGraph(userId, limit);
        return AjaxResult.success(graph);
    }
    
    /**
     * 搜索实体
     */
    @GetMapping("/search")
    @Operation(summary = "搜索实体", description = "按名称搜索实体")
    public AjaxResult<?> searchEntities(
            @Parameter(description = "搜索关键词", required = true)
            @RequestParam String keyword,
            @Parameter(description = "文档ID（可选，限定范围）")
            @RequestParam(required = false) String documentId,
            @Parameter(description = "限制数量")
            @RequestParam(defaultValue = "20") Integer limit) {
        
        var results = knowledgeGraphService.searchEntities(keyword, documentId, limit);
        return AjaxResult.success(results);
    }
}
