package incheon.ags.ias.trms.web;

import incheon.ags.ias.trms.service.TrmsService;
import incheon.ags.ias.trms.vo.TrmsVO;
import incheon.com.cmm.api.DefaultApiResponse;
import incheon.com.cmm.exception.BusinessException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
@Slf4j
public class TrmsApiController {

    private final TrmsService trmsService;

    @PostMapping("/ags/ias/trms/trmsRegistAction.do")
    public ResponseEntity<DefaultApiResponse<TrmsVO>> trmsRegistAction(
            @RequestBody TrmsVO trmsVO) throws Exception {

        log.info("약관 등록 요청: {}", trmsVO.toString());

        // 시스템 코드 기본값 설정
        if (trmsVO.getSysCd() == null || trmsVO.getSysCd().isEmpty()) {
            trmsVO.setSysCd("RES");
        }

        int result = trmsService.insertTrms(trmsVO);

        if (result <= 0) {
            throw new BusinessException("약관 등록에 실패했습니다.");
        }

        log.info("약관 등록 성공 - trmsSn: {}", trmsVO.getTrmsSn());
        return ResponseEntity.ok(
            DefaultApiResponse.success(trmsVO, "약관이 성공적으로 등록되었습니다.")
        );
    }

    @PutMapping("/ags/ias/trms/trmsModifyAction.do/{trmsSn}")
    public ResponseEntity<DefaultApiResponse<TrmsVO>> trmsModifyAction(
            @PathVariable("trmsSn") Long trmsSn,
            @RequestBody TrmsVO trmsVO) throws Exception {

        log.info("약관 수정 요청 - trmsSn: {}, 데이터: {}", trmsSn, trmsVO.toString());

        if (trmsSn == null) {
            throw new BusinessException("수정할 약관 일련번호가 없습니다.");
        }

        trmsVO.setTrmsSn(trmsSn);

        TrmsVO existingTrms = trmsService.selectTrmsDetail(trmsSn);
        if (existingTrms == null) {
            throw new BusinessException("수정할 약관을 찾을 수 없습니다.");
        }

        int result = trmsService.updateTrms(trmsVO);

        if (result <= 0) {
            throw new BusinessException("약관 수정에 실패했습니다.");
        }

        log.info("약관 수정 성공 - trmsSn: {}", trmsSn);
        return ResponseEntity.ok(
            DefaultApiResponse.success(trmsVO, "약관이 성공적으로 수정되었습니다.")
        );
    }

    @DeleteMapping("/ags/ias/trms/trmsDeleteAction.do/{trmsSn}")
    public ResponseEntity<DefaultApiResponse<Object>> trmsDeleteAction(@PathVariable Long trmsSn) throws Exception {
        log.info("약관 삭제 요청 - trmsSn: {}", trmsSn);

        if (trmsSn == null) {
            throw new BusinessException("삭제할 약관 일련번호가 없습니다.");
        }

        TrmsVO existingTrms = trmsService.selectTrmsDetail(trmsSn);
        if (existingTrms == null) {
            throw new BusinessException("삭제할 약관을 찾을 수 없습니다.");
        }

        int result = trmsService.deleteTrms(trmsSn);

        if (result <= 0) {
            throw new BusinessException("약관 삭제에 실패했습니다.");
        }

        log.info("약관 삭제 성공 - trmsSn: {}", trmsSn);
        return ResponseEntity.ok(
            DefaultApiResponse.success("약관이 성공적으로 삭제되었습니다.")
        );
    }
}
