package com.handong.smartservice.controller.api;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import com.handong.smartservice._AG;
import com.handong.smartservice.component.CtResponse;
import com.handong.smartservice.component.ErrorCode;
import com.handong.smartservice.service.ResourceBoardService;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;


import java.util.List;
import java.util.Map;
import java.util.HashMap;


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


    public ResourceBoardController(ResourceBoardService resourceBoardService) {
        this.resourceBoardService = resourceBoardService;
    }

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

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

        CtResponse result = resourceBoardService.getResourceBoardDetail(resource_board_id);
        return result;
    }

    @RequestMapping(value = "/get-resource-board.do", method = RequestMethod.POST)
    public CtResponse getResourceBoardList(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");
        
        Map<String, Object> mapResult = resourceBoardService.getResourceBoardList(offset, limit, date_start, date_end, title, content);
        result.put("result", mapResult);
        return result;
    }


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

        String resource_board_id = body.get("resource_board_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 = resourceBoardService.addOrModifyResourceBoard(isModify, resource_board_id, title, content);
        return result;
    }

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

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

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

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

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

        CtResponse result = resourceBoardService.removeResourceBoard(ids);
        return result;
    }
}
