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

import com.oqpo.api.enums.SystemMessageCode;
import com.oqpo.api.service.stockmng.WhsMngService;
import com.oqpo.api.web.dto.request.stockmng.*;
import com.oqpo.api.web.dto.response.GridResponse;
import com.oqpo.api.web.dto.response.SaveResponse;
import com.oqpo.api.web.dto.response.code.CodeSearchListResponse;
import com.oqpo.api.web.dto.response.stockmng.WhsLocationPopListResponse;
import com.oqpo.api.web.dto.response.stockmng.WhsMngInfoResponse;
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/whs/mng")
@Api(tags = {"창고관리 (WhsMngController) -- 완료"})
public class WhsMngController {

    @Autowired
    private WhsMngService whsMngService;

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

    /**
     * 설명 : 창고 정보
     *
     * @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 = "storeId", value = "매장아이디", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "whsId", value = "창고아이디", required = false, dataType = "String", paramType = "query")
    })
    @ApiOperation(value = "창고 정보")
    @GetMapping("/info-whs")
    public ResponseEntity<WhsMngInfoResponse> infoWhs(@RequestParam(value = "brandId") String brandId,
                                                      @RequestParam(value = "storeId") String storeId, @RequestParam(value = "whsId") String whsId) throws Exception {
        return ResponseEntity.ok(whsMngService.selectWhsMngInfo(brandId, storeId, whsId));
    }

    /**
     * 설명 : Location 그리드 리스트
     *
     * @param locationGridRequest
     * @return
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "Location 그리드 리스트")
    @PostMapping("/location-grid-list")
    public ResponseEntity<GridResponse> locationGridList(@RequestBody @Valid LocationGridRequest locationGridRequest) throws Exception {
        return ResponseEntity.ok(whsMngService.selectWhsLocGridList(locationGridRequest.getBrandId(), locationGridRequest.getWhsId(),
                locationGridRequest.toDTO(locationGridRequest)));
    }

    /**
     * 설명 : 창고 신규등록
     *
     * @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("/add-whs")
    public ResponseEntity<SaveResponse> addWhs(@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("/modify-whs")
    public ResponseEntity<SaveResponse> modifyWhs(@ApiIgnore String userId, @RequestBody @Valid SaveWhsInfoRequest saveWhsInfoRequest) throws Exception {
        whsMngService.modifyWhsInfo(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("/remove-whs")
    public ResponseEntity<SaveResponse> removeWhs(@ApiIgnore String userId, @RequestBody @Valid SaveWhsInfoRequest saveWhsInfoRequest) throws Exception {
        whsMngService.removeWhsInfo(userId, saveWhsInfoRequest);
        return ResponseEntity.ok(SaveResponse.toDTO(SystemMessageCode.SAVE_OK));
    }


    /**
     * 설명 : 창고 팝업 검색
     *
     * @param sWhsDvsn
     * @param sWhsNm
     * @return
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "창고 팝업 검색")
    @PostMapping("/pop-whs-search")
    public ResponseEntity<GridResponse> searchPopWhsList(@RequestBody @Valid PopWhsLocationRequest popWhsLocationRequest) throws Exception {
        return ResponseEntity.ok(whsMngService.searchPopWhsLocationList(popWhsLocationRequest.getSBrandId()
                                                                        , popWhsLocationRequest.getSStoreId()
                                                                        , popWhsLocationRequest.getSWhsNm()
                                                                        , popWhsLocationRequest.getSWhsDvsn()
                                                                        , popWhsLocationRequest.toDTO(popWhsLocationRequest)   ));
    }
}
