package com.ichangzuo.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ichangzuo.common.annotation.Log;
import com.ichangzuo.common.enums.LogModuleEnum;
import com.ichangzuo.common.result.PageResult;
import com.ichangzuo.common.result.Result;
import com.ichangzuo.system.model.entity.Message;
import com.ichangzuo.system.model.form.UserForm;
import com.ichangzuo.system.model.query.MessagePageQuery;
import com.ichangzuo.system.model.query.UserPageQuery;
import com.ichangzuo.system.model.vo.UserPageVO;
import com.ichangzuo.system.service.impl.MessageServiceImpl;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1/message")
@RequiredArgsConstructor
public class MessageController {
    private final MessageServiceImpl messageService;

    @Operation(summary = "消息分页列表")
    @GetMapping("/page")
    public PageResult<Message> listPagedMessage(
            MessagePageQuery queryParams
    ) {
        IPage<Message> result = messageService.listPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "获取用户最新通知")
    @GetMapping(value = "/{userId}/latest")
    public Result<?> getAllMessage(@Parameter(description = "用户ID") @PathVariable Long userId){
        return Result.success(messageService.getLatest(userId));
    }

    @PutMapping(value = "/{ids}")
    public Result<?> setByIds(
            @Parameter(description = "消息ID，多个以英文逗号(,)分割") @PathVariable String ids,
            @Parameter(description = "设置参数 1=已读 2=删除") @RequestParam Integer state
    ) {
        boolean result = messageService.setByIds(ids,state);
        return Result.judge(result);
    }

    @PutMapping(value = "/save")
    public Result<?> save(
            @RequestBody Message message
    ) {
        boolean result = messageService.saveMessage(message);
        return Result.judge(result);
    }
}
