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

import com.oqpo.api.enums.SystemMessageCode;
import com.oqpo.api.service.stinfo.BrandService;
import com.oqpo.api.web.dto.request.oper.affl.AfflShopBaseInfoGridRequest;
import com.oqpo.api.web.dto.request.oper.affl.SaveAfflRequest;
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.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.oper.affl.AfflShopInfoResponse;
import com.oqpo.api.web.dto.response.stinfo.brand.AfflBrandSearchResponse;
import com.oqpo.api.web.dto.response.stinfo.brand.BrandInfoResponse;
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/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 설명 : 팝업 브랜드 그리드 리스트
     *
     * @param brandGridRequest
     * @return
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = " 팝업 브랜드 그리드 리스트")
    @PostMapping("/pop-grid-list")
    public ResponseEntity<GridResponse> brandPopGridList(@ApiIgnore String userId, @ApiIgnore String authTpCd, @RequestBody @Valid BrandGridRequest brandGridRequest) throws Exception {
        return ResponseEntity.ok(brandService.selectPopBrandGridList(userId, authTpCd, brandGridRequest.getBrandNm(), brandGridRequest.toDTO(brandGridRequest)));
    }


    /**
     * 설명 : 가맹점 브랜드 리스트
     *
     * @param afflShopId
     * @return
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "afflShopId", value = "가맹점아이디", required = true, dataType = "String", paramType = "query")
    })
    @ApiOperation(value = "가맹점 브랜드 리스트")
    @GetMapping("/affl-brand-list")
    public ResponseEntity<AfflBrandSearchResponse> selectAfflBrandList(@RequestParam(value = "afflShopId", required = true) String afflShopId) throws Exception {
        return ResponseEntity.ok(brandService.selectAfflBrandList(afflShopId));
    }


    /**
     * 설명 : 브랜드 그리드 리스트
     *
     * @param brandSearchGridRequest
     * @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 BrandSearchGridRequest brandSearchGridRequest) throws Exception {
        return ResponseEntity.ok(brandService.selectBrandGridList(brandSearchGridRequest.getSBrandNm(), brandSearchGridRequest.getSBrandType(), brandSearchGridRequest.getSStCd(),  brandSearchGridRequest.toDTO(brandSearchGridRequest)));
    }



    /**
     * 설명 : 브랜드 정보
     *
     * @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")
    })
    @ApiOperation(value = "브랜드 정보")
    @GetMapping("/info-brand")
    public ResponseEntity<BrandInfoResponse> infoBrand(@RequestParam(value = "brandId") String brandId) throws Exception {
        return ResponseEntity.ok(brandService.selectSaleMgrMgn(brandId));
    }


    /**
     * 설명 : 브랜드 정보 저장
     *
     * @param userId
     * @param SaveBrandInfolRequest
     * @return SaveResponse
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "브랜드정보  저장")
    @PostMapping("/save-brand")
    public ResponseEntity<SaveResponse> saveBrand(@ApiIgnore String userId, @RequestBody @Valid SaveBrandInfolRequest saveBrandInfolRequest) throws Exception {
        brandService.saveBrandInfo(userId, saveBrandInfolRequest);
        return ResponseEntity.ok(SaveResponse.toDTO(SystemMessageCode.SAVE_OK));
    }


    /**
     * 설명 : 브랜드 패스워드 초기화
     *
     * @param userId
     * @param
     * @return SaveResponse
     * @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")
    })
    @ApiOperation(value = "브랜드 패스워드 초기화")
    @GetMapping("/inti-brandPw")
    public ResponseEntity<SaveResponse> initBrandPw(@RequestParam(value = "brandId") String brandId) throws Exception {
        brandService.updateInitUserPw(brandId);
        return ResponseEntity.ok(SaveResponse.toDTO(SystemMessageCode.INIT_PW_OK));
    }

}
