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.service.SalesService;

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

    private final SalesService salesService;
   

    public SalesController(SalesService salesService) {
        this.salesService = salesService;
    }

    @RequestMapping(value = "/get-sales.do", method = RequestMethod.POST)
    public CtResponse getSalesList(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");
        String device_name = body.get("device_name");
        Long approval_type = _AG.toLong(body.get("approval_type"));
        Long date_type = _AG.toLong(body.get("date_type"));

        Map<String, Object> mapResult = salesService.getSalesList(is_excel, offset, limit, is_group_access, biz_group_id, date_start, date_end, 
            uid1, device_name, approval_type, date_type);

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

            String[] arrHeader = {"날짜", "무인기기명", "단말기TID", "카드건수", "카드금액", "현금건수", "현금금액", "T머니건수", "T머니금액", "캐시비건수", "캐시비금액", "합계"};
            String[] arrColumn = {"date", "device_name", "uid1", "card_count", "card_amount", "cash_count", "cash_amount", "tmoney_count", "tmoney_amount", "cbee_count", "cbee_amount", "total"};

            CtExcelMaker.makeExcelResponse(request, response, "매출집계", arrHeader, arrColumn, list, null);
            return null;
        }

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