package com.zhentao.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhentao.common.Result;
import com.zhentao.entity.SystemMessage;
import com.zhentao.mapper.SystemMessageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/message/system")
public class SystemMessageController {

    @Autowired
    private SystemMessageMapper systemMessageMapper;

    /** 系统消息列表 */
    @GetMapping("/list")
    public Result<Map<String, Object>> list(@RequestParam Integer userId,
                                            @RequestParam(defaultValue = "1") Integer pageNum,
                                            @RequestParam(defaultValue = "20") Integer pageSize) {
        if (userId == null || userId <= 0) {
            return Result.error("userId 不能为空");
        }
        Page<SystemMessage> page = new Page<>(pageNum, pageSize);
        QueryWrapper<SystemMessage> qw = new QueryWrapper<>();
        qw.eq("user_id", userId).orderByDesc("created_at");
        Page<SystemMessage> res = systemMessageMapper.selectPage(page, qw);
        Map<String, Object> data = new HashMap<>();
        data.put("list", res.getRecords());
        data.put("total", res.getTotal());
        data.put("page", res.getCurrent());
        data.put("pageSize", res.getSize());
        return Result.success(data);
    }

    /** 未读数量 */
    @GetMapping("/unread-count")
    public Result<Integer> unreadCount(@RequestParam Integer userId) {
        if (userId == null || userId <= 0) return Result.success(0);
        QueryWrapper<SystemMessage> qw = new QueryWrapper<>();
        qw.eq("user_id", userId).eq("is_read", 0);
        Integer count = systemMessageMapper.selectCount(qw).intValue();
        return Result.success(count);
    }

    /** 详情 */
    @GetMapping("/detail/{id}")
    public Result<SystemMessage> detail(@PathVariable Long id) {
        SystemMessage m = systemMessageMapper.selectById(id);
        if (m == null) return Result.error("消息不存在");
        return Result.success(m);
    }

    /** 标记单条已读 */
    @PostMapping("/read/{id}")
    public Result<String> markRead(@PathVariable Long id) {
        SystemMessage m = new SystemMessage();
        m.setId(id);
        m.setIsRead(1);
        m.setReadAt(java.time.LocalDateTime.now());
        systemMessageMapper.updateById(m);
        return Result.success("OK");
    }

    /** 全部标记已读 */
    @PostMapping("/read-all")
    public Result<String> markAllRead(@RequestParam Integer userId) {
        if (userId == null || userId <= 0) return Result.error("userId 不能为空");
        SystemMessage upd = new SystemMessage();
        upd.setIsRead(1);
        upd.setReadAt(java.time.LocalDateTime.now());
        systemMessageMapper.update(upd, new QueryWrapper<SystemMessage>().eq("user_id", userId).eq("is_read", 0));
        return Result.success("OK");
    }
}


