package com.icomsys.main_vm.biz.rcp.conversation.controller;

import com.google.gson.Gson;
import com.icomsys.main_vm.biz.advice.excep.CustomBadRequestException;
import com.icomsys.main_vm.biz.advice.excep.CustomNotFoundException;
import com.icomsys.main_vm.biz.common.system.vo.DslDataTableRes;
import com.icomsys.main_vm.biz.rcp.conversation.service.ConversationScenarioGUIService;
import com.icomsys.main_vm.biz.rcp.conversation.vo.*;
import com.querydsl.core.QueryResults;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
@Slf4j
@RequiredArgsConstructor
@RequestMapping("/admin/rcp/conversation")
public class ConversationScenarioGUIController {

    private final ConversationScenarioGUIService conversationScenarioGUIService;

    @GetMapping("/scenario/manage.do")
    public String ScenarioView(@RequestParam(value = "scenarioSeq", required = false, defaultValue = "-1")Long scenarioSeq, Model model){
        log.info("scenarioSeq", scenarioSeq);
        model.addAttribute("scenarioSeq", scenarioSeq);

        return "/adm/rcp/conversation/scenarioGUI";
    }

    @PostMapping("/scenario/list/search")
    @ResponseBody
    public DslDataTableRes<ConversationScenarioManagerRes> searchScenarioList(@RequestBody ConversationScenarioManagerSearchReq dto ) {
        log.info("list search dto -> {}", new Gson().toJson(dto));

        return conversationScenarioGUIService.searchScenarioList(dto);
    }

    @PostMapping("/scenario/release/list/search")
    @ResponseBody
    public DslDataTableRes<ConversationScenarioReleaseRes> searchScenarioReleaseList(@RequestBody ConversationScenarioManagerSearchReq dto) {
        log.info("list search dto -> {}", new Gson().toJson(dto));

        return conversationScenarioGUIService.searchScenarioReleaseList(dto);
    }

    @PostMapping("/scenario/register")
    @ResponseBody
    public int scenarioRegister(@RequestBody ConversationScenarioMasterVO dto)throws CustomNotFoundException, CustomBadRequestException {
        log.info("register dto -> {}", new Gson().toJson(dto));

        return conversationScenarioGUIService.scenarioRegister(dto);
    }

    @PostMapping("/scenario/name/update")
    @ResponseBody
    public int scenarioNameUpdate(@RequestBody ConversationScenarioMasterVO dto)throws CustomNotFoundException, CustomBadRequestException {
        log.info("update dto -> {}", new Gson().toJson(dto));

        return conversationScenarioGUIService.scenarioNameUpdate(dto);
    }

    @PostMapping("/scenario/release")
    @ResponseBody
    public int scenarioRelease(@RequestBody ConversationScenarioMasterVO dto)throws CustomNotFoundException, CustomBadRequestException {
        log.info("register dto -> {}", new Gson().toJson(dto));

        return conversationScenarioGUIService.scenarioRelease(dto);
    }

    @PostMapping("/scenario/rollback")
    @ResponseBody
    public int scenarioRollback(@RequestBody ConversationScenarioMasterVO dto)throws CustomNotFoundException, CustomBadRequestException {
        log.info("register dto -> {}", new Gson().toJson(dto));

        return conversationScenarioGUIService.scenarioRollback(dto);
    }

    @PostMapping("/scenario/reload")
    @ResponseBody
    public int scenarioReload(@RequestBody ConversationScenarioMasterVO dto)throws CustomNotFoundException, CustomBadRequestException {
        log.info("register dto -> {}", new Gson().toJson(dto));

        return conversationScenarioGUIService.scenarioReload(dto);
    }

    @PostMapping("/scenario/delete")
    @ResponseBody
    public int scenarioDelete(@RequestBody ConversationScenarioMasterVO dto)throws CustomNotFoundException, CustomBadRequestException {
        log.info("register dto -> {}", new Gson().toJson(dto));

        return conversationScenarioGUIService.scenarioDelete(dto);
    }

    @GetMapping("/scenario/manager/info")
    @ResponseBody
    public ConversationScenarioManagerInfoRes scenarioManagerInfo(@RequestParam(name = "scenarioSeq", required = true)Long scenarioSeq) throws CustomNotFoundException{
        log.info("view Controller - {}", scenarioSeq);

        return conversationScenarioGUIService.scenarioManagerInfo(scenarioSeq);
    }

    @PostMapping("/scenario/history/log")
    @ResponseBody
    public QueryResults<ConversationScenarioWorkHistoryRes> scenarioHistoryLog(@RequestBody ConversationScenarioMasterVO dto) throws CustomBadRequestException{
        log.info("view dto - {}", dto);

        return conversationScenarioGUIService.scenarioHistoryLog(dto);
    }

    @PostMapping("/scenario/related/intent")
    @ResponseBody
    public QueryResults<ConversationScenarioRelatedIntentVO> scenarioRelatedIntent(@RequestBody ConversationScenarioMasterVO dto) throws CustomBadRequestException {
        log.info("dto - {}", dto);
        return conversationScenarioGUIService.scenarioRelatedIntent(dto);
    }

    @PostMapping("/scenario/related/intent/save")
    @ResponseBody
    public int scenarioRelatedIntentSave(@RequestBody ConversationScenarioRelatedIntentSaveReq dto) throws CustomBadRequestException, CustomNotFoundException {
        log.info("dto - {}", dto);

        return  conversationScenarioGUIService.scenarioRelatedIntentSave(dto);
    }
}
