package handlers

import (
	"net/http"

	"github.com/gin-gonic/gin"

	"mengyaping-backend/models"
	"mengyaping-backend/services"
)

// WebsiteHandler 网站处理器
type WebsiteHandler struct {
	websiteService *services.WebsiteService
	monitorService *services.MonitorService
}

// NewWebsiteHandler 创建网站处理器
func NewWebsiteHandler() *WebsiteHandler {
	return &WebsiteHandler{
		websiteService: services.NewWebsiteService(),
		monitorService: services.GetMonitorService(),
	}
}

// GetWebsites 获取所有网站状态
func (h *WebsiteHandler) GetWebsites(c *gin.Context) {
	statuses := h.monitorService.GetAllWebsiteStatuses()

	c.JSON(http.StatusOK, models.APIResponse{
		Code:    0,
		Message: "success",
		Data:    statuses,
	})
}

// GetWebsite 获取单个网站状态
func (h *WebsiteHandler) GetWebsite(c *gin.Context) {
	id := c.Param("id")

	status := h.monitorService.GetWebsiteStatus(id)
	if status == nil {
		c.JSON(http.StatusNotFound, models.APIResponse{
			Code:    404,
			Message: "网站不存在",
		})
		return
	}

	c.JSON(http.StatusOK, models.APIResponse{
		Code:    0,
		Message: "success",
		Data:    status,
	})
}

// CreateWebsite 创建网站
func (h *WebsiteHandler) CreateWebsite(c *gin.Context) {
	var req models.CreateWebsiteRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, models.APIResponse{
			Code:    400,
			Message: "参数错误: " + err.Error(),
		})
		return
	}

	website, err := h.websiteService.CreateWebsite(req)
	if err != nil {
		c.JSON(http.StatusInternalServerError, models.APIResponse{
			Code:    500,
			Message: "创建失败: " + err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, models.APIResponse{
		Code:    0,
		Message: "创建成功",
		Data:    website,
	})
}

// UpdateWebsite 更新网站
func (h *WebsiteHandler) UpdateWebsite(c *gin.Context) {
	id := c.Param("id")

	var req models.UpdateWebsiteRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, models.APIResponse{
			Code:    400,
			Message: "参数错误: " + err.Error(),
		})
		return
	}

	website, err := h.websiteService.UpdateWebsite(id, req)
	if err != nil {
		c.JSON(http.StatusInternalServerError, models.APIResponse{
			Code:    500,
			Message: "更新失败: " + err.Error(),
		})
		return
	}

	if website == nil {
		c.JSON(http.StatusNotFound, models.APIResponse{
			Code:    404,
			Message: "网站不存在",
		})
		return
	}

	c.JSON(http.StatusOK, models.APIResponse{
		Code:    0,
		Message: "更新成功",
		Data:    website,
	})
}

// DeleteWebsite 删除网站
func (h *WebsiteHandler) DeleteWebsite(c *gin.Context) {
	id := c.Param("id")

	if err := h.websiteService.DeleteWebsite(id); err != nil {
		c.JSON(http.StatusInternalServerError, models.APIResponse{
			Code:    500,
			Message: "删除失败: " + err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, models.APIResponse{
		Code:    0,
		Message: "删除成功",
	})
}

// CheckWebsiteNow 立即检测网站
func (h *WebsiteHandler) CheckWebsiteNow(c *gin.Context) {
	id := c.Param("id")

	website := h.websiteService.GetWebsite(id)
	if website == nil {
		c.JSON(http.StatusNotFound, models.APIResponse{
			Code:    404,
			Message: "网站不存在",
		})
		return
	}

	h.monitorService.CheckWebsiteNow(id)

	c.JSON(http.StatusOK, models.APIResponse{
		Code:    0,
		Message: "检测任务已提交",
	})
}

// GetGroups 获取所有分组
func (h *WebsiteHandler) GetGroups(c *gin.Context) {
	groups := h.websiteService.GetGroups()

	c.JSON(http.StatusOK, models.APIResponse{
		Code:    0,
		Message: "success",
		Data:    groups,
	})
}

// GetGroup 获取单个分组
func (h *WebsiteHandler) GetGroup(c *gin.Context) {
	id := c.Param("id")
	g := h.websiteService.GetGroup(id)
	if g == nil {
		c.JSON(http.StatusNotFound, models.APIResponse{
			Code:    404,
			Message: "分类不存在",
		})
		return
	}

	c.JSON(http.StatusOK, models.APIResponse{
		Code:    0,
		Message: "success",
		Data:    g,
	})
}

// UpdateGroup 更新分组
func (h *WebsiteHandler) UpdateGroup(c *gin.Context) {
	id := c.Param("id")

	var req struct {
		Name string `json:"name" binding:"required"`
	}
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, models.APIResponse{
			Code:    400,
			Message: "参数错误: " + err.Error(),
		})
		return
	}

	group, err := h.websiteService.UpdateGroup(id, req.Name)
	if err != nil {
		c.JSON(http.StatusBadRequest, models.APIResponse{
			Code:    400,
			Message: err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, models.APIResponse{
		Code:    0,
		Message: "更新成功",
		Data:    group,
	})
}

// DeleteGroup 删除分组
func (h *WebsiteHandler) DeleteGroup(c *gin.Context) {
	id := c.Param("id")

	if err := h.websiteService.DeleteGroup(id); err != nil {
		c.JSON(http.StatusBadRequest, models.APIResponse{
			Code:    400,
			Message: err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, models.APIResponse{
		Code:    0,
		Message: "删除成功",
	})
}

// AddGroup 添加分组
func (h *WebsiteHandler) AddGroup(c *gin.Context) {
	var req struct {
		Name string `json:"name" binding:"required"`
	}

	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, models.APIResponse{
			Code:    400,
			Message: "参数错误: " + err.Error(),
		})
		return
	}

	group, err := h.websiteService.AddGroup(req.Name)
	if err != nil {
		c.JSON(http.StatusInternalServerError, models.APIResponse{
			Code:    500,
			Message: "添加失败: " + err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, models.APIResponse{
		Code:    0,
		Message: "添加成功",
		Data:    group,
	})
}
