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

import com.oqpo.api.enums.SystemMessageCode;
import com.oqpo.api.service.pomng.PchReqService;
import com.oqpo.api.service.rtnmng.RtnReqService;
import com.oqpo.api.web.dto.request.pomng.pchReq.*;
import com.oqpo.api.web.dto.request.rtnmng.RtnReqRejectTargetGridRequest;
import com.oqpo.api.web.dto.request.rtnmng.RtnReqSearchGridRequest;
import com.oqpo.api.web.dto.request.rtnmng.RtnReqTargetSearchGridRequest;
import com.oqpo.api.web.dto.request.rtnmng.SaveRtnReqlRequest;
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 com.oqpo.api.web.dto.response.rtnmng.rtnReq.RtnReqInfoResponse;
import com.oqpo.api.web.dto.response.rtnmng.rtnReq.RtnReqInitInfoResponse;
import com.oqpo.api.web.dto.response.rtnmng.rtnReq.RtnReqRejectTargetResponse;
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/rtnmng")
@Api(tags = {"반품요청 관리 (RtnReqController) -- 진행중"})
public class RtnReqController {
    @Autowired
    private RtnReqService rtnReqService;


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


    /**
     * 설명 : 반품요청 정보 저장
     *
     * @param userId
     * @param SaveRtnReqlRequest
     * @return SaveResponse
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "반품요청 정보 저장")
    @PostMapping("/save-rtnReq")
    public ResponseEntity<SaveResponse> savePchReq(@ApiIgnore String userId, @RequestBody @Valid SaveRtnReqlRequest saveRtnReqlRequest) throws Exception {
        rtnReqService.saveRtnReqInfo(userId,  saveRtnReqlRequest);

        if ("RR00".equals(saveRtnReqlRequest.getRtnReqStCd())) {
            return ResponseEntity.ok(SaveResponse.toDTO(SystemMessageCode.SAVE_OK));
        } else {
            return ResponseEntity.ok(SaveResponse.toDTO(SystemMessageCode.REQ_OK));
        }

    }


    /**
     * 설명 : 반품요청 그리드 리스트
     *
     * @param rtnReqSearchGridRequest
     * @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 RtnReqSearchGridRequest rtnReqSearchGridRequest) throws Exception {
        return ResponseEntity.ok(rtnReqService.selectRtnReqGridList(userId, rtnReqSearchGridRequest.getSBrandId(), rtnReqSearchGridRequest.getSStoreId(), rtnReqSearchGridRequest.getFromDt(), rtnReqSearchGridRequest.getToDt(), rtnReqSearchGridRequest.getSWhsId(), rtnReqSearchGridRequest.getSRtnReqStCd(), rtnReqSearchGridRequest.getSItemNm(),  rtnReqSearchGridRequest.toDTO(rtnReqSearchGridRequest)));
    }

    /**
     * 설명 : 반품요청 정보
     *
     * @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 = "rtnReqUnqNo", value = "반품요청번호", required = true, dataType = "String", paramType = "query")
    })
    @ApiOperation(value = "반품요청 정보")
    @GetMapping("/info-rtnReq")
    public ResponseEntity<RtnReqInfoResponse> infoRtnReq(@ApiIgnore String userId, @RequestParam(value = "brandId") String brandId, @RequestParam(value = "rtnReqUnqNo") String rtnReqUnqNo) throws Exception {
        return ResponseEntity.ok(rtnReqService.selectRtnReqInfo(userId, brandId,rtnReqUnqNo));
    }


    /**
     * 설명 : 반품요청 대상 그리드 리스트
     *
     * @param RtnReqTargetSearchGridRequest
     * @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> rtnReqTargetGridList(@ApiIgnore String userId,   @RequestBody @Valid RtnReqTargetSearchGridRequest rtnReqTargetSearchGridRequest) throws Exception {
        return ResponseEntity.ok(rtnReqService.selectRtnReqTargetGridList(userId, rtnReqTargetSearchGridRequest.getSBrandId(), rtnReqTargetSearchGridRequest.getSStoreId(), rtnReqTargetSearchGridRequest.getFromDt(), rtnReqTargetSearchGridRequest.getToDt(), rtnReqTargetSearchGridRequest.getSWhsId(), rtnReqTargetSearchGridRequest.getSSpplyId(), rtnReqTargetSearchGridRequest.getSItemNm(),  rtnReqTargetSearchGridRequest.toDTO(rtnReqTargetSearchGridRequest)));
    }


    /**
     * 설명 : 반품요청 반련 대상 리스트
     *
     * @param rtnReqRejectTargetGridRequest
     * @return
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "반품요청 반려 대상 리스트")
    @PostMapping("/reject-rtnreq-target")
    public ResponseEntity<RtnReqRejectTargetResponse> rejectTargetRtnReq(@ApiIgnore String userId, @RequestBody @Valid RtnReqRejectTargetGridRequest rtnReqRejectTargetGridRequest) throws Exception {

        return ResponseEntity.ok(rtnReqService.selectRtnRejectPrTargetList(rtnReqRejectTargetGridRequest.getRtnReqUnqNo()));
    }


    /**
     * 설명 : 반품요청 반려 처리
     *
     * @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 = "rtnReqUnqNo", value = "구매요청번호", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "rtnRjctRsn", value = "반려사유", required = true, dataType = "String", paramType = "query")
    })
    @ApiOperation(value = "반품요청 반려 처리")
    @GetMapping("/reject-rtnReq")
    public ResponseEntity<SaveResponse> rejectRtnReq(@ApiIgnore String userId, @RequestParam(value = "rtnReqUnqNo") String rtnReqUnqNo, @RequestParam(value = "rtnRjctRsn") String rtnRjctRsn) throws Exception {
        rtnReqService.saveRejRtnReqBaseInfo(userId, rtnReqUnqNo, rtnRjctRsn);
        return ResponseEntity.ok(SaveResponse.toDTO(SystemMessageCode.REJECT_OK));
    }
}
