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}
}

// Check 检查 Neo4j 连接
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)
}

// GetGraphs 获取图谱概览数据
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)
}

// GetNodes 获取节点详情
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)
}

// GetRelationships 获取关系详情
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)
}
