package shop.alien.storeplatform.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.entity.store.vo.LifeNoticeVo;
import shop.alien.storeplatform.dto.MarkAllNoticesReadDTO;
import shop.alien.storeplatform.service.NoticeService;

/**
 * web端商户通知管理 前端控制器
 *
 * @author ssk
 * @since 2025-01-xx
 */
@Slf4j
@Api(tags = {"web端商户通知管理"})
@ApiSort(6)
@CrossOrigin
@RestController
@RequestMapping("/notice")
@RequiredArgsConstructor
public class NoticeController {

    private final NoticeService noticeService;

    @ApiOperation("获取系统通知和订单提醒统计")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "receiverId", value = "接收人ID（商户ID）", dataType = "String", paramType = "query", required = true)
    })
    @GetMapping("/getNoticeStatistics")
    public R<JSONObject> getNoticeStatistics(@RequestParam("receiverId") String receiverId) {
        log.info("NoticeController.getNoticeStatistics?receiverId={}", receiverId);
        try {
            JSONObject result = noticeService.getNoticeStatistics(receiverId);
            return R.data(result, "查询成功");
        } catch (Exception e) {
            log.error("NoticeController.getNoticeStatistics ERROR: {}", e.getMessage(), e);
            return R.fail(e.getMessage());
        }
    }

    @ApiOperation("获取通知列表")
    @ApiOperationSupport(order = 2)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "receiverId", value = "接收人ID（商户ID）", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "noticeType", value = "通知类型（0-系统通知和订单提醒之外的类型，1-系统通知，2-订单提醒）", dataType = "int", paramType = "query", required = false, defaultValue = "0")
    })
    @GetMapping("/getNoticeList")
    public R<IPage<LifeNoticeVo>> getNoticeList(@RequestParam("pageNum") int pageNum,
                                                 @RequestParam("pageSize") int pageSize,
                                                 @RequestParam("receiverId") String receiverId,
                                                 @RequestParam(value = "noticeType", defaultValue = "0") int noticeType) {
        log.info("NoticeController.getNoticeList?pageNum={}, pageSize={}, receiverId={}, noticeType={}", 
                pageNum, pageSize, receiverId, noticeType);
        try {
            IPage<LifeNoticeVo> result = noticeService.getNoticeList(pageNum, pageSize, receiverId, noticeType);
            return R.data(result);
        } catch (Exception e) {
            log.error("NoticeController.getNoticeList ERROR: {}", e.getMessage(), e);
            return R.fail(e.getMessage());
        }
    }

    @ApiOperation("标记通知为已读")
    @ApiOperationSupport(order = 3)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "通知ID", dataType = "Integer", paramType = "query", required = true)
    })
    @GetMapping("/markNoticeAsRead")
    public R<Boolean> markNoticeAsRead(@RequestParam("id") Integer id) {
        log.info("NoticeController.markNoticeAsRead?id={}", id);
        try {
            int result = noticeService.markNoticeAsRead(id);
            if (result > 0) {
                return R.success("标记已读成功");
            }
            return R.fail("标记已读失败");
        } catch (Exception e) {
            log.error("NoticeController.markNoticeAsRead ERROR: {}", e.getMessage(), e);
            return R.fail(e.getMessage());
        }
    }

    @ApiOperation("批量标记通知为已读（一键已读）")
    @ApiOperationSupport(order = 4)
    @PostMapping("/markAllNoticesAsRead")
    public R<Integer> markAllNoticesAsRead(@RequestBody MarkAllNoticesReadDTO dto) {
        log.info("NoticeController.markAllNoticesAsRead?receiverId={}, noticeType={}", 
                dto.getReceiverId(), dto.getNoticeType());
        try {
            int result = noticeService.markAllNoticesAsRead(dto.getReceiverId(), dto.getNoticeType());
            if (result > 0) {
                return R.data(result, "批量标记已读成功，共标记 " + result + " 条通知");
            }
            return R.data(0, "没有需要标记的通知");
        } catch (Exception e) {
            log.error("NoticeController.markAllNoticesAsRead ERROR: {}", e.getMessage(), e);
            return R.fail(e.getMessage());
        }
    }
}

