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.mapper.BizGroupMapper;
import com.handong.smartservice.service.BizGroupService;

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

    private final BizGroupService bizGroupService;


    public BizGroupController(BizGroupService bizGroupService) {
        this.bizGroupService = bizGroupService;
    }

    @RequestMapping(value = "/get-biz-group.do", method = RequestMethod.POST)
    public CtResponse getBizGroupList(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 name  = body.get("name");

        String biz_reg_num = body.get("biz_reg_num");
        if (biz_reg_num != null && biz_reg_num.isEmpty() == false) {
            biz_reg_num = biz_reg_num.replaceAll("-", "");
        }

        String email = body.get("email");
        String phone = body.get("phone");
        String address = body.get("address");
        
        Map<String, Object> mapResult = bizGroupService.getBizGroupList(offset, limit, date_start, date_end, name, biz_reg_num, email, phone, address);
        result.put("result", mapResult);
        return result;
    }


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

        Long biz_group_id = _AG.toLong(body.get("biz_group_id"));
        Long pid = _AG.toLong(body.get("pid"));
        Long top_group_id = _AG.toLong(body.get("top_group_id"));
        String name = body.get("name");

        
        String biz_reg_num = body.get("biz_reg_num");

        if (biz_reg_num != null && biz_reg_num.isEmpty() == false) {
            biz_reg_num = biz_reg_num.replaceAll("-", "");
            if (_AG.isValidBusinessNumber(biz_reg_num) == false) {
                result.setErrCode(ErrorCode.INVALID_BIZ_REG_NUM);
                return result;
            }
        }

        String email = body.get("email");
        String phone = body.get("phone");
        String ceo = body.get("ceo");
        String address = body.get("address");

        //if (name == null || name.isEmpty() || biz_reg_num == null || biz_reg_num.isEmpty()) {
        if (name == null || name.isEmpty()) {
            result.setErrCode(ErrorCode.INVALID_PARAMETER);
            return result;
        }

        result = bizGroupService.addOrModifyBizGroup(isModify, biz_group_id, pid, top_group_id, name, biz_reg_num, email, phone, ceo, address);
        return result;
    }

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

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

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

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

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

        CtResponse result = bizGroupService.removeBizGroup(ids);
        return result;
    }


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

        Boolean isRoot = _AG.toBoolean(body.get("isRoot"));
        Long pid = _AG.toLong(body.get("pid"));
        Long top_group_id = _AG.toLong(body.get("top_group_id"));
        String date_start = body.get("date_start");
        String date_end = body.get("date_end");
        String name  = body.get("name");

        String biz_reg_num = body.get("biz_reg_num");
        if (biz_reg_num != null && biz_reg_num.isEmpty() == false) {
            biz_reg_num = biz_reg_num.replaceAll("-", "");
        }

        return bizGroupService.getBizGroup2(isRoot, pid, top_group_id, date_start, date_end, name, biz_reg_num);
    }

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

        Long biz_group_id = _AG.toLong(body.get("biz_group_id"));

        return bizGroupService.getBizGroup3(biz_group_id);
    }
}
