package com.oqpo.api.web.controller.oper;

import com.oqpo.api.entity.oper.NticeEntity;
import com.oqpo.api.enums.SystemMessageCode;
import com.oqpo.api.service.oper.NticeService;
import com.oqpo.api.web.dto.request.oper.ntice.NticeGridRequest;
import com.oqpo.api.web.dto.request.oper.ntice.NticeTargetGridRequest;
import com.oqpo.api.web.dto.request.oper.ntice.SaveNticeRequest;
import com.oqpo.api.web.dto.response.GridResponse;
import com.oqpo.api.web.dto.response.SaveResponse;
import com.oqpo.api.web.dto.response.oper.ntice.NkMNticeListResponse;
import com.oqpo.api.web.dto.response.oper.ntice.NticeResponse;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/api/ntice")
public class NticeController {
    @Autowired
    private NticeService nticeService;

    /**
     * 설명 : 공지 그리드 리스트
     *
     * @param nticeGridRequest
     * @return
     * @throws Exception
     */

    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = " 공지 그리드 리스트")
    @PostMapping("/detail-grid-list")
    public ResponseEntity<GridResponse> detailGridList(@ApiIgnore String afflShopId , @ApiIgnore String brandId, @ApiIgnore String authTpCd, @RequestBody @Valid NticeGridRequest nticeGridRequest) throws Exception {
        return ResponseEntity.ok(nticeService.selectNticeGridList( afflShopId, brandId, authTpCd, nticeGridRequest.getSAfflShopId(), nticeGridRequest.getSBrandId(), nticeGridRequest.getFromDt(), nticeGridRequest.getToDt(), nticeGridRequest.getSNticeTitl(),  nticeGridRequest.toDTO(nticeGridRequest)));
    }

    /**
     * 설명 : 공지정보 저장
     *
     * @param userId
     * @param saveNticeRequest
     * @return SaveResponse
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "공지정보 저장")
    @PostMapping("/save-ntice")
    public ResponseEntity<SaveResponse> saveUser(@ApiIgnore String userId, @RequestBody @Valid SaveNticeRequest saveNticeRequest) throws Exception {

        nticeService.saveNtice(userId, saveNticeRequest);
        return ResponseEntity.ok(SaveResponse.toDTO(SystemMessageCode.SAVE_OK));
    }

    /**
     * 설명 : 공지 헤더 정보
     *
     * @param nticeNo
     * @return
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "nticeNo", value = "공지번호", required = true, dataType = "String", paramType = "query")
    })
    @ApiOperation(value = "공지 헤더 정보")
    @GetMapping("/info-ntice")
    public ResponseEntity<NticeResponse> infoUser(@RequestParam(value = "nticeNo") String nticeNo) throws Exception {
        return ResponseEntity.ok(nticeService.selectNticeInfo(nticeNo));
    }

    /**
     * 설명 : 공지타겟 그리드 리스트
     *
     * @param nticeTargetGridRequest
     * @return
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "공지타켓 그리드 리스트")
    @PostMapping("/target-grid-list")
    public ResponseEntity<GridResponse> detailGridList(@RequestBody @Valid NticeTargetGridRequest nticeTargetGridRequest) throws Exception {
        return ResponseEntity.ok(nticeService.selectNticeTargetGridList(nticeTargetGridRequest.getNticeNo(), nticeTargetGridRequest.toDTO(nticeTargetGridRequest)));
    }

    /**
     * 설명 : 공지 view 카운터 증가 처리
     *
     * @param nticeNo
     * @param
     * @return SaveResponse
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "nticeNo", value = "공지번호", required = true, dataType = "String", paramType = "query")
    })
    @ApiOperation(value = "공지 view 카운터 증가 처리")
    @GetMapping("/up-viewcnt")
    public ResponseEntity<SaveResponse> saveUser(@RequestParam(value = "nticeNo") String nticeNo) throws Exception {
        nticeService.saveViewCnt(nticeNo);
        return ResponseEntity.ok(SaveResponse.toDTO(SystemMessageCode.EMPTY_OK));
    }

    /**
     * 설명 : 모바일 공지 리스트
     *
     * @param
     * @return TopMenuListResponse
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "모바일 공지 리스트")
    @GetMapping("/list-m-ntice")
    public ResponseEntity<List<NkMNticeListResponse>> listMobileNticeList(@ApiIgnore String userId) throws Exception {
        List<NticeEntity> entities = nticeService.selectMNticeList(userId);
        return ResponseEntity.ok(
                entities.stream()
                        .map(entity -> NkMNticeListResponse.toDTO(entity))
                        .collect(Collectors.toList())
        );
    }
}
