package com.zhentao.service.impl;

import com.zhentao.entity.SystemMessage;
import com.zhentao.mapper.SystemMessageMapper;
import com.zhentao.service.SystemMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class SystemMessageServiceImpl implements SystemMessageService {

    @Autowired
    private SystemMessageMapper systemMessageMapper;

    // 无需加载动态Mapper：只按入参落库系统消息

    @Override
    public void sendDynamicModerationMessage(Integer receiverUserId,
                                             Integer dynamicId,
                                             String dynamicTitle,
                                             String type,
                                             String reason) {
        if (receiverUserId == null || dynamicId == null) return;

        String titlePrefix = "系统通知";
        String title = titlePrefix + " - " + ("ban".equals(type) ? "动态被封禁" : ("unban".equals(type) ? "解除封禁" : "动态被删除"));

        String content;
        String safeTitle = dynamicTitle != null ? dynamicTitle : "(无标题)";
        if ("ban".equals(type)) {
            content = String.format("您发布的动态《%s》因 %s 已被封禁。若同类违规累计达 3 次，将面临账号封禁处罚。处理时间：%s",
                    safeTitle, reason, LocalDateTime.now());
        } else if ("unban".equals(type)) {
            content = String.format("您发布的动态《%s》已解除封禁，可正常查看和互动。请注意遵守社区规范。处理时间：%s",
                    safeTitle, LocalDateTime.now());
        } else { // delete
            content = String.format("您发布的动态《%s》因 %s 已被删除。处理时间：%s",
                    safeTitle, reason, LocalDateTime.now());
        }

        SystemMessage msg = new SystemMessage();
        msg.setUserId(receiverUserId);
        msg.setType(type);
        msg.setRelatedDynamicId(dynamicId);
        msg.setTitle(title);
        msg.setContent(content);
        msg.setIsRead(0);
        msg.setCreatedAt(LocalDateTime.now());
        systemMessageMapper.insert(msg);
    }

    @Override
    public void sendMessage(Integer receiverUserId, Integer dynamicId, String title, String content, String type) {
        if (receiverUserId == null) return;
        SystemMessage msg = new SystemMessage();
        msg.setUserId(receiverUserId);
        msg.setType(type);
        msg.setRelatedDynamicId(dynamicId);
        msg.setTitle(title);
        msg.setContent(content);
        msg.setIsRead(0);
        msg.setCreatedAt(LocalDateTime.now());
        systemMessageMapper.insert(msg);
    }
}


