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

import com.oqpo.api.enums.SystemMessageCode;
import com.oqpo.api.service.stinfo.BrandService;
import com.oqpo.api.service.stinfo.ItemUnitService;
import com.oqpo.api.web.dto.request.stinfo.brand.BrandGridRequest;
import com.oqpo.api.web.dto.request.stinfo.brand.BrandSearchGridRequest;
import com.oqpo.api.web.dto.request.stinfo.brand.SaveBrandInfolRequest;
import com.oqpo.api.web.dto.request.stinfo.itemUnit.ItemUnitSearchGridRequest;
import com.oqpo.api.web.dto.request.stinfo.itemUnit.SaveItemUnitInfolRequest;
import com.oqpo.api.web.dto.request.stinfo.itemUnit.SaveStoreItemUnitInfolRequest;
import com.oqpo.api.web.dto.request.stinfo.itemUnit.StoreItemUnitSearchGridRequest;
import com.oqpo.api.web.dto.response.GridResponse;
import com.oqpo.api.web.dto.response.SaveResponse;
import com.oqpo.api.web.dto.response.stinfo.brand.AfflBrandSearchResponse;
import com.oqpo.api.web.dto.response.stinfo.brand.BrandInfoResponse;
import com.oqpo.api.web.dto.response.stinfo.itemUnit.ItemUnitInfoResponse;
import com.oqpo.api.web.dto.response.stinfo.itemUnit.StoreItemUnitInfoResponse;
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/itemunit")
public class ItemUnitController {

    @Autowired
    private ItemUnitService itemUnitService;

    /**
     * 설명 : 공급사 단가 그리드 리스트
     *
     * @param ItemUnitSearchGridRequest
     * @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 ItemUnitSearchGridRequest itemUnitSearchGridRequest) throws Exception {
        return ResponseEntity.ok(itemUnitService.selectItemUnitGridList(itemUnitSearchGridRequest.getSBrandId(), itemUnitSearchGridRequest.getFromDt(), itemUnitSearchGridRequest.getToDt(), itemUnitSearchGridRequest.getSUnitStCd(), itemUnitSearchGridRequest.getSSpplyId(), itemUnitSearchGridRequest.getSClass1(), itemUnitSearchGridRequest.getSClass2(), itemUnitSearchGridRequest.getSClass3(), itemUnitSearchGridRequest.getSClass4(), itemUnitSearchGridRequest.getSItemNm(),  itemUnitSearchGridRequest.toDTO(itemUnitSearchGridRequest)));
    }



    /**
     * 설명 : 공급사 단가 정보
     *
     * @param brandId
     * @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 = "brandUnitUnqNo", value = "브랜드단가고유번호", required = true, dataType = "String", paramType = "query")
    })
    @ApiOperation(value = "공급사 단가  정보")
    @GetMapping("/info-itemunit")
    public ResponseEntity<ItemUnitInfoResponse> infoUnitItem(@RequestParam(value = "brandId") String brandId, @RequestParam(value = "brandUnitUnqNo") String brandUnitUnqNo) throws Exception {
        return ResponseEntity.ok(itemUnitService.selectItemUnitInfo(brandId, brandUnitUnqNo));
    }


    /**
     * 설명 : 공급사 단가 정보 저장
     *
     * @param userId
     * @param SaveItemUnitInfolRequest
     * @return SaveResponse
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "브랜드 단가정보  저장")
    @PostMapping("/save-itemunit")
    public ResponseEntity<SaveResponse> saveItemUnit(@ApiIgnore String userId, @RequestBody @Valid SaveItemUnitInfolRequest saveItemUnitInfolRequest) throws Exception {
        itemUnitService.saveItemUnitInfo(userId, saveItemUnitInfolRequest);
        return ResponseEntity.ok(SaveResponse.toDTO(SystemMessageCode.SAVE_OK));
    }

    /**
     * 설명 : 매장 단가 그리드 리스트
     *
     * @param StoreItemUnitSearchGridRequest
     * @return
     * @throws Exception
     */

    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "  매장 단가 그리드 리스트")
    @PostMapping("/store-unit-grid-list")
    public ResponseEntity<GridResponse> storeDetailGridList(@RequestBody @Valid StoreItemUnitSearchGridRequest storeItemUnitSearchGridRequest) throws Exception {
        return ResponseEntity.ok(itemUnitService.selectStoreItemUnitGridList(storeItemUnitSearchGridRequest.getSBrandId(),storeItemUnitSearchGridRequest.getSStoreId(),  storeItemUnitSearchGridRequest.toDTO(storeItemUnitSearchGridRequest)));
    }

    /**
     * 설명 : 매장 단가 정보
     *
     * @param brandId
     * @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 = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "contSttDt", value = "계약시작일시", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "contEndDt", value = "계약종료일시", required = true, dataType = "String", paramType = "query")
    })
    @ApiOperation(value = "매장단가 정보")
    @GetMapping("/info-storeitemunit")
    public ResponseEntity<StoreItemUnitInfoResponse> infoUnitItem(@RequestParam(value = "brandId") String brandId, @RequestParam(value = "storeId") String storeId, @RequestParam(value = "contSttDt") String contSttDt , @RequestParam(value = "contEndDt") String contEndDt) throws Exception {
        return ResponseEntity.ok(itemUnitService.selectStoreItemUnitInfo(brandId, storeId, contSttDt, contEndDt));
    }

    /**
     * 설명 : 매장 단가 정보 저장
     *
     * @param userId
     * @param SaveStoreItemUnitInfolRequest
     * @return SaveResponse
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "매장 단가정보  저장")
    @PostMapping("/save-store-itemunit")
    public ResponseEntity<SaveResponse> saveStoreItemUnit(@ApiIgnore String userId, @RequestBody @Valid SaveStoreItemUnitInfolRequest saveStoreItemUnitInfolRequest) throws Exception {
        itemUnitService.saveStroeItemUnitInfo(userId, saveStoreItemUnitInfolRequest);
        return ResponseEntity.ok(SaveResponse.toDTO(SystemMessageCode.SAVE_OK));
    }

}
