package com.ruoyi.web.modules.message.controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.web.modules.account.support.MemberContext;
import com.ruoyi.web.modules.message.service.IMemberMessageAppService;
import com.ruoyi.web.modules.message.vo.MemberMessageListItemVO;

/**
 * C 端站内消息（须会员 Token）
 */
@RestController
@RequestMapping("/api/member/message")
public class MemberMessageAppController extends BaseController
{
    @Autowired
    private IMemberMessageAppService memberMessageAppService;

    @GetMapping("/unread-count")
    public AjaxResult unreadCount()
    {
        return success(memberMessageAppService.getUnreadCount(requireMemberId()));
    }

    @GetMapping("/list")
    public TableDataInfo list()
    {
        startPage();
        List<MemberMessageListItemVO> list = memberMessageAppService.listMessages(requireMemberId());
        return getDataTable(list);
    }

    @GetMapping("/{messageId}")
    public AjaxResult detail(@PathVariable("messageId") Long messageId)
    {
        return success(memberMessageAppService.getDetailAndMarkRead(requireMemberId(), messageId));
    }

    private Long requireMemberId()
    {
        Long memberId = MemberContext.getMemberId();
        if (memberId == null)
        {
            throw new ServiceException("请先登录", 401);
        }
        return memberId;
    }
}
