package handlers

import (
	"net/http"
	"strings"

	"gauth-central/internal/services"

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

type KnowledgeHandler struct {
	knowledgeService *services.KnowledgeService
}

func NewKnowledgeHandler(knowledgeService *services.KnowledgeService) *KnowledgeHandler {
	return &KnowledgeHandler{knowledgeService: knowledgeService}
}

func (h *KnowledgeHandler) GetAllKnowledges(c *gin.Context) {
	resumeIDStr := c.Query("resume_id")
	var resumeID *string
	if resumeIDStr != "" {
		resumeID = &resumeIDStr
	}

	items, err := h.knowledgeService.GetAllKnowledges(resumeID, true)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}
	c.JSON(http.StatusOK, items)
}

func (h *KnowledgeHandler) AdminGetAllKnowledges(c *gin.Context) {
	resumeIDStr := c.Query("resume_id")
	var resumeID *string
	if resumeIDStr != "" {
		resumeID = &resumeIDStr
	}

	items, err := h.knowledgeService.GetAllKnowledges(resumeID, false)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}
	c.JSON(http.StatusOK, items)
}

func (h *KnowledgeHandler) GetKnowledgeByID(c *gin.Context) {
	id := c.Param("id")
	item, err := h.knowledgeService.GetKnowledgeByID(id)
	if err != nil {
		c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
		return
	}
	c.JSON(http.StatusOK, item)
}

func (h *KnowledgeHandler) CreateKnowledge(c *gin.Context) {
	var req struct {
		Title    string `json:"title"`
		ResumeID string `json:"resume_id"`
		IsActive *bool  `json:"is_active"`
	}
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	req.Title = strings.TrimSpace(req.Title)
	if req.Title == "" {
		c.JSON(http.StatusBadRequest, gin.H{"error": "title is required"})
		return
	}

	resumeUUID, err := uuid.Parse(req.ResumeID)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "invalid resume_id"})
		return
	}

	isActive := false
	if req.IsActive != nil {
		isActive = *req.IsActive
	}

	item, err := h.knowledgeService.CreateKnowledge(
		req.Title,
		resumeUUID,
		isActive,
	)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}
	c.JSON(http.StatusCreated, item)
}

func (h *KnowledgeHandler) UpdateKnowledge(c *gin.Context) {
	id := c.Param("id")
	var req struct {
		Title    *string `json:"title"`
		ResumeID *string `json:"resume_id"`
		IsActive *bool   `json:"is_active"`
	}
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	var resumeUUID *uuid.UUID
	if req.ResumeID != nil {
		parsed, err := uuid.Parse(*req.ResumeID)
		if err != nil {
			c.JSON(http.StatusBadRequest, gin.H{"error": "invalid resume_id"})
			return
		}
		resumeUUID = &parsed
	}

	item, err := h.knowledgeService.UpdateKnowledge(
		id,
		req.Title,
		resumeUUID,
		req.IsActive,
	)
	if err != nil {
		status := http.StatusInternalServerError
		if err.Error() == "knowledge not found" {
			status = http.StatusNotFound
		}
		c.JSON(status, gin.H{"error": err.Error()})
		return
	}
	c.JSON(http.StatusOK, item)
}

func (h *KnowledgeHandler) DeleteKnowledge(c *gin.Context) {
	id := c.Param("id")
	if err := h.knowledgeService.DeleteKnowledge(id); err != nil {
		c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
		return
	}
	c.JSON(http.StatusOK, gin.H{"message": "Knowledge deleted successfully"})
}
