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

import com.oqpo.api.enums.SystemMessageCode;
import com.oqpo.api.service.stockmng.WhsMngService;
import com.oqpo.api.web.dto.request.stockmng.BrandWhsMngGridRequest;
import com.oqpo.api.web.dto.request.stockmng.SaveWhsInfoRequest;
import com.oqpo.api.web.dto.response.GridResponse;
import com.oqpo.api.web.dto.response.SaveResponse;
import com.oqpo.api.web.dto.response.stockmng.WhsMngInfoResponse;
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/dspt/mng")
public class DsptMngController {

    @Autowired
    private WhsMngService whsMngService;

    /**
     * 설명 : 입금관리 그리드 리스트
     *
     * @param brandWhsMngGridRequest
     * @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 BrandWhsMngGridRequest brandWhsMngGridRequest) throws Exception {
        return ResponseEntity.ok(whsMngService.selectWhsMngGridList(brandWhsMngGridRequest.getSBrandId(), brandWhsMngGridRequest.getSStoreId(),
                brandWhsMngGridRequest.getSWhsDvsn(), brandWhsMngGridRequest.getSWhsStCd(),
                brandWhsMngGridRequest.getSWhsNm(), brandWhsMngGridRequest.toDTO(brandWhsMngGridRequest)));
    }

    /**
     * 설명 : 입금 요청
     *
     * @param userId
     * @param saveWhsInfoRequest
     * @return SaveResponse
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "입금 요청")
    @PostMapping("/request-dspt")
    public ResponseEntity<SaveResponse> requestDspt(@ApiIgnore String userId, @RequestBody @Valid SaveWhsInfoRequest saveWhsInfoRequest) throws Exception {
        whsMngService.addWhsInfo(userId, saveWhsInfoRequest);
        return ResponseEntity.ok(SaveResponse.toDTO(SystemMessageCode.SAVE_OK));
    }

    /**
     * 설명 : 입금 승인
     *
     * @param userId
     * @param saveWhsInfoRequest
     * @return SaveResponse
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "입금 승인")
    @PostMapping("/approve-dspt")
    public ResponseEntity<SaveResponse> approveDspt(@ApiIgnore String userId, @RequestBody @Valid SaveWhsInfoRequest saveWhsInfoRequest) throws Exception {
        whsMngService.removeWhsInfo(userId, saveWhsInfoRequest);
        return ResponseEntity.ok(SaveResponse.toDTO(SystemMessageCode.SAVE_OK));
    }

    /**
     * 설명 : 입금 반려
     *
     * @param userId
     * @param saveWhsInfoRequest
     * @return SaveResponse
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "입금 반려")
    @PostMapping("/reject-dspt")
    public ResponseEntity<SaveResponse> rejectDspt(@ApiIgnore String userId, @RequestBody @Valid SaveWhsInfoRequest saveWhsInfoRequest) throws Exception {
        whsMngService.removeWhsInfo(userId, saveWhsInfoRequest);
        return ResponseEntity.ok(SaveResponse.toDTO(SystemMessageCode.SAVE_OK));
    }

    /**
     * 설명 : 입금 정보
     *
     * @param brandId
     * @param whsId
     * @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 = "whsId", value = "입금아이디", required = true, dataType = "String", paramType = "query")
    })
    @ApiOperation(value = "입금 정보")
    @GetMapping("/info-dspt")
    public ResponseEntity<WhsMngInfoResponse> infoDspt(@RequestParam(value = "brandId") String brandId, @RequestParam(value = "whsId") String whsId) throws Exception {
        return ResponseEntity.ok(whsMngService.selectWhsMngInfo(brandId, whsId));
    }

    /**
     * 설명 : 입금 정보 삭제
     *
     * @param userId
     * @param saveWhsInfoRequest
     * @return SaveResponse
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "입금 삭제")
    @PostMapping("/remove-dspt")
    public ResponseEntity<SaveResponse> removeDspt(@ApiIgnore String userId, @RequestBody @Valid SaveWhsInfoRequest saveWhsInfoRequest) throws Exception {
        whsMngService.modifyWhsInfo(userId, saveWhsInfoRequest);
        return ResponseEntity.ok(SaveResponse.toDTO(SystemMessageCode.SAVE_OK));
    }

}
