package org.dromara.main.controller.miniapp;

import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.common.web.core.BaseController;
import org.dromara.main.domain.bo.MainMessageBo;
import org.dromara.main.domain.vo.MainMessageVo;
import org.dromara.main.service.IMainMessageService;
import org.springframework.web.bind.annotation.DeleteMapping;
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.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author chenYing
 * @date 2026-04-03 14:19:19
 **/

@RestController
@RequestMapping("/miniapp/message")
@RequiredArgsConstructor
public class MainMessageController extends BaseController {
    private final IMainMessageService service;

    @GetMapping("/list")
    public R<List<MainMessageVo>> list(MainMessageBo bo){
        return R.ok(service.queryList(bo));
    }

    @GetMapping("/{id}")
    public R<MainMessageVo> getInfo(@PathVariable Long id){
        return R.ok(service.queryById(id));
    }

    @PostMapping("/add")
    public R<Boolean> add(MainMessageBo bo){
        return R.ok(service.insertByBo(bo));
    }

    @PutMapping("/edit")
    public R<Boolean> edit(MainMessageBo bo){
        return R.ok(service.updateByBo(bo));
    }

    @DeleteMapping("/{ids}")
    public R<Boolean> remove(@PathVariable Long[] ids){
        return R.ok(service.deleteWithValidByIds(List.of(ids), true));
    }


    // 1. 获取未读消息数量
    @GetMapping("/unread-count")
    public R<Long> getUnreadCount() {
        MainMessageBo bo = new MainMessageBo();
        bo.setStudentId(LoginHelper.getUserId()); // 【修复】设置当前用户ID
        bo.setIsRead(0);
        return R.ok(service.selectCount(bo));
    }
    @PostMapping("/read-all")
    public R<Boolean> readAll() {
        // 【修复】使用 LoginHelper.getUserId()
        return R.ok(service.markAllAsRead(LoginHelper.getUserId()));
    }


    @PutMapping("/read/{id}")
    public R<Boolean> read(@PathVariable Long id) {
        return R.ok(service.markRead(id));
    }


}
