package com.interplug.qcast.biz.excelDown;

import java.util.List;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import com.interplug.qcast.biz.excelDown.dto.NtrCtsCmpRequest;
import com.interplug.qcast.biz.excelDown.dto.NtrCtsCmpResponse;
import com.interplug.qcast.biz.excelDown.dto.QuotItemResponse;
import com.interplug.qcast.biz.excelDown.dto.QuotPlanResponse;
import com.interplug.qcast.biz.excelDown.dto.QuotRequest;
import com.interplug.qcast.biz.excelDown.dto.QuotResponse;
import com.interplug.qcast.biz.excelDown.dto.WrntIsncCmplRequest;
import com.interplug.qcast.biz.excelDown.dto.WrntIsncCmplResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequestMapping("/api/excel-down")
@RequiredArgsConstructor
@Tag(name = "ExcelDownController", description = "과거데이터 엑셀다운로드 API")
public class ExcelDownController {
  private final ExcelDownService excelDownService;

  @Operation(description = "과거데이터_견적 엑셀다운로드 조회")
  @PostMapping("/quot-excl-down-data")
  @ResponseStatus(HttpStatus.OK)
  public QuotResponse quotExclDownData(@RequestBody QuotRequest quotRequest) throws Exception {

    List<QuotPlanResponse> quotPlanExclDownData =
        excelDownService.selectQuotPlanExclDownData(quotRequest);
    List<QuotItemResponse> quotItemExclDownData =
        excelDownService.selectQuotItemExclDownData(quotRequest);

    QuotResponse quotRes = new QuotResponse();
    quotRes.setQuotPlanList(quotPlanExclDownData);
    quotRes.setQuotItemList(quotItemExclDownData);

    return quotRes;
  }

  @Operation(description = "과거데이터_자연재해보상입력 엑셀다운로드 조회")
  @PostMapping("/ntr-cts-cmp-excl-down-data")
  @ResponseStatus(HttpStatus.OK)
  public List<NtrCtsCmpResponse> ntrCtsCmpExclDownData(
      @RequestBody NtrCtsCmpRequest ntrCtsCmpRequest) throws Exception {
    return excelDownService.selectNtrCtsCmpExclDownData(ntrCtsCmpRequest);
  }

  @Operation(description = "과거데이터_보증서발행완료 물건 엑셀다운로드 조회")
  @PostMapping("/wrnt-isnc-cmpl-excl-down-data")
  @ResponseStatus(HttpStatus.OK)
  public List<WrntIsncCmplResponse> warrantyIssuedCmpExclDownData(
      @RequestBody WrntIsncCmplRequest wrntIsncCmplRequest) throws Exception {
    return excelDownService.selectWarrantyIssuedCmpExclDownData(wrntIsncCmplRequest);
  }

  @Operation(description = "과거데이터_보증서발행완료 물건 파일 다운로드 조회")
  @PostMapping("/wrnt-isnc-cmpl-file-down")
  public void warrantyIssuedCmpFileDown(
      @RequestBody WrntIsncCmplRequest wrntIsncCmplRequest, HttpServletResponse response)
      throws Exception {
    try {
      excelDownService.selectWarrantyIssuedCmpFileData(wrntIsncCmplRequest, response);
    } catch (Exception e) {
      // 파일 이슈가 있어서 다운로드 되지 않도록 처리
      log.error(e.getMessage());
    }
  }

}
