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

    private final ErrorHistoryService errorHistoryService;


    public ErrorHistoryController(ErrorHistoryService errorHistoryService) {
        this.errorHistoryService = errorHistoryService;
    }

    @RequestMapping(value = "/get-error-history.do", method = RequestMethod.POST)
    public CtResponse getErrorHistory(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 uid1 = body.get("uid1");
        Long uid1_type = _AG.toLong(body.get("uid1_type"));
        String slot = body.get("slot");
        
        Map<String, Object> mapResult = errorHistoryService.getErrorHistoryList(is_excel, offset, limit, date_start, date_end, uid1, uid1_type, slot);
        result.put("result", mapResult);
        return result;
    }

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

        CtResponse result = new CtResponse();

        Long state = _AG.toLong(body.get("state"));
        
        Map<String, Object> mapResult = errorHistoryService.getMiss(state);
        result.put("result", mapResult);
        return result;
    }

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

        CtResponse result = new CtResponse();

        String uid1 = body.get("uid1");
        Long uid1_type = _AG.toLong(body.get("uid1_type"));
        String slot = body.get("slot");
        
        Map<String, Object> mapResult = errorHistoryService.missSlot(uid1, uid1_type, slot);
        result.put("result", mapResult);
        return result;
    }

    
}
