package io.controller;


import io.annotation.Login;
import io.annotation.LoginUser;
import io.common.constant.Constant;
import io.common.page.PageData;
import io.common.utils.Result;
import io.common.validator.AssertUtils;
import io.common.validator.ValidatorUtils;
import io.common.validator.group.AddGroup;
import io.common.validator.group.DefaultGroup;
import io.common.validator.group.UpdateGroup;
import io.entity.UserEntity;
import io.modules.item.dto.CommentDTO;
import io.modules.item.service.CommentService;
import io.modules.item.service.DictService;
import io.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;

import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 评论表
 */
@RestController
@RequestMapping("/api/comment")
@AllArgsConstructor
@Tag(name="评论表")
@CrossOrigin
public class CommentController {
    private final CommentService commentService;
    private final UserService userService;
    private final DictService dictService;

    @GetMapping("list")
    @Operation(summary = "分页")
    @Parameters({
        @Parameter(name = Constant.PAGE, description = "当前页码，从1开始", in = ParameterIn.QUERY, required = true, ref="int") ,
        @Parameter(name = Constant.LIMIT, description = "每页显示记录数", in = ParameterIn.QUERY,required = true, ref="int") ,
        @Parameter(name = Constant.ORDER_FIELD, description = "排序字段", in = ParameterIn.QUERY, ref="String") ,
        @Parameter(name = Constant.ORDER, description = "排序方式，可选值(asc、desc)", in = ParameterIn.QUERY, ref="String"),
        @Parameter(name = "itemId", description = "关联编号", in = ParameterIn.QUERY, ref="String")
    })
    public Result<List<CommentDTO>> page(@Parameter(hidden = true) @RequestParam Map<String, Object> params){

        List<CommentDTO> page = commentService.list(params);
        List<CommentDTO> updatedPage = page.stream()
                .map(comment -> {
                    Long userId = comment.getUserId();
                    // 如果缓存中已经存在该用户的信息，直接使用
                    UserEntity userInfo = userService.getUserByUserId(userId);
                    if (userInfo != null) {
                        // 否则调用 userService 获取用户信息并缓存
                        String nickName = userService.getUserByUserId(userId).getNickName();
                        comment.setNickName(nickName);  // 设置昵称
                    }
                    return comment;
                })
                .collect(Collectors.toList());
        return new Result<List<CommentDTO>>().ok(updatedPage);
    }

    @Login
    @PostMapping
    @Operation(summary = "保存")
    public Result save(@RequestBody CommentDTO dto,@Parameter(hidden = true) @LoginUser UserEntity user){
        //效验数据
        dto.setUserId(user.getId());
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

//        List<String> contentExistsList = dictService.isExistsList(dto.getContent());
//        if (!contentExistsList.isEmpty()){
//            return new Result().error("内容出现敏感词："+ contentExistsList);
//        }

        commentService.save(dto);
        return new Result();
    }
//
//
//    @DeleteMapping
//    @Operation(summary = "删除")
//    @LogOperation("删除")
//    @RequiresPermissions("item:comment:delete")
//    public Result delete(@RequestBody Long[] ids){
//        //效验数据
//        AssertUtils.isArrayEmpty(ids, "id");
//
//        commentService.delete(ids);
//
//        return new Result();
//    }


}
