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

import com.oqpo.api.enums.DataMessageCode;
import com.oqpo.api.enums.SystemMessageCode;
import com.oqpo.api.mapper.system.VerInfoMapper;
import com.oqpo.api.service.system.JobService;
import com.oqpo.api.service.system.VerInfoService;
import com.oqpo.api.web.dto.request.VerInfo.SaveVerInfoRequest;
import com.oqpo.api.web.dto.request.VerInfo.VerInfoGridRequest;
import com.oqpo.api.web.dto.request.code.SaveCodeRequest;
import com.oqpo.api.web.dto.request.job.JobGridRequest;
import com.oqpo.api.web.dto.response.DataResponse;
import com.oqpo.api.web.dto.response.GridResponse;
import com.oqpo.api.web.dto.response.SaveResponse;
import com.oqpo.api.web.dto.response.stinfo.store.StoreInfoResponse;
import com.oqpo.api.web.dto.response.ver.VerInfoResponse;
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/verinfo")
public class VerInfoController {
    @Autowired
    private VerInfoService verInfoService;

    /**
     * 설명 : 앱버전 상세 그리드 리스트
     *
     * @param verInfoGridRequest
     * @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> appVerGridList(@RequestBody @Valid VerInfoGridRequest verInfoGridRequest) throws Exception {
        return ResponseEntity.ok(verInfoService.appVerGridList( verInfoGridRequest.toDTO(verInfoGridRequest)));
    }


    /**
     * 설명 : 앱버전정보 저장
     *
     * @param userId
     * @param saveVerInfoRequest
     * @return SaveResponse
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "앱버전정보 저장")
    @PostMapping("/save-verinfo")
    public ResponseEntity<SaveResponse> saveVerInfo(@ApiIgnore String userId, @RequestBody @Valid SaveVerInfoRequest saveVerInfoRequest) throws Exception {
        verInfoService.saveAppVer(userId, saveVerInfoRequest);
        return ResponseEntity.ok(SaveResponse.toDTO(SystemMessageCode.SAVE_OK));
    }


    /**
     * 설명 : 앱버전  정보
     *
     * @param appNo
     * @return
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "appNo", value = "앱버전번호", required = true, dataType = "String", paramType = "query")
    })
    @ApiOperation(value = "앱버전 정보")
    @GetMapping("/info-verinfo")
    public ResponseEntity<VerInfoResponse> infoVerInfo(@RequestParam(value = "appNo") String appNo) throws Exception {
        return ResponseEntity.ok(verInfoService.selectVerAppInfo(appNo));
    }


    /**
     * 설명 : 앱버전정보 삭제
     *
     * @param userId
     * @param appNo
     * @return SaveResponse
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "appNo", value = "앱버전번호", required = true, dataType = "String", paramType = "query")
    })
    @ApiOperation(value = "앱버전정보 삭제")
    @PostMapping("/del-verinfo")
    public ResponseEntity<SaveResponse> delVerInfo(@ApiIgnore String userId,@RequestParam(value = "appNo") String appNo ) throws Exception {
        verInfoService.delAppVer(userId, appNo);
        return ResponseEntity.ok(SaveResponse.toDTO(SystemMessageCode.DELETE_OK));
    }

    /**
     * 설명 : 앱 최신버전정보
     *
     * @param
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "앱 최신버전정보")
    @GetMapping("/info-lastver")
    public ResponseEntity<VerInfoResponse> selectLastVer() throws Exception {
        return ResponseEntity.ok(verInfoService.selectLastVerInfo());
    }


}
