package service

import (
	"fmt"
	"sort"

	"metazone.cc/metalab/internal/model"
)

// checkAndNotifyAggregation 检查并生成点赞聚合通知
func (s *NotificationService) checkAndNotifyAggregation(userID uint) {
	summaries, err := s.repo.GetUnnotifiedDailyLikeSummaries(userID)
	if err != nil || len(summaries) == 0 {
		return
	}

	for _, sm := range summaries {
		count := countUIDs(sm.LikerUIDs)
		var content string
		if count <= 3 {
			content = fmt.Sprintf("你的文章被 %d 人点赞了", count)
		} else {
			content = fmt.Sprintf("你的文章被 %d 人点赞了", count)
		}
		_ = s.Create(userID, model.NotifyLikeAggregated,
			"点赞汇总通知",
			content,
			nil, nil)
		_ = s.repo.MarkDailyLikeSummaryNotified(sm.ID)
	}
}

func countUIDs(uids string) int {
	if uids == "" {
		return 0
	}
	count := 1
	for _, c := range uids {
		if c == ',' {
			count++
		}
	}
	return count
}

// categoryTypes 分类→通知类型列表
func categoryTypes(category string) []string {
	switch category {
	case "system":
		return []string{model.NotifyAuditApproved, model.NotifyAuditRejected, model.NotifyPostApproved, model.NotifyPostRejected, model.NotifyPostLocked, model.NotifyPostUnlocked, model.NotifyLevelUp}
	case "mention":
		return []string{model.NotifyComment, model.NotifyCommentReply}
	case "like":
		return []string{model.NotifyLikeAggregated}
	case "follow":
		return []string{model.NotifyFollow}
	default:
		return []string{}
	}
}

// sortNotifications 按 CreatedAt 降序排序
func sortNotifications(items []model.Notification) {
	sort.Slice(items, func(i, j int) bool {
		return items[i].CreatedAt.After(items[j].CreatedAt)
	})
}

// 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, nil)
}

// 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, nil)
}

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

// 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, nil)
}

// NotifyPostLocked 稿件锁定通知
func (s *NotificationService) NotifyPostLocked(userID uint, postTitle, reason string, postID uint) {
	title := "稿件被锁定"
	content := "你的稿件《" + postTitle + "》已被锁定"
	if reason != "" {
		content += "，理由：" + reason
	}
	_ = s.Create(userID, model.NotifyPostLocked, title, content, &postID, nil)
}

// NotifyPostUnlocked 稿件解除锁定通知
func (s *NotificationService) NotifyPostUnlocked(userID uint, postTitle string, postID uint) {
	_ = s.Create(userID, model.NotifyPostUnlocked,
		"稿件被解除锁定",
		"你的稿件《"+postTitle+"》已被解除锁定",
		&postID, nil)
}

// NotifyFollow 关注通知（followerID 关注了 followeeID）
func (s *NotificationService) NotifyFollow(followerID, followeeID uint) {
	// 获取关注者用户名
	followerName := s.getUsername(followerID)
	if followerName == "" {
		followerName = "一位用户"
	}
	title := "关注通知"
	content := followerName + " 关注了你"
	relID := followerID
	_ = s.Create(followeeID, model.NotifyFollow, title, content, &relID, nil)
}
