package com.handong.smartservice.controller.api;

import com.handong.smartservice._AG;
import com.handong.smartservice.component.CtResponse;
import com.handong.smartservice.component.ErrorCode;
import com.handong.smartservice.service.VocService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


@RestController
@RequestMapping("/api")
class VocController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    private final VocService vocService;


    public VocController(VocService vocService) {
        this.vocService = vocService;
    }

    @RequestMapping(value = "/get-voc-detail.do", method = RequestMethod.POST)
    public CtResponse getVocDetail(HttpServletRequest request, HttpServletResponse response, @RequestBody Map<String, String> body) {
        logger.info("req: " + request.getRequestURI());

        String voc_id = body.get("id");

        CtResponse result = vocService.getVocDetail(voc_id);
        return result;
    }

    @RequestMapping(value = "/get-voc.do", method = RequestMethod.POST)
    public CtResponse getVocList(HttpServletRequest request, HttpServletResponse response, @RequestBody Map<String, String> body) {
        logger.info("req: " + request.getRequestURI());

        CtResponse result = new CtResponse();

        Long offset= _AG.toLong(body.get("offset"));
        Long limit = _AG.toLong(body.get("limit"));
        if (limit > 100)
            limit = 100L;

        String date_start = body.get("date_start");
        String date_end = body.get("date_end");
        String title = body.get("title");
        String content = body.get("content");
        String process = body.get("process");
        
        Map<String, Object> mapResult = vocService.getVocList(offset, limit, date_start, date_end, title, content, process);
        result.put("result", mapResult);
        return result;
    }


    public CtResponse addOrModifyVoc(boolean isModify, HttpServletRequest request, HttpServletResponse response, @RequestBody Map<String, String> body) {
        CtResponse result = new CtResponse();

        String voc_id = body.get("voc_id");
        String title = body.get("title");
        String content = body.get("content");

        if (title == null || title.isEmpty() || content == null || content.isEmpty()) {
            result.setErrCode(ErrorCode.INVALID_PARAMETER);
            return result;
        }

        result = vocService.addOrModifyVoc(isModify, voc_id, title, content);
        return result;
    }

    @RequestMapping(value = "/add-voc.do", method = RequestMethod.POST)
    public CtResponse addVoc(HttpServletRequest request, HttpServletResponse response, @RequestBody Map<String, String> body) {
        logger.info("req: " + request.getRequestURI());

        return addOrModifyVoc(false, request, response, body);
    }

    @RequestMapping(value = "/modify-voc.do", method = RequestMethod.POST)
    public CtResponse modifyVoc(HttpServletRequest request, HttpServletResponse response, @RequestBody Map<String, String> body) {
        logger.info("req: " + request.getRequestURI());

        return addOrModifyVoc(true, request, response, body);
    }

    @RequestMapping(value = "/remove-voc.do", method = RequestMethod.POST)
    public CtResponse removeVoc(HttpServletRequest request, HttpServletResponse response, @RequestBody Map<String, String> body) {
        logger.info("req: " + request.getRequestURI());
        
        String ids = body.get("ids");

        CtResponse result = vocService.removeVoc(ids);
        return result;
    }
}
