package service

import (
	"metazone.cc/metalab/internal/common"
	"metazone.cc/metalab/internal/model"
)

// notifRepo 通知服务所需的最小仓储接口（ISP）
type notifRepo interface {
	Create(n *model.Notification) error
	ListByUser(userID uint, offset, limit int) ([]model.Notification, error)
	CountByUser(userID uint) (int64, error)
	CountUnread(userID uint) (int64, error)
	MarkRead(id, userID uint) error
	MarkAllRead(userID uint) error
}

// NotificationService 消息/通知业务逻辑
type NotificationService struct {
	repo notifRepo
}

// NewNotificationService 构造函数
func NewNotificationService(repo notifRepo) *NotificationService {
	return &NotificationService{repo: repo}
}

// Create 创建消息
func (s *NotificationService) Create(userID uint, notifyType, title, content string, relatedID *uint) error {
	n := &model.Notification{
		UserID:     userID,
		NotifyType: notifyType,
		Title:      title,
		Content:    content,
		IsRead:     false,
		RelatedID:  relatedID,
	}
	return s.repo.Create(n)
}

// List 分页查询用户消息列表
func (s *NotificationService) List(userID uint, page, pageSize int) (*model.NotificationListResult, error) {
	p := common.Pagination{Page: page, PageSize: pageSize}
	p.DefaultPagination()

	total, err := s.repo.CountByUser(userID)
	if err != nil {
		return nil, err
	}

	items, err := s.repo.ListByUser(userID, p.Offset(), p.PageSize)
	if err != nil {
		return nil, err
	}
	if items == nil {
		items = []model.Notification{}
	}

	totalPages := int((total + int64(p.PageSize) - 1) / int64(p.PageSize))
	unread, _ := s.CountUnread(userID)

	return &model.NotificationListResult{
		Items:      items,
		Total:      total,
		Page:       p.Page,
		TotalPages: totalPages,
		Unread:     unread,
	}, nil
}

// CountUnread 统计未读消息数
func (s *NotificationService) CountUnread(userID uint) (int64, error) {
	return s.repo.CountUnread(userID)
}

// MarkRead 标记单条消息为已读
func (s *NotificationService) MarkRead(id, userID uint) error {
	return s.repo.MarkRead(id, userID)
}

// MarkAllRead 标记所有未读为已读
func (s *NotificationService) MarkAllRead(userID uint) error {
	return s.repo.MarkAllRead(userID)
}

// NotifyAuditApproved 审核通过通知
func (s *NotificationService) NotifyAuditApproved(userID uint, auditType string, relatedID uint) {
	typeName := model.AuditTypeNames[auditType]
	if typeName == "" {
		typeName = auditType
	}
	_ = s.Create(userID, model.NotifyAuditApproved,
		"审核已通过",
		"你的"+typeName+"修改已通过审核",
		&relatedID)
}

// NotifyAuditRejected 审核拒绝通知
func (s *NotificationService) NotifyAuditRejected(userID uint, auditType, reason string, relatedID uint) {
	typeName := model.AuditTypeNames[auditType]
	if typeName == "" {
		typeName = auditType
	}
	content := "你的" + typeName + "修改被拒绝"
	if reason != "" {
		content += "，理由：" + reason
	}
	_ = s.Create(userID, model.NotifyAuditRejected,
		"审核未通过",
		content,
		&relatedID)
}

// NotifyPostApproved 稿件审核通过通知
func (s *NotificationService) NotifyPostApproved(userID uint, postTitle string, postID uint) {
	title := "稿件审核通过"
	content := "你的稿件《" + postTitle + "》已通过审核，现已公开发布"
	_ = s.Create(userID, model.NotifyPostApproved, title, content, &postID)
}

// NotifyPostRejected 稿件审核拒绝通知
func (s *NotificationService) NotifyPostRejected(userID uint, postTitle, reason string, postID uint) {
	title := "稿件审核未通过"
	content := "你的稿件《" + postTitle + "》未通过审核"
	if reason != "" {
		content += "，理由：" + reason
	}
	_ = s.Create(userID, model.NotifyPostRejected, title, content, &postID)
}
