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

import com.oqpo.api.enums.SystemMessageCode;
import com.oqpo.api.service.sttlmng.SttlReqService;
import com.oqpo.api.web.dto.request.loanmng.ReqDsptRequest;
import com.oqpo.api.web.dto.request.loanmng.StlDtlGridRequest;
import com.oqpo.api.web.dto.request.sttlmng.SttlReqGridRequest;
import com.oqpo.api.web.dto.request.sttlmng.SttlReqProcRequest;
import com.oqpo.api.web.dto.request.sttlmng.SttlReqRequest;
import com.oqpo.api.web.dto.response.GridResponse;
import com.oqpo.api.web.dto.response.SaveResponse;
import com.oqpo.api.web.dto.response.sttlmng.SttlMngInfoResponse;
import com.oqpo.api.web.dto.response.sttlmng.SttlReqResponse;
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/sttl/req")
@Api(tags = {"정산요청 (SttlReqController)"})
public class SttlReqController {

    @Autowired
    private SttlReqService sttlReqService;

    /**
     * 설명 : 정산요청 그리드 리스트
     *
     * @param sttlReqGridRequest
     * @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(@RequestBody @Valid SttlReqGridRequest sttlReqGridRequest) throws Exception {
        return ResponseEntity.ok(sttlReqService.selectSttlReqGridList(sttlReqGridRequest.getSBrandId(), sttlReqGridRequest.getSStoreId(),
                sttlReqGridRequest.getSItemNm(), sttlReqGridRequest.getFromDt(), sttlReqGridRequest.getToDt(), sttlReqGridRequest.toDTO(sttlReqGridRequest)));
    }

    /**
     * 설명 : 정산 요청
     *
     * @param userId
     * @param sttlReqRequest
     * @return SaveResponse
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "정산 요청 - 임시저장")
    @PostMapping("/request-sttl")
    public ResponseEntity<SttlReqResponse> requestSttl(@ApiIgnore String userId, @RequestBody @Valid SttlReqRequest sttlReqRequest) throws Exception {
        return ResponseEntity.ok(sttlReqService.requestSttl(userId, sttlReqRequest));
    }

    /**
     * 설명 : 정산 요청 처리
     *
     * @param userId
     * @param sttlReqProcRequest
     * @return SaveResponse
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "정산 요청 처리")
    @PostMapping("/request-sttl-proc")
    public ResponseEntity<SaveResponse> requestSttlProc(@ApiIgnore String userId, @RequestBody @Valid SttlReqProcRequest sttlReqProcRequest) throws Exception {
        sttlReqService.procSttlRequest(userId, sttlReqProcRequest);
        return ResponseEntity.ok(SaveResponse.toDTO(SystemMessageCode.SAVE_OK));
    }

    /**
     * 설명 : 정산 상세정보
     *
     * @param sttlMgntUnqNo
     * @return
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "sttlMgntUnqNo", value = "정산관리고유번호", required = true, dataType = "String", paramType = "query")
    })
    @ApiOperation(value = "정산 상세정보")
    @GetMapping("/info-sttl")
    public ResponseEntity<SttlMngInfoResponse> infoSttl(@RequestParam(value = "sttlMgntUnqNo") String sttlMgntUnqNo) throws Exception {
        return ResponseEntity.ok(sttlReqService.selectSttlReqInfo(sttlMgntUnqNo));
    }

    /**
     * 설명 : 정산대상내역 그리드 리스트
     *
     * @param stlDtlGridRequest
     * @return
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "정산대상내역 그리드 리스트")
    @PostMapping("/stl-grid-list")
    public ResponseEntity<GridResponse> stlGridList(@RequestBody @Valid StlDtlGridRequest stlDtlGridRequest) throws Exception {
        return ResponseEntity.ok(sttlReqService.selectStlDtlGridList(stlDtlGridRequest.getSttlMgntUnqNo(), stlDtlGridRequest.toDTO(stlDtlGridRequest)));
    }

}
