package handler

import (
	"net/http"
	"strconv"

	"git.zkcoi.com/zkcoi/meshray/internal/api/dto"
	"git.zkcoi.com/zkcoi/meshray/internal/model"
	"git.zkcoi.com/zkcoi/meshray/internal/service"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

// PolicyHandler 策略管理 Handler
type PolicyHandler struct {
	policyService *service.PolicyService
	logger        *zap.Logger
}

// NewPolicyHandler 创建 Policy Handler
func NewPolicyHandler(policyService *service.PolicyService, logger *zap.Logger) *PolicyHandler {
	return &PolicyHandler{
		policyService: policyService,
		logger:        logger,
	}
}

// ListPolicies 获取策略列表
func (h *PolicyHandler) ListPolicies(c *gin.Context) {
	policies, err := h.policyService.ListPolicies()
	if err != nil {
		h.logger.Error("获取策略列表失败", zap.Error(err))
		c.JSON(http.StatusInternalServerError, gin.H{"error": "获取策略列表失败"})
		return
	}
	// 使用 DTO 批量转换
	respList := dto.ToPolicyResponseList(policies)
	c.JSON(http.StatusOK, gin.H{"data": respList})
}

// GetPolicy 获取策略详情
func (h *PolicyHandler) GetPolicy(c *gin.Context) {
	id, err := strconv.ParseUint(c.Param("id"), 10, 32)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "无效的 ID"})
		return
	}

	policy, err := h.policyService.GetPolicyByID(uint(id))
	if err != nil {
		h.logger.Error("获取策略失败", zap.Error(err))
		c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
		return
	}
	// 使用 DTO 转换
	resp := dto.ToPolicyResponse(policy)
	c.JSON(http.StatusOK, gin.H{"data": resp})
}

// CreatePolicy 创建策略
func (h *PolicyHandler) CreatePolicy(c *gin.Context) {
	var req model.Policy
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "请求参数错误"})
		return
	}

	policy, err := h.policyService.CreatePolicy(&req)
	if err != nil {
		h.logger.Error("创建策略失败", zap.Error(err))
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	h.logger.Info("策略创建成功", zap.String("name", policy.Name))

	// 使用 DTO 转换
	resp := dto.ToPolicyResponse(policy)
	c.JSON(http.StatusOK, gin.H{"data": resp})
}

// UpdatePolicy 更新策略
func (h *PolicyHandler) UpdatePolicy(c *gin.Context) {
	id, err := strconv.ParseUint(c.Param("id"), 10, 32)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "无效的 ID"})
		return
	}

	var updates map[string]interface{}
	if err := c.ShouldBindJSON(&updates); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "请求参数错误"})
		return
	}

	policy, err := h.policyService.UpdatePolicy(uint(id), updates)
	if err != nil {
		h.logger.Error("更新策略失败", zap.Error(err))
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	h.logger.Info("策略更新成功", zap.Uint64("id", policy.ID))

	// 使用 DTO 转换
	resp := dto.ToPolicyResponse(policy)
	c.JSON(http.StatusOK, gin.H{"data": resp})
}

// DeletePolicy 删除策略
func (h *PolicyHandler) DeletePolicy(c *gin.Context) {
	id, err := strconv.ParseUint(c.Param("id"), 10, 32)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "无效的 ID"})
		return
	}

	if err := h.policyService.DeletePolicy(uint(id)); err != nil {
		h.logger.Error("删除策略失败", zap.Error(err))
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	h.logger.Info("策略删除成功", zap.Uint64("id", id))
	c.JSON(http.StatusOK, gin.H{"message": "策略已删除"})
}
