package com.takensoft.cms.search.web;

import com.takensoft.cms.search.service.SearchService;
import com.takensoft.cms.search.vo.SearchResultVO;
import com.takensoft.cms.search.vo.SearchVO;
import com.takensoft.common.util.ResponseData;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.nio.charset.Charset;

/**
 * @author  : 하석형
 * @since   : 2024.06.12
 *
 * 통합 검색 관련 컨트롤러
 */
@RestController
@RequiredArgsConstructor
@Slf4j
@RequestMapping("/sys/search")
public class SearchController {

    private final SearchService searchService;

    /**
     * @author 하석형
     * @since  2024.06.11
     * @param  searchVO
     * @return
     * @throws Exception
     *
     * 통합 검색
     */
    @PostMapping("/totalSearch.json")
    public ResponseEntity<?> totalSearch(@RequestBody SearchVO searchVO) throws Exception {
        SearchResultVO result = searchService.searchAll(searchVO);

        // 응답 처리
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(new MediaType("application", "json", Charset.forName("UTF-8")));
        ResponseData responseData = new ResponseData();
        responseData.setStatus(HttpStatus.OK);
        responseData.setMessage("정상적으로 조회가 처리되었습니다.");
        responseData.setData(result);
        return new ResponseEntity<>(responseData, headers, HttpStatus.OK);
    }
}