package handler

import (
	"net/http"
	"strconv"

	"github.com/gin-gonic/gin"

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

// ThemeHandler 主题配置处理器
type ThemeHandler struct {
	themeService *service.ThemeService
}

// NewThemeHandler 创建主题配置处理器
func NewThemeHandler(themeService *service.ThemeService) *ThemeHandler {
	return &ThemeHandler{themeService: themeService}
}

// CreateTheme 创建主题
func (h *ThemeHandler) CreateTheme(c *gin.Context) {
	var req service.CreateThemeRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	theme, err := h.themeService.CreateTheme(c.Request.Context(), &req)
	if err != nil {
		handleError(c, err)
		return
	}

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

// UpdateTheme 更新主题
func (h *ThemeHandler) UpdateTheme(c *gin.Context) {
	id, err := strconv.ParseInt(c.Param("id"), 10, 64)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "invalid theme id"})
		return
	}

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

	theme, err := h.themeService.UpdateTheme(c.Request.Context(), id, &req)
	if err != nil {
		handleError(c, err)
		return
	}

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

// DeleteTheme 删除主题
func (h *ThemeHandler) DeleteTheme(c *gin.Context) {
	id, err := strconv.ParseInt(c.Param("id"), 10, 64)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "invalid theme id"})
		return
	}

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

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

// GetTheme 获取主题
func (h *ThemeHandler) GetTheme(c *gin.Context) {
	id, err := strconv.ParseInt(c.Param("id"), 10, 64)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "invalid theme id"})
		return
	}

	theme, err := h.themeService.GetTheme(c.Request.Context(), id)
	if err != nil {
		handleError(c, err)
		return
	}

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

// ListThemes 获取所有主题
func (h *ThemeHandler) ListThemes(c *gin.Context) {
	themes, err := h.themeService.ListThemes(c.Request.Context())
	if err != nil {
		handleError(c, err)
		return
	}

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

// ListAllThemes 获取所有主题（包括禁用的）
func (h *ThemeHandler) ListAllThemes(c *gin.Context) {
	themes, err := h.themeService.ListAllThemes(c.Request.Context())
	if err != nil {
		handleError(c, err)
		return
	}

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

// GetDefaultTheme 获取默认主题
func (h *ThemeHandler) GetDefaultTheme(c *gin.Context) {
	theme, err := h.themeService.GetDefaultTheme(c.Request.Context())
	if err != nil {
		handleError(c, err)
		return
	}

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

// SetDefaultTheme 设置默认主题
func (h *ThemeHandler) SetDefaultTheme(c *gin.Context) {
	id, err := strconv.ParseInt(c.Param("id"), 10, 64)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "invalid theme id"})
		return
	}

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

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

// GetActiveTheme 获取当前生效的主题（公开接口）
func (h *ThemeHandler) GetActiveTheme(c *gin.Context) {
	theme, err := h.themeService.GetActiveTheme(c.Request.Context())
	if err != nil {
		handleError(c, err)
		return
	}

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