package handler

import (
	"net/http"

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

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

// MCPHandler MCP工具处理器
type MCPHandler struct {
	mcpService *service.MCPService
}

// NewMCPHandler 创建MCP处理器
func NewMCPHandler(mcpService *service.MCPService) *MCPHandler {
	return &MCPHandler{mcpService: mcpService}
}

// List 获取MCP列表
// @Summary 获取MCP列表
// @Description 获取所有MCP工具配置
// @Tags MCP管理
// @Accept json
// @Produce json
// @Param category query string false "分类"
// @Success 200 {object} map[string]interface{}
// @Router /mcp/list [get]
func (h *MCPHandler) List(c *gin.Context) {
	category := c.Query("category")

	var mcps []model.MCP
	var err error

	if category != "" {
		mcps, err = h.mcpService.GetMCPsByCategory(category)
	} else {
		mcps, err = h.mcpService.GetAllMCPs()
	}

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

	c.JSON(http.StatusOK, gin.H{"list": mcps, "total": len(mcps)})
}

// GetByID 获取MCP详情
// @Summary 获取MCP详情
// @Description 根据ID获取MCP配置
// @Tags MCP管理
// @Accept json
// @Produce json
// @Param id path string true "MCP ID"
// @Success 200 {object} map[string]interface{}
// @Router /mcp/{id} [get]
func (h *MCPHandler) GetByID(c *gin.Context) {
	id := c.Param("id")
	if id == "" {
		c.JSON(http.StatusBadRequest, gin.H{"error": "mcp id is required"})
		return
	}

	mcp, err := h.mcpService.GetMCPByID(id)
	if err != nil {
		c.JSON(http.StatusNotFound, gin.H{"error": "mcp not found"})
		return
	}

	c.JSON(http.StatusOK, gin.H{"mcp": mcp})
}

// Create 创建MCP
// @Summary 创建MCP
// @Description 创建新的MCP工具配置
// @Tags MCP管理
// @Accept json
// @Produce json
// @Param mcp body model.MCP true "MCP信息"
// @Success 200 {object} map[string]interface{}
// @Router /mcp/add [post]
func (h *MCPHandler) Create(c *gin.Context) {
	var mcp model.MCP
	if err := c.ShouldBindJSON(&mcp); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	if err := h.mcpService.CreateMCP(&mcp); err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, gin.H{"message": "mcp created", "mcp": mcp})
}

// Update 更新MCP
// @Summary 更新MCP
// @Description 更新MCP配置
// @Tags MCP管理
// @Accept json
// @Produce json
// @Param id path string true "MCP ID"
// @Param mcp body model.MCP true "MCP信息"
// @Success 200 {object} map[string]interface{}
// @Router /mcp/{id} [put]
func (h *MCPHandler) Update(c *gin.Context) {
	id := c.Param("id")
	if id == "" {
		c.JSON(http.StatusBadRequest, gin.H{"error": "mcp id is required"})
		return
	}

	var mcp model.MCP
	if err := c.ShouldBindJSON(&mcp); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	mcp.ID = id
	if err := h.mcpService.UpdateMCP(&mcp); err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, gin.H{"message": "mcp updated"})
}

// Delete 删除MCP
// @Summary 删除MCP
// @Description 删除MCP配置
// @Tags MCP管理
// @Accept json
// @Produce json
// @Param id path string true "MCP ID"
// @Success 200 {object} map[string]interface{}
// @Router /mcp/{id} [delete]
func (h *MCPHandler) Delete(c *gin.Context) {
	id := c.Param("id")
	if id == "" {
		c.JSON(http.StatusBadRequest, gin.H{"error": "mcp id is required"})
		return
	}

	if err := h.mcpService.DeleteMCP(id); err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, gin.H{"message": "mcp deleted"})
}
