package com.webchat.controller.client;


import com.webchat.common.bean.APIPageResponseBean;
import com.webchat.common.bean.APIResponseBean;
import com.webchat.common.bean.APIResponseBeanUtil;
import com.webchat.common.enums.ESMessageTypeEum;
import com.webchat.common.helper.SessionHelper;
import com.webchat.config.annotation.ValidatePermission;
import com.webchat.domain.vo.dto.AbstractBaseEsDTO;
import com.webchat.service.elastic.WebChatElasticSearchService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(tags = "【搜索引擎】消息搜索服务接口")
@RestController
@RequestMapping("/api/search")
public class SearchController {

    @Autowired
    private WebChatElasticSearchService webChatElasticSearchService;

    /**
     * 聊天消息搜索
     *
     * @param query 关键词
     * @param type  搜索内容：空代表聚合搜索，搜全部；可以置顶具体的消息类型（聊天、公众号文章、用户）
     * @see ESMessageTypeEum
     *
     * @param size
     * @return
     */
    @GetMapping("/query")
    public APIPageResponseBean query(@RequestParam(value = "q", required = false) String query,
                                     @RequestParam(value = "type", required = false) Integer type,
                                     @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                     @RequestParam(value = "size", required = false, defaultValue = "10") Integer size) {
        String userId = SessionHelper.getCurrentUserId();
        return webChatElasticSearchService.query(type, userId, query, page, size);
    }

    @ValidatePermission
    @GetMapping("/createIndex")
    public APIResponseBean<Boolean> createChatMessageIndex() {
        boolean createIndexResult = webChatElasticSearchService.createIndex();
        return APIResponseBeanUtil.success(createIndexResult);
    }

    @ValidatePermission
    @GetMapping("/deleteIndex")
    public APIResponseBean<Boolean> deleteIndex() {
        boolean createIndexResult = webChatElasticSearchService.deleteIndex();
        return APIResponseBeanUtil.success(createIndexResult);
    }
}
