package handler

import (
	"log"
	"net/http"
	"strconv"

	"github.com/gin-gonic/gin"
	"github.com/Nanqipro/YunQue-Tech-Projects/ai_english_learning/serve/internal/common"
	"github.com/Nanqipro/YunQue-Tech-Projects/ai_english_learning/serve/internal/services"
)

// NotificationHandler 通知处理器
type NotificationHandler struct {
	notificationService *services.NotificationService
}

// NewNotificationHandler 创建通知处理器
func NewNotificationHandler(notificationService *services.NotificationService) *NotificationHandler {
	return &NotificationHandler{
		notificationService: notificationService,
	}
}

// GetNotifications 获取通知列表
// @Summary 获取用户通知列表
// @Tags notification
// @Param page query int false "页码" default(1)
// @Param limit query int false "每页数量" default(10)
// @Param only_unread query bool false "只显示未读" default(false)
// @Success 200 {object} common.Response
// @Router /api/v1/notifications [get]
func (h *NotificationHandler) GetNotifications(c *gin.Context) {
	userID := c.GetInt64("user_id")

	// 解析分页参数
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	limit, _ := strconv.Atoi(c.DefaultQuery("limit", "10"))
	onlyUnread, _ := strconv.ParseBool(c.DefaultQuery("only_unread", "false"))

	if page < 1 {
		page = 1
	}
	if limit < 1 || limit > 100 {
		limit = 10
	}

	notifications, total, err := h.notificationService.GetUserNotifications(userID, page, limit, onlyUnread)
	if err != nil {
		log.Printf("[ERROR] 获取通知列表失败: userID=%d, error=%v", userID, err)
		common.ErrorResponse(c, http.StatusInternalServerError, "获取通知列表失败")
		return
	}

	common.SuccessResponse(c, gin.H{
		"notifications": notifications,
		"total":         total,
		"page":          page,
		"limit":         limit,
	})
}

// GetUnreadCount 获取未读通知数量
// @Summary 获取未读通知数量
// @Tags notification
// @Success 200 {object} common.Response
// @Router /api/v1/notifications/unread-count [get]
func (h *NotificationHandler) GetUnreadCount(c *gin.Context) {
	userID := c.GetInt64("user_id")

	count, err := h.notificationService.GetUnreadCount(userID)
	if err != nil {
		log.Printf("[ERROR] 获取未读通知数量失败: userID=%d, error=%v", userID, err)
		common.ErrorResponse(c, http.StatusInternalServerError, "获取未读通知数量失败")
		return
	}

	common.SuccessResponse(c, gin.H{
		"count": count,
	})
}

// MarkAsRead 标记通知为已读
// @Summary 标记通知为已读
// @Tags notification
// @Param id path int true "通知ID"
// @Success 200 {object} common.Response
// @Router /api/v1/notifications/:id/read [put]
func (h *NotificationHandler) MarkAsRead(c *gin.Context) {
	userID := c.GetInt64("user_id")
	notificationID, err := strconv.ParseInt(c.Param("id"), 10, 64)
	if err != nil {
		common.ErrorResponse(c, http.StatusBadRequest, "无效的通知ID")
		return
	}

	if err := h.notificationService.MarkAsRead(userID, notificationID); err != nil {
		log.Printf("[ERROR] 标记通知已读失败: userID=%d, notificationID=%d, error=%v", userID, notificationID, err)
		common.ErrorResponse(c, http.StatusInternalServerError, err.Error())
		return
	}

	common.SuccessResponse(c, nil)
}

// MarkAllAsRead 标记所有通知为已读
// @Summary 标记所有通知为已读
// @Tags notification
// @Success 200 {object} common.Response
// @Router /api/v1/notifications/read-all [put]
func (h *NotificationHandler) MarkAllAsRead(c *gin.Context) {
	userID := c.GetInt64("user_id")

	if err := h.notificationService.MarkAllAsRead(userID); err != nil {
		log.Printf("[ERROR] 标记所有通知已读失败: userID=%d, error=%v", userID, err)
		common.ErrorResponse(c, http.StatusInternalServerError, "标记所有通知已读失败")
		return
	}

	common.SuccessResponse(c, nil)
}

// DeleteNotification 删除通知
// @Summary 删除通知
// @Tags notification
// @Param id path int true "通知ID"
// @Success 200 {object} common.Response
// @Router /api/v1/notifications/:id [delete]
func (h *NotificationHandler) DeleteNotification(c *gin.Context) {
	userID := c.GetInt64("user_id")
	notificationID, err := strconv.ParseInt(c.Param("id"), 10, 64)
	if err != nil {
		common.ErrorResponse(c, http.StatusBadRequest, "无效的通知ID")
		return
	}

	if err := h.notificationService.DeleteNotification(userID, notificationID); err != nil {
		log.Printf("[ERROR] 删除通知失败: userID=%d, notificationID=%d, error=%v", userID, notificationID, err)
		common.ErrorResponse(c, http.StatusInternalServerError, err.Error())
		return
	}

	common.SuccessResponse(c, nil)
}
