package handler

import (
	"net/http"
	"strconv"

	"github.com/gin-gonic/gin"

	"github.com/user-management-system/internal/service"
)

// CustomFieldHandler 自定义字段处理器
type CustomFieldHandler struct {
	customFieldService *service.CustomFieldService
}

// NewCustomFieldHandler 创建自定义字段处理器
func NewCustomFieldHandler(customFieldService *service.CustomFieldService) *CustomFieldHandler {
	return &CustomFieldHandler{customFieldService: customFieldService}
}

// CreateField 创建自定义字段
func (h *CustomFieldHandler) CreateField(c *gin.Context) {
	var req service.CreateFieldRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	field, err := h.customFieldService.CreateField(c.Request.Context(), &req)
	if err != nil {
		handleError(c, err)
		return
	}

	c.JSON(http.StatusCreated, gin.H{
		"code":    0,
		"message": "success",
		"data":    field,
	})
}

// UpdateField 更新自定义字段
func (h *CustomFieldHandler) UpdateField(c *gin.Context) {
	id, err := strconv.ParseInt(c.Param("id"), 10, 64)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "invalid field id"})
		return
	}

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

	field, err := h.customFieldService.UpdateField(c.Request.Context(), id, &req)
	if err != nil {
		handleError(c, err)
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    0,
		"message": "success",
		"data":    field,
	})
}

// DeleteField 删除自定义字段
func (h *CustomFieldHandler) DeleteField(c *gin.Context) {
	id, err := strconv.ParseInt(c.Param("id"), 10, 64)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "invalid field id"})
		return
	}

	if err := h.customFieldService.DeleteField(c.Request.Context(), id); err != nil {
		handleError(c, err)
		return
	}

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

// GetField 获取自定义字段
func (h *CustomFieldHandler) GetField(c *gin.Context) {
	id, err := strconv.ParseInt(c.Param("id"), 10, 64)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "invalid field id"})
		return
	}

	field, err := h.customFieldService.GetField(c.Request.Context(), id)
	if err != nil {
		handleError(c, err)
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    0,
		"message": "success",
		"data":    field,
	})
}

// ListFields 获取所有自定义字段
func (h *CustomFieldHandler) ListFields(c *gin.Context) {
	fields, err := h.customFieldService.ListFields(c.Request.Context())
	if err != nil {
		handleError(c, err)
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    0,
		"message": "success",
		"data":    fields,
	})
}

// SetUserFieldValues 设置用户自定义字段值
func (h *CustomFieldHandler) SetUserFieldValues(c *gin.Context) {
	userID, ok := getUserIDFromContext(c)
	if !ok {
		c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"})
		return
	}

	var req struct {
		Values map[string]string `json:"values" binding:"required"`
	}

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

	if err := h.customFieldService.BatchSetUserFieldValues(c.Request.Context(), userID, req.Values); err != nil {
		handleError(c, err)
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    0,
		"message": "field values set",
	})
}

// GetUserFieldValues 获取用户自定义字段值
func (h *CustomFieldHandler) GetUserFieldValues(c *gin.Context) {
	userID, ok := getUserIDFromContext(c)
	if !ok {
		c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"})
		return
	}

	values, err := h.customFieldService.GetUserFieldValues(c.Request.Context(), userID)
	if err != nil {
		handleError(c, err)
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    0,
		"message": "success",
		"data":    values,
	})
}
