package handler

import (
	"net/http"
	"strconv"

	"github.com/gin-gonic/gin"
	"x-agents/server/internal/model"
	"x-agents/server/internal/service"
)

type KnowledgeHandler struct {
	service *service.KnowledgeService
}

func NewKnowledgeHandler(s *service.KnowledgeService) *KnowledgeHandler {
	return &KnowledgeHandler{service: s}
}

// Create 创建知识库
func (h *KnowledgeHandler) Create(c *gin.Context) {
	var req model.CreateKnowledgeRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"success": false, "message": err.Error()})
		return
	}

	kb, err := h.service.Create(req)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"success": false, "message": err.Error()})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"success": true,
		"id":     kb.ID,
		"message": "Knowledge base created successfully",
	})
}

// List 获取知识库列表
func (h *KnowledgeHandler) List(c *gin.Context) {
	list, err := h.service.List()
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"success": false, "message": err.Error()})
		return
	}

	c.JSON(http.StatusOK, gin.H{"success": true, "data": list})
}

// GetByID 获取知识库详情
func (h *KnowledgeHandler) GetByID(c *gin.Context) {
	id := c.Param("id")
	if id == "" {
		c.JSON(http.StatusBadRequest, gin.H{"success": false, "message": "id is required"})
		return
	}

	kb, err := h.service.GetByID(id)
	if err != nil {
		c.JSON(http.StatusNotFound, gin.H{"success": false, "message": "Knowledge base not found"})
		return
	}

	c.JSON(http.StatusOK, gin.H{"success": true, "data": kb})
}

// Update 更新知识库
func (h *KnowledgeHandler) Update(c *gin.Context) {
	id := c.Param("id")
	if id == "" {
		c.JSON(http.StatusBadRequest, gin.H{"success": false, "message": "id is required"})
		return
	}

	var req model.UpdateKnowledgeRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"success": false, "message": err.Error()})
		return
	}

	if err := h.service.Update(id, req); err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"success": false, "message": err.Error()})
		return
	}

	c.JSON(http.StatusOK, gin.H{"success": true, "message": "Knowledge base updated"})
}

// Delete 删除知识库
func (h *KnowledgeHandler) Delete(c *gin.Context) {
	id := c.Param("id")
	if id == "" {
		c.JSON(http.StatusBadRequest, gin.H{"success": false, "message": "id is required"})
		return
	}

	if err := h.service.Delete(id); err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"success": false, "message": err.Error()})
		return
	}

	c.JSON(http.StatusOK, gin.H{"success": true, "message": "Knowledge base deleted"})
}

// ListDocuments 获取知识库下的文档列表
func (h *KnowledgeHandler) ListDocuments(c *gin.Context) {
	id := c.Param("id")
	if id == "" {
		c.JSON(http.StatusBadRequest, gin.H{"success": false, "message": "id is required"})
		return
	}

	status := c.Query("status")
	list, err := h.service.ListDocuments(id, status)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"success": false, "message": err.Error()})
		return
	}

	c.JSON(http.StatusOK, gin.H{"success": true, "data": list})
}

// UploadDocument 上传文档到知识库
func (h *KnowledgeHandler) UploadDocument(c *gin.Context) {
	id := c.Param("id")
	if id == "" {
		c.JSON(http.StatusBadRequest, gin.H{"success": false, "message": "id is required"})
		return
	}

	file, err := c.FormFile("file")
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"success": false, "message": "No file uploaded"})
		return
	}

	// 检查文件大小（最大 100MB）
	if file.Size > 100*1024*1024 {
		c.JSON(http.StatusBadRequest, gin.H{"success": false, "message": "File too large (max 100MB)"})
		return
	}

	doc, fileURL, err := h.service.UploadDocument(id, file)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"success": false, "message": err.Error()})
		return
	}
	if doc == nil {
		c.JSON(http.StatusInternalServerError, gin.H{"success": false, "message": "Upload failed"})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"success":  true,
		"id":       doc.ID,
		"url":      fileURL,
		"document": doc,
		"message":  "Document uploaded",
	})
}

// DeleteDocument 删除文档
func (h *KnowledgeHandler) DeleteDocument(c *gin.Context) {
	id := c.Param("id")
	docID := c.Param("doc_id")

	if id == "" || docID == "" {
		c.JSON(http.StatusBadRequest, gin.H{"success": false, "message": "id and doc_id are required"})
		return
	}

	if err := h.service.DeleteDocument(id, docID); err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"success": false, "message": err.Error()})
		return
	}

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

// ReparseDocument 重新解析文档
func (h *KnowledgeHandler) ReparseDocument(c *gin.Context) {
	id := c.Param("id")
	docID := c.Param("doc_id")

	if id == "" || docID == "" {
		c.JSON(http.StatusBadRequest, gin.H{"success": false, "message": "id and doc_id are required"})
		return
	}

	if err := h.service.ReparseDocument(id, docID); err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"success": false, "message": err.Error()})
		return
	}

	c.JSON(http.StatusOK, gin.H{"success": true, "message": "Document reparse started"})
}

// GetDocumentPreview 获取文档预览
func (h *KnowledgeHandler) GetDocumentPreview(c *gin.Context) {
	id := c.Param("id")
	docID := c.Param("doc_id")

	if id == "" || docID == "" {
		c.JSON(http.StatusBadRequest, gin.H{"success": false, "message": "id and doc_id are required"})
		return
	}

	page := 1
	if p := c.Query("page"); p != "" {
		if parsed, err := strconv.Atoi(p); err == nil {
			page = parsed
		}
	}

	preview, err := h.service.GetDocumentPreview(id, docID, page)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"success": false, "message": err.Error()})
		return
	}

	c.JSON(http.StatusOK, gin.H{"success": true, "data": preview})
}
