package com.interplug.qcast.biz.object;

import com.interplug.qcast.biz.object.dto.ObjectRequest;
import com.interplug.qcast.biz.object.dto.ObjectResponse;
import com.interplug.qcast.biz.object.dto.PlanReqRequest;
import com.interplug.qcast.biz.object.dto.PlanReqResponse;
import com.interplug.qcast.biz.object.dto.PlanRequest;
import com.interplug.qcast.biz.object.dto.PrefResponse;
import com.interplug.qcast.biz.object.dto.SaleStoreResponse;
import com.interplug.qcast.biz.object.dto.UploadRequest;
import com.interplug.qcast.biz.object.dto.WindSpeedResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
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;

@Slf4j
@RestController
@RequestMapping("/api/object")
@RequiredArgsConstructor
@Tag(name = "ObjectController", description = "물건정보 관련 API")
public class ObjectController {
  // @Autowired private ObjectService objectService;
  private final ObjectService objectService;

  @Operation(description = "물건정보 도도부현을 조회한다.")
  @GetMapping("/prefecture/list")
  @ResponseStatus(HttpStatus.OK)
  public List<PrefResponse> selectPrefList() throws Exception {
    return objectService.selectPrefList();
  }

  @Operation(description = "물건정보 도도부현 발전시뮬레이션 지역을 조회한다.")
  @GetMapping("/prefecture/{prefId}/list")
  @ResponseStatus(HttpStatus.OK)
  public List<PrefResponse> selectPrefAreaList(@PathVariable String prefId) throws Exception {
    return objectService.selectPrefAreaList(prefId);
  }

  @Operation(description = "물건정보 지역 기준풍속을 조회한다.")
  @GetMapping("/windSpeed/{city}/list")
  @ResponseStatus(HttpStatus.OK)
  public List<WindSpeedResponse> selectWindSpeedList(@PathVariable String city) throws Exception {
    return objectService.selectWindSpeedList(city);
  }

  @Operation(description = "1차점 판매점 목록을 조회한다.")
  @GetMapping("/saleStore/{saleStoreId}/firstList")
  @ResponseStatus(HttpStatus.OK)
  public List<SaleStoreResponse> selectFirstSaleStoreList(
      @PathVariable String saleStoreId, String userId) throws Exception {
    return objectService.selectFirstSaleStoreList(saleStoreId, userId);
  }

  @Operation(description = "판매점 목록을 조회한다.")
  @GetMapping("/saleStore/{saleStoreId}/list")
  @ResponseStatus(HttpStatus.OK)
  public List<SaleStoreResponse> selectSaleStoreList(
      @PathVariable String saleStoreId, String firstFlg, String userId) throws Exception {
    return objectService.selectSaleStoreList(saleStoreId, firstFlg, userId);
  }

  @Operation(description = "물건정보 목록을 조회한다.")
  @GetMapping("/list")
  @ResponseStatus(HttpStatus.OK)
  public List<ObjectResponse> selectObjectList(ObjectRequest objectRequest) throws Exception {
    return objectService.selectObjectList(objectRequest);
  }

  @Operation(description = "물건정보 상세를 조회한다.")
  @GetMapping("/{objectNo}/detail")
  @ResponseStatus(HttpStatus.OK)
  public ObjectResponse selectObjectDetail(@PathVariable String objectNo) throws Exception {
    return objectService.selectObjectDetail(objectNo);
  }

  @Operation(description = "물건정보을 저장한다.")
  @PostMapping("/save-object")
  @ResponseStatus(HttpStatus.CREATED)
  public ObjectResponse insertObject(@RequestBody ObjectRequest objectRequest) throws Exception {
    return objectService.insertObject(objectRequest);
  }

  @Operation(description = "물건정보을 수정한다.")
  @PutMapping("/save-object")
  @ResponseStatus(HttpStatus.CREATED)
  public ObjectResponse updateObject(@RequestBody ObjectRequest objectRequest) throws Exception {
    return objectService.updateObject(objectRequest);
  }

  @Operation(description = "물건정보을 삭제한다.")
  @DeleteMapping("/{objectNo}")
  @ResponseStatus(HttpStatus.NO_CONTENT)
  public void deleteObject(@PathVariable String objectNo, ObjectRequest objectRequest)
      throws Exception {
    objectRequest.setObjectNo(objectNo);
    objectService.deleteObject(objectRequest);
  }

  @Operation(description = "물건정보의 플랜정보를 추가한다.")
  @PostMapping("/add-plan")
  @ResponseStatus(HttpStatus.CREATED)
  public void insertPlan(@RequestBody PlanRequest planRequest) throws Exception {
    objectService.insertPlan(planRequest);
  }

  @Operation(description = "물건정보의 플랜정보를 삭제한다.")
  @DeleteMapping("/plan/{objectNo}/{planNo}")
  @ResponseStatus(HttpStatus.NO_CONTENT)
  public void deletePlan(@PathVariable String objectNo, @PathVariable String planNo)
      throws Exception {
    PlanRequest planRequest = new PlanRequest();
    planRequest.setObjectNo(objectNo);
    planRequest.setPlanNo(planNo);

    objectService.deletePlan(planRequest);
  }

  @Operation(description = "설계의뢰 목록을 조회한다.")
  @GetMapping("/planReq/list")
  @ResponseStatus(HttpStatus.OK)
  public PlanReqResponse selectPlanReqList(PlanReqRequest planReqRequest) throws Exception {
    return objectService.selectPlanReqList(planReqRequest);
  }

  @Operation(description = "견적서 파일을 다운로드한다.")
  @PostMapping("/file/{objectNo}/{no}")
  @ResponseStatus(HttpStatus.OK)
  public void fileDownload(
      HttpServletRequest request,
      HttpServletResponse response,
      @PathVariable String objectNo,
      @PathVariable String no)
      throws Exception {

    UploadRequest uploadRequest = new UploadRequest();
    uploadRequest.setObjectNo(objectNo);
    uploadRequest.setNo(no);

    objectService.fileDownload(request, response, uploadRequest);
  }
}
