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

import com.oqpo.api.enums.SystemMessageCode;
import com.oqpo.api.service.pomng.PchReqService;
import com.oqpo.api.web.dto.request.pomng.pchReq.*;
import com.oqpo.api.web.dto.response.GridResponse;
import com.oqpo.api.web.dto.response.SaveResponse;
import com.oqpo.api.web.dto.response.pomng.pchReq.PchReqInfoResponse;
import com.oqpo.api.web.dto.response.pomng.pchReq.PchReqInitInfoResponse;
import com.oqpo.api.web.dto.response.pomng.pchReq.PchReqRejectTargetResponse;
import io.swagger.annotations.Api;
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;

@Slf4j
@RestController
@RequestMapping("/api/pomng")
@Api(tags = {"구매요청 관리 (PchReqController) -- 2022.10.22 완료"})
public class PchReqController {
    @Autowired
    private PchReqService pchReqService;


    /**
     * 설명 : 구매요청 초기 정보
     *
     * @param userId
     * @return
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "구매요청 초기 정보")
    @GetMapping("/init-pchReq")
    public ResponseEntity<PchReqInitInfoResponse> selectPchReqInitInfo(@ApiIgnore String userId, PchReqInitInfoRequest pchReqInitInfoRequest) throws Exception {
        return ResponseEntity.ok(pchReqService.selectPchReqInitInfo(userId, pchReqInitInfoRequest));
    }

    /**
     * 설명 : 구매요청 그리드 리스트
     *
     * @param pchReqSearchGridRequest
     * @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 userId,   @RequestBody @Valid PchReqSearchGridRequest pchReqSearchGridRequest) throws Exception {
        return ResponseEntity.ok(pchReqService.selectPchReqGridList(userId, pchReqSearchGridRequest.getSBrandId(), pchReqSearchGridRequest.getSStoreId(), pchReqSearchGridRequest.getFromDt(), pchReqSearchGridRequest.getToDt(), pchReqSearchGridRequest.getSWhsId(), pchReqSearchGridRequest.getSPchReqStCd(), pchReqSearchGridRequest.getSItemNm(),  pchReqSearchGridRequest.toDTO(pchReqSearchGridRequest)));
    }


    /**
     * 설명 : 구매요청 정보
     *
     * @param brandId, pchReqUnqNo
     * @return
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "brandId", value = "브랜드아이디", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "pchReqUnqNo", value = "구매요청번호", required = true, dataType = "String", paramType = "query")
    })
    @ApiOperation(value = "구매요청 정보")
    @GetMapping("/info-pchReq")
    public ResponseEntity<PchReqInfoResponse> infoPchReq(@ApiIgnore String userId, @RequestParam(value = "brandId") String brandId, @RequestParam(value = "pchReqUnqNo") String pchReqUnqNo) throws Exception {
        return ResponseEntity.ok(pchReqService.selectPchReqInfo(userId, brandId,pchReqUnqNo));
    }


    /**
     * 설명 : 구매요청 정보 저장
     *
     * @param userId
     * @param savePchReqlRequest
     * @return SaveResponse
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "구매요청 정보 저장")
    @PostMapping("/save-pchReq")
    public ResponseEntity<SaveResponse> savePchReq(@ApiIgnore String userId, @RequestBody @Valid SavePchReqlRequest savePchReqlRequest) throws Exception {
        pchReqService.savePchReqInfo(userId,  savePchReqlRequest);

        if ("PR00".equals(savePchReqlRequest.getPchReqStCd())) {
            return ResponseEntity.ok(SaveResponse.toDTO(SystemMessageCode.SAVE_OK));
        } else {
            return ResponseEntity.ok(SaveResponse.toDTO(SystemMessageCode.REQ_OK));
        }

    }

    /**
     * 설명 : 구매발주 대상 그리드 리스트
     *
     * @param pchReqSearchGridRequest
     * @return
     * @throws Exception
     */

    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = " 구매발주 대상 그리드 리스트")
    @PostMapping("/po-target-grid-list")
    public ResponseEntity<GridResponse> detailPoGridList(@RequestBody @Valid PchReqPoTargetSearchGridRequest pchReqPoTargetSearchGridRequest) throws Exception {
        return ResponseEntity.ok(pchReqService.selectPchReqPoTargetGridList(pchReqPoTargetSearchGridRequest.getSBrandId(), pchReqPoTargetSearchGridRequest.getFromDt(), pchReqPoTargetSearchGridRequest.getToDt(), pchReqPoTargetSearchGridRequest.getSSpplyId(), pchReqPoTargetSearchGridRequest.getSWhsId(), pchReqPoTargetSearchGridRequest.getSItemNm(),  pchReqPoTargetSearchGridRequest.toDTO(pchReqPoTargetSearchGridRequest)));
    }


    /**
     * 설명 : 구매요청 반련 대상 리스트
     *
     * @param pchReqRejectTargetGridRequest
     * @return
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "구매요청 반련 대상 리스트")
    @PostMapping("/reject-pchreq-target")
    public ResponseEntity<PchReqRejectTargetResponse> rejectTargetPchReq(@ApiIgnore String userId,@RequestBody @Valid PchReqRejectTargetGridRequest pchReqRejectTargetGridRequest) throws Exception {

            return ResponseEntity.ok(pchReqService.selectPchRejectPrTargetList(pchReqRejectTargetGridRequest.getPchReqUnqNo()));
    }


    /**
     * 설명 : 구매요청 반려 처리
     *
     * @param userId
     * @param savePchReqlRequest
     * @return SaveResponse
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "pchReqUnqNo", value = "구매요청번호", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "pchReqRjctRsn", value = "반려사유", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "storeId", value = "매장아이디", required = false, dataType = "String", paramType = "query")
    })
    @ApiOperation(value = "구매요청 반려 처리")
    @GetMapping("/reject-pchReq")
    public ResponseEntity<SaveResponse> rejectPchReq(@ApiIgnore String userId, @RequestParam(value = "pchReqUnqNo") String pchReqUnqNo, @RequestParam(value = "pchReqRjctRsn") String pchReqRjctRsn, @RequestParam(value = "storeId") String storeId) throws Exception {
        pchReqService.saveRejectPchReqInfo(userId, storeId, pchReqUnqNo, pchReqRjctRsn);
        return ResponseEntity.ok(SaveResponse.toDTO(SystemMessageCode.REJECT_OK));
    }


}
