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.TransactionMapper;
import com.handong.smartservice.service.TransactionService;

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

    private final TransactionService transactionService;
   

    public TransactionController(TransactionService transactionService) {
        this.transactionService = transactionService;
    }

    @RequestMapping(value = "/get-transaction.do", method = RequestMethod.POST)
    public CtResponse getTransactionList(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;

        Boolean is_group_access = _AG.toBoolean(body.get("is_group_access"));
        String date_start = body.get("date_start");
        String date_end = body.get("date_end");
        Long biz_group_id = _AG.toLong(body.get("biz_group_id"));
        String uid1 = body.get("uid1");
        Long uid1_type = _AG.toLong(body.get("uid1_type"));
        String type = body.get("type");
        Long amount = _AG.toLong(body.get("amount"));
        String approval = body.get("approval");
        String pay_name = body.get("pay_name");
        String pay_vendor = body.get("pay_vendor");
        String slot = body.get("slot");
        String code = body.get("code");
        String goods_name = body.get("goods_name");

        Map<String, Object> mapResult = transactionService.getTransactionList(is_excel, offset, limit, is_group_access, biz_group_id, date_start, date_end, 
            uid1, uid1_type, type, amount, approval, pay_name, pay_vendor, slot, code, goods_name);

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

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

            String[] arrHeader = {"거래시간", "단말기 TID", "금액", "결제유형", "승인번호", "컬럼번호", "상품코드", "상품명", "결제서비스업체"};
            String[] arrColumn = {"order_time", "uid1", "amount", "type", "approval", "slot", "code", "goods_name", "pay_vendor"};

            CtExcelMaker.makeExcelResponse(request, response, "거래내역목록", arrHeader, arrColumn, list, null);
            return null;
        }

        result.put("result", mapResult);
        return result;
    }

    @RequestMapping(value = "/get-u-transaction.do", method = RequestMethod.POST)
    public CtResponse getUndefinedTransactionList(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 type = body.get("type");
        Long amount = _AG.toLong(body.get("amount"));
        String approval = body.get("approval");
        String pay_name = body.get("pay_name");
        String pay_vendor = body.get("pay_vendor");
        String slot = body.get("slot");
        String code = body.get("code");
        String goods_name = body.get("goods_name");

        Map<String, Object> mapResult = transactionService.getUndefinedTransactionList(is_excel, offset, limit, date_start, date_end, 
            uid1, uid1_type, type, amount, approval, pay_name, pay_vendor, slot, code, goods_name);
        result.put("result", mapResult);
        return result;
    }

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

        /*
        curl -X POST http://localhost:18080/api/add-transaction.do \
        -H "Content-Type: application/json" \
        -d '{
            "uid1": "123",
            "uid1_type": "1",
            "type": "D1",
            "amount": "900",
            "approval": "33",
            "pay_name": " HANA",
            "pay_vendor": "HANA",
            "slot": "01"
        }'
        */
        
        CtResponse result = new CtResponse();

        String uid1 = body.get("uid1");
        Long uid1_type = _AG.toLong(body.get("uid1_type"));
        String type = body.get("type");
        Long amount = _AG.toLong(body.get("amount"));
        String approval = body.get("approval");
        String pay_name = body.get("pay_name");
        String pay_vendor = body.get("pay_vendor");
        String slot = body.get("slot");
        //String code = body.get("code");
        //String goods_name = body.get("goods_name");
        String pay_unique_num = body.get("pay_unique_num");
        String pay_order_time = body.get("pay_order_time");

        if (uid1 == null || uid1_type == null || type == null || amount == null || slot == null) {
            result.setErrCode(ErrorCode.INVALID_PARAMETER);
            return result;
        }

        result = transactionService.addTransaction(uid1, uid1_type, type, amount, approval, pay_name, pay_vendor, slot, pay_unique_num, pay_order_time);
        return result;
    }
}
