package com.lingyue.graph.controller;

import com.lingyue.common.domain.AjaxResult;
import com.lingyue.graph.dto.KnowledgeGraphDTO;
import com.lingyue.graph.neo4j.Neo4jGraphService;
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.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Neo4j 图数据库控制器
 * 
 * 提供基于 Neo4j 的高级图查询功能
 * 仅在 neo4j.enabled=true 时启用
 * 
 * @author lingyue
 * @since 2026-01-21
 */
@Slf4j
@RestController
@RequestMapping("/api/v1/neo4j")
@RequiredArgsConstructor
@ConditionalOnProperty(name = "neo4j.enabled", havingValue = "true")
@Tag(name = "Neo4j 图数据库", description = "Neo4j 图查询接口")
public class Neo4jGraphController {
    
    private final Neo4jGraphService neo4jGraphService;
    
    /**
     * 获取文档的知识图谱（从 Neo4j）
     */
    @GetMapping("/documents/{documentId}/graph")
    @Operation(summary = "获取文档图谱", description = "从 Neo4j 获取文档的知识图谱")
    public AjaxResult<?> getDocumentGraph(
            @Parameter(description = "文档ID", required = true)
            @PathVariable String documentId) {
        
        KnowledgeGraphDTO graph = neo4jGraphService.getDocumentGraph(documentId);
        if (graph == null) {
            return AjaxResult.error("文档不存在或无图谱数据");
        }
        
        return AjaxResult.success(graph);
    }
    
    /**
     * 查找最短路径
     */
    @GetMapping("/path")
    @Operation(summary = "查找最短路径", description = "查找两个节点之间的最短路径")
    public AjaxResult<?> findShortestPath(
            @Parameter(description = "起始节点ID", required = true)
            @RequestParam String fromNodeId,
            @Parameter(description = "目标节点ID", required = true)
            @RequestParam String toNodeId,
            @Parameter(description = "最大深度")
            @RequestParam(defaultValue = "5") int maxDepth) {
        
        List<Map<String, Object>> path = neo4jGraphService.findShortestPath(fromNodeId, toNodeId, maxDepth);
        return AjaxResult.success(path);
    }
    
    /**
     * 获取节点邻域图
     */
    @GetMapping("/nodes/{nodeId}/neighborhood")
    @Operation(summary = "获取邻域图", description = "获取节点的 N 跳邻居形成的子图")
    public AjaxResult<?> getNeighborhood(
            @Parameter(description = "节点ID", required = true)
            @PathVariable String nodeId,
            @Parameter(description = "邻域深度")
            @RequestParam(defaultValue = "2") int depth) {
        
        KnowledgeGraphDTO graph = neo4jGraphService.getNeighborhood(nodeId, depth);
        return AjaxResult.success(graph);
    }
    
    /**
     * 获取核心节点（按关联度排序）
     */
    @GetMapping("/documents/{documentId}/top-nodes")
    @Operation(summary = "获取核心节点", description = "获取文档中最重要的节点（按关联度）")
    public AjaxResult<?> getTopNodes(
            @Parameter(description = "文档ID", required = true)
            @PathVariable String documentId,
            @Parameter(description = "返回数量")
            @RequestParam(defaultValue = "10") int limit) {
        
        List<Map<String, Object>> nodes = neo4jGraphService.getTopNodesByPageRank(documentId, limit);
        return AjaxResult.success(nodes);
    }
    
    /**
     * 删除文档图数据
     */
    @DeleteMapping("/documents/{documentId}")
    @Operation(summary = "删除文档图数据", description = "删除文档在 Neo4j 中的所有节点和关系")
    public AjaxResult<?> deleteDocumentGraph(
            @Parameter(description = "文档ID", required = true)
            @PathVariable String documentId) {
        
        neo4jGraphService.deleteDocumentGraph(documentId);
        return AjaxResult.success("删除成功");
    }
    
    /**
     * 初始化索引
     */
    @PostMapping("/init-indexes")
    @Operation(summary = "初始化索引", description = "创建 Neo4j 数据库索引")
    public AjaxResult<?> initializeIndexes() {
        neo4jGraphService.initializeIndexes();
        return AjaxResult.success("索引初始化完成");
    }
}
