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.NoticeService;
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 NoticeController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    private final NoticeService noticeService;
    

    public NoticeController(NoticeService noticeService) {
        this.noticeService = noticeService;
    }

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

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

        CtResponse result = noticeService.getNoticeDetail(notice_id);
        return result;
    }

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

        CtResponse result = new CtResponse();

        Boolean is_excel = _AG.toBoolean(body.get("is_excel"));
        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");
        
        Map<String, Object> mapResult = noticeService.getNoticeList(offset, limit, date_start, date_end, title, content);
        result.put("result", mapResult);
        return result;
    }


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

        String notice_id = body.get("notice_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 = noticeService.addOrModifyNotice(isModify, notice_id, title, content);
        return result;
    }

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

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

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

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

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

        CtResponse result = noticeService.removeNotice(ids);
        return result;
    }
}
