package com.interplug.qcast.biz.excelDown;

import com.interplug.qcast.biz.excelDown.dto.*;
import com.interplug.qcast.config.message.Messages;
import com.interplug.qcast.util.ZipFileManager;
import jakarta.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@RequiredArgsConstructor
public class ExcelDownService {
  private final ExcelDownMapper excelDownMapper;

  private final ZipFileManager zipFileManager;

  Messages message;

  @Value("${file.ini.root.path}")
  private String baseDirPath;

  /**
   * 과거데이터_견적 엑셀다운로드 조회
   *
   * @param quotRequest
   * @return
   * @throws Exception
   */
  public List<QuotPlanResponse> selectQuotPlanExclDownData(QuotRequest quotRequest)
      throws Exception {
    return excelDownMapper.selectQuotPlanExclDownData(quotRequest);
  }

  /**
   * 과거데이터_견적 엑셀다운로드 조회(아이템)
   *
   * @param quotRequest
   * @return
   * @throws Exception
   */
  public List<QuotItemResponse> selectQuotItemExclDownData(QuotRequest quotRequest)
      throws Exception {
    return excelDownMapper.selectQuotItemExclDownData(quotRequest);
  }

  /**
   * 과거데이터_자연재해보상입력 엑셀다운로드 조회
   *
   * @param ntrCtsCmpRequest
   * @return
   * @throws Exception
   */
  public List<NtrCtsCmpResponse> selectNtrCtsCmpExclDownData(NtrCtsCmpRequest ntrCtsCmpRequest)
      throws Exception {
    return excelDownMapper.selectNtrCtsCmpExclDownData(ntrCtsCmpRequest);
  }

  /**
   * 과거데이터_보증서발행완료 물건 엑셀다운로드 조회
   *
   * @param wrntIsncCmplRequest
   * @return
   * @throws Exception
   */
  public List<WrntIsncCmplResponse> selectWarrantyIssuedCmpExclDownData(
      WrntIsncCmplRequest wrntIsncCmplRequest) throws Exception {
    return excelDownMapper.selectWarrantyIssuedCmpExclDownData(wrntIsncCmplRequest);
  }

  /**
   * 과거데이터_보증서발행완료 물건 파일 다운로드 조회
   *
   * @param wrntIsncCmplRequest
   * @param response
   * @throws Exception
   */
  public void selectWarrantyIssuedCmpFileData(
      WrntIsncCmplRequest wrntIsncCmplRequest, HttpServletResponse response) throws Exception {
    List<WrntIsncCmplFileResponse> listData =
        excelDownMapper.selectWarrantyIssuedCmpFileData(wrntIsncCmplRequest);

    if (listData != null && listData.size() > 0) {
      // 파일 목록 설정
      List<Map<String, String>> listFile = new ArrayList<Map<String, String>>();
      Map<String, String> mapFile = null;

      for (WrntIsncCmplFileResponse res : listData) {
        mapFile = new HashMap<String, String>();
        mapFile.put("directory", res.getObjectNo());
        mapFile.put("filename", baseDirPath + "\\" + res.getObjectNo() + "\\" + res.getFaileName());
        listFile.add(mapFile);
      }

      zipFileManager.createZipFile(response, "Warranty-issued-completed-files", listFile);

    } else {
      // 데이터가 없을 경우 NO_CONTENT 상태 코드 설정 후 종료
      response.setStatus(HttpServletResponse.SC_NO_CONTENT);
      response.getWriter().write(message.getMessage("common.message.no.dataDown"));
      response.flushBuffer();
    }
  }
}
