package handler

import (
	"net/http"

	"x-agents/server/internal/model"
	"x-agents/server/internal/service"

	"github.com/gin-gonic/gin"
)

type Neo4jHandler struct {
	service *service.Neo4jService
}

func NewNeo4jHandler(svc *service.Neo4jService) *Neo4jHandler {
	return &Neo4jHandler{service: svc}
}

// @Summary 检查 Neo4j 连接
// @Description 测试 Neo4j 数据库连接是否正常
// @Tags Neo4j
// @Accept json
// @Produce json
// @Param request body model.Neo4jCheckRequest true "Neo4j连接信息"
// @Success 200 {object} map[string]interface{}
// @Failure 400 {object} map[string]string
// @Failure 500 {object} map[string]string
// @Router /neo4j/check [post]
func (h *Neo4jHandler) Check(c *gin.Context) {
	var req model.Neo4jCheckRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	result, err := h.service.Check(req)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, result)
}

// @Summary 获取图谱概览
// @Description 获取 Neo4j 中的图谱概览数据
// @Tags Neo4j
// @Accept json
// @Produce json
// @Param request body model.Neo4jGraphRequest true "图谱查询请求"
// @Success 200 {object} map[string]interface{}
// @Failure 400 {object} map[string]string
// @Failure 500 {object} map[string]string
// @Router /neo4j/graphs [post]
func (h *Neo4jHandler) GetGraphs(c *gin.Context) {
	var req model.Neo4jGraphRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	result, err := h.service.GetGraphs(req)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, result)
}

// @Summary 获取节点列表
// @Description 获取 Neo4j 中的节点详情
// @Tags Neo4j
// @Accept json
// @Produce json
// @Param request body model.Neo4jNodeRequest true "节点查询请求"
// @Success 200 {object} map[string]interface{}
// @Failure 400 {object} map[string]string
// @Failure 500 {object} map[string]string
// @Router /neo4j/nodes [post]
func (h *Neo4jHandler) GetNodes(c *gin.Context) {
	var req model.Neo4jNodeRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	result, err := h.service.GetNodes(req)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, result)
}

// @Summary 获取关系列表
// @Description 获取 Neo4j 中的关系详情
// @Tags Neo4j
// @Accept json
// @Produce json
// @Param request body model.Neo4jRelRequest true "关系查询请求"
// @Success 200 {object} map[string]interface{}
// @Failure 400 {object} map[string]string
// @Failure 500 {object} map[string]string
// @Router /neo4j/relationships [post]
func (h *Neo4jHandler) GetRelationships(c *gin.Context) {
	var req model.Neo4jRelRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	result, err := h.service.GetRelationships(req)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, result)
}
