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.CtExcelMaker;
import com.handong.smartservice.component.CtResponse;
import com.handong.smartservice.component.ErrorCode;
import com.handong.smartservice.mapper.AccountMapper;
import com.handong.smartservice.service.AccountService;

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

    private final AccountService accountService;
   

    public AccountController(AccountService accountService) {
        this.accountService = accountService;
    }

    @RequestMapping(value = "/get-account.do", method = RequestMethod.POST)
    public CtResponse getAccountList(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");
        Boolean is_excel = _AG.toBoolean(body.get("is_excel"));

        Boolean is_group_access = _AG.toBoolean(body.get("is_group_access"));
        String nick_name = body.get("nick_name");
        String user_id = body.get("user_id");
        String email = body.get("email");
        Long biz_group_id = _AG.toLong(body.get("biz_group_id"));

        Map<String, Object> mapResult = accountService.getAccountList(offset, limit, date_start, date_end, is_excel, is_group_access, user_id, email, nick_name, biz_group_id);

        if (is_excel) {
            List<Map<String, String>> list = (List<Map<String, String>>)mapResult.get("list");

            for (Map<String, String> item : list) {
                item.replace("reg_time", _AG.timestampToStr(item.get("reg_time")));
            }

            String[] arrHeader = {"등록일", "그룹", "아이디", "닉네임", "이메일", "상태"};
            String[] arrColumn = {"reg_time", "biz_group_name", "user_id", "nick_name", "email", "state"};

            CtExcelMaker.makeExcelResponse(request, response, "계정목록", arrHeader, arrColumn, list, null);
            return null;
        }

        result.put("result", mapResult);
        return result;
    }
    
    public CtResponse addOrModifyAccount(boolean isModify, HttpServletRequest request, HttpServletResponse response, @RequestBody Map<String, String> body) {
        CtResponse result = new CtResponse();

        Long gid = _AG.toLong(body.get("gid"));
        String user_id = body.get("user_id");
        String user_pw = body.get("user_pw");
        String email = body.get("email");
        String nick_name = body.get("nick_name");
        Long state = _AG.toLong(body.get("state"));
        Long biz_group_id = _AG.toLong(body.get("biz_group_id"));
        Long new_biz_group_id = _AG.toLong(body.get("new_biz_group_id"));

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

        result = accountService.addOrModifyAccount(isModify, gid, user_id, user_pw, email, nick_name, state, biz_group_id, new_biz_group_id);
        return result;
    }

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

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

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

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

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

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

        return accountService.removeAccount(ids);
    }
}
