package com.its.api.its.controller.database;

import com.its.api.its.model.dto.code.TbCmmnCdDto;
import com.its.api.its.model.dto.code.TbCmmnClsfCdDto;
import com.its.api.its.service.code.TbCmmnCdService;
import com.its.api.its.service.code.TbCmmnClsfCdService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/database/code")
@Api(tags = "09.기초데이터관리-09.운영코드관리")
public class CodeTableController {

    private final TbCmmnClsfCdService cmmnClsfCdService;
    private final TbCmmnCdService cmmnCdService;

    @ApiOperation(value = "대분류코드-전체조회(TB_CMMN_CLSF_CD)", response = TbCmmnClsfCdDto.class, responseContainer = "ArrayList")
    @GetMapping(value = "/clsf-cd", produces = {"application/json; charset=utf8"})
    public List<TbCmmnClsfCdDto> findAllCmmnClsfCd() {
        return this.cmmnClsfCdService.findAll();
    }

   /* @ApiOperation(value = "대분류코드-개별조회(TB_CMMN_CLSF_CD)", response = CmmnClsfCdDto.CmmnClsfCdInfo.class)
    @GetMapping(value = "/clsf-cd/{id}", produces = {"application/json; charset=utf8"})
    public ResponseEntity<CmmnClsfCdDto.CmmnClsfCdInfo> findByIdCmmnClsfCd(@PathVariable final String id) {
        CmmnClsfCd obj = this.cmmnClsfCdService.findById(id);
        if (obj != null) {
            return new ResponseEntity<>(new CmmnClsfCdDto.CmmnClsfCdInfo(obj), HttpStatus.OK);
        }
        return new ResponseEntity<>(null, HttpStatus.OK);
    }
*/
    @ApiOperation(value = "대분류코드-정보변경/생성(TB_CMMN_CLSF_CD)", response = TbCmmnClsfCdDto.TbCmmnClsfCdUpdReq.class)
    @PostMapping(value = "/clsf-cd", produces = {"application/json; charset=utf8"})
    public TbCmmnClsfCdDto mergeInfoCmmnClsfCd(@RequestBody @Valid final TbCmmnClsfCdDto.TbCmmnClsfCdUpdReq req) {
        return this.cmmnClsfCdService.mergeInfo(req);
    }

    @ApiOperation(value = "대분류코드-정보삭제(TB_CMMN_CLSF_CD)", response = TbCmmnClsfCdDto.class)
    @DeleteMapping(value = "/clsf-cd/{id}", produces = {"application/json; charset=utf8"})
    public TbCmmnClsfCdDto deleteDataByIdCmmnClsfCd(@PathVariable("id") String id) {
        return this.cmmnClsfCdService.deleteById(id);
    }


    ////////////////////////////////////// 소분류코드
    /*@ApiOperation(value = "소분류코드-전체조회(TB_CMMN_CD)", response = CmmnCdDto.CmmnCdInfo.class)
    @GetMapping(value = "/cmmn-cd", produces = {"application/json; charset=utf8"})
    public ResponseEntity<List<CmmnCdDto.CmmnCdInfo>> findAllCmmnCd() {
        List<CmmnCdDto.CmmnCdInfo> result = new ArrayList<>();
        List<CmmnCd> data = this.cmmnCdService.findAll();
        for (CmmnCd obj: data) {
            result.add(new CmmnCdDto.CmmnCdInfo(obj));
        }
        return new ResponseEntity<>(result, HttpStatus.OK);
    }
*/
    @ApiOperation(value = "소분류코드-개별조회-목록(TB_CMMN_CD)", response = TbCmmnCdDto.class, responseContainer = "ArrayList")
    @GetMapping(value = "/cmmn-cd/{id}", produces = {"application/json; charset=utf8"})
    public List<TbCmmnCdDto> findByIdCmmnCd(@PathVariable final String id) {
        return this.cmmnCdService.findAllByCmmnClsfCd(id);
    }

    /*@ApiOperation(value = "소분류코드-개별조회-단일(TB_CMMN_CD)", response = CmmnCdDto.CmmnCdInfo.class)
    @GetMapping(value = "/cmmn-cd/{id}/{sub}", produces = {"application/json; charset=utf8"})
    public ResponseEntity<CmmnCdDto.CmmnCdInfo> findByIdCmmnCd(@PathVariable final String id, @PathVariable final String sub) {
        CmmnCd data = this.cmmnCdService.findAllByCmmnClsfCdCmmnCd(id, sub);
        if (data != null) {
            return new ResponseEntity<>(new CmmnCdDto.CmmnCdInfo(data), HttpStatus.OK);
        }
        return new ResponseEntity<>(null, HttpStatus.NOT_FOUND);
    }
*/
    @ApiOperation(value = "소분류코드-정보변경/생성(TB_CMMN_CD)", response = TbCmmnCdDto.class)
    @PostMapping(value = "/cmmn-cd", produces = {"application/json; charset=utf8"})
    public TbCmmnCdDto mergeInfoCmmnCd(@RequestBody @Valid final TbCmmnCdDto.TbCmmnCdUpdReq req) {
        return this.cmmnCdService.mergeInfo(req);
    }

    @ApiOperation(value = "소분류코드-정보삭제(TB_CMMN_CD)", response = TbCmmnCdDto.class)
    @DeleteMapping(value = "/cmmn-cd/{id}/{sub}", produces = {"application/json; charset=utf8"})
    public TbCmmnCdDto deleteDataByIdCmmnCd(@PathVariable("id") String id, @PathVariable("sub") String sub) {
        return this.cmmnCdService.deleteById(id, sub);
    }

}
