package com.caliverse.admin.domain.service;

import com.caliverse.admin.domain.dao.admin.AdminMapper;
import com.caliverse.admin.domain.entity.Admin;
import com.caliverse.admin.domain.entity.HISTORYTYPEDETAIL;
import com.caliverse.admin.domain.request.HistoryRequest;
import com.caliverse.admin.domain.response.HistoryResponse;
import com.caliverse.admin.global.common.annotation.RequestLog;
import com.caliverse.admin.global.common.code.CommonCode;
import com.caliverse.admin.global.common.code.ErrorCode;
import com.caliverse.admin.global.common.utils.CommonUtils;

import com.caliverse.admin.global.common.utils.DateUtils;
import com.caliverse.admin.mongodb.domain.BusinessLog;
import com.caliverse.admin.mongodb.domain.HistoryLogInfoBase;
import com.caliverse.admin.mongodb.service.HistoryLogService;
import lombok.RequiredArgsConstructor;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Service
@RequiredArgsConstructor
@Slf4j
public class HistoryService {

    private final AdminMapper adminMapper;
    private final HistoryLogService historyLogService;

    @RequestLog
    public HistoryResponse getHistoryList(Map requestParams){

        //페이징 처리
        requestParams = CommonUtils.pageSetting(requestParams);
        String searchType = requestParams.get("searchType").toString();
        String searchData = requestParams.get("searchData").toString();
        LocalDateTime startDt = requestParams.get("startDate").equals("") ? null : DateUtils.stringISOToLocalDateTime(requestParams.get("startDate").toString());
        LocalDateTime endDt = requestParams.get("endDate").equals("") ? null : DateUtils.stringISOToLocalDateTime(requestParams.get("endDate").toString());

        HistoryRequest historyRequest = new HistoryRequest();
        historyRequest.setStartDt(startDt);
        historyRequest.setEndDt(endDt);
        if(searchData != null && !searchData.isEmpty()) {
            if (searchType.equals("NAME")) {
                Optional<Admin> admin = adminMapper.findByEmail(searchData);
                if (admin.isPresent()) {
                    historyRequest.setUserMail(searchData);
                } else {
                    return HistoryResponse.builder()
                            .resultData(HistoryResponse.ResultData.builder()
                                    .message(ErrorCode.NOT_USER.toString())
                                    .build())
                            .status(CommonCode.ERROR.getHttpStatus())
                            .result(CommonCode.ERROR.getResult())
                            .build();
                }
            } else {
                historyRequest.setUserMail(searchData);
            }
        }

        List<BusinessLog> logList = historyLogService.loadHistoryData(historyRequest, BusinessLog.class);

        return HistoryResponse.builder()
                .resultData(HistoryResponse.ResultData.builder()
                        .list(logList)
                        .total(logList.size())
                        .totalAll(logList.size())
                        .build())
                .status(CommonCode.SUCCESS.getHttpStatus())
                .result(CommonCode.SUCCESS.getResult())
                .build();
    }

    public HistoryResponse getHistory(HistoryRequest historyRequest){

        List<BusinessLog> logList = historyLogService.loadHistoryData(historyRequest, BusinessLog.class);

        return HistoryResponse.builder()
                .resultData(HistoryResponse.ResultData.builder()
                        .list(logList)
                        .build())
                .status(CommonCode.SUCCESS.getHttpStatus())
                .result(CommonCode.SUCCESS.getResult())
                .build();
    }

    public HistoryResponse getHistoryDetail(String id){
        return HistoryResponse.builder()
                .resultData(HistoryResponse.ResultData.builder().build())
                .status(CommonCode.SUCCESS.getHttpStatus())
                .result(CommonCode.SUCCESS.getResult())
                .build();
    }

}
