package com.jeesharp.business.member.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.jeesharp.business.member.support.MemberLetterInboxMobileSupport;
import com.jeesharp.framework.memberauth.utils.MemberAuthUtils;
import com.jeesharp.framework.pojo.AjaxResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 小程序消息中心接口（对接 jeesharp-spring-boot-starter-letter 站内信）。
 *
 * @author jeesharp
 * @since 2026-05-08
 */
@Slf4j
@RestController
@RequestMapping("${jeesharp.web.mobilePath}/notification")
@Tag(name = "移动端-消息中心")
@AllArgsConstructor
public class NotificationMobileController {

    private final MemberLetterInboxMobileSupport memberLetterInboxMobileSupport;

    @Operation(summary = "消息列表")
    @GetMapping("/list")
    @SaCheckLogin
    public AjaxResult<?> list(
        @RequestParam(value = "page", defaultValue = "1") long page,
        @RequestParam(value = "pageSize", defaultValue = "15") long pageSize,
        @RequestParam(value = "type", required = false) Integer type,
        @RequestParam(value = "isRead", required = false) Integer isRead
    ) {
        String memberId = MemberAuthUtils.getLoginUserId();
        Map<String, Object> data = memberLetterInboxMobileSupport.list(memberId, page, pageSize, type, isRead);
        return AjaxResult.okData(data);
    }

    @Operation(summary = "消息详情")
    @GetMapping("/detail/{id}")
    @SaCheckLogin
    public AjaxResult<?> detail(@PathVariable("id") String id) {
        String memberId = MemberAuthUtils.getLoginUserId();
        Map<String, Object> row = memberLetterInboxMobileSupport.detail(memberId, id);
        if (row == null) {
            return AjaxResult.failMsg("消息不存在");
        }
        return AjaxResult.okData(row);
    }

    @Operation(summary = "标记已读")
    @PostMapping("/read/{id}")
    @SaCheckLogin
    public AjaxResult<?> read(@PathVariable("id") String id) {
        String memberId = MemberAuthUtils.getLoginUserId();
        memberLetterInboxMobileSupport.markRead(memberId, id);
        return AjaxResult.ok("标记成功");
    }

    @Operation(summary = "全部已读")
    @PostMapping("/read-all")
    @SaCheckLogin
    public AjaxResult<?> readAll() {
        String memberId = MemberAuthUtils.getLoginUserId();
        memberLetterInboxMobileSupport.markAllRead(memberId);
        return AjaxResult.ok("全部已读");
    }

    @Operation(summary = "删除单条")
    @PostMapping("/delete/{id}")
    @SaCheckLogin
    public AjaxResult<?> delete(@PathVariable("id") String id) {
        String memberId = MemberAuthUtils.getLoginUserId();
        memberLetterInboxMobileSupport.delete(memberId, id);
        return AjaxResult.ok("删除成功");
    }

    @Operation(summary = "清空已读")
    @PostMapping("/clear-read")
    @SaCheckLogin
    public AjaxResult<?> clearRead() {
        String memberId = MemberAuthUtils.getLoginUserId();
        memberLetterInboxMobileSupport.clearRead(memberId);
        return AjaxResult.ok("清空成功");
    }

    @Operation(summary = "未读数量")
    @GetMapping("/unread-count")
    @SaCheckLogin
    public AjaxResult<?> unreadCount() {
        String memberId = MemberAuthUtils.getLoginUserId();
        return AjaxResult.okData(memberLetterInboxMobileSupport.unreadSummary(memberId));
    }
}
