package com.dbnt.kcscbackend.admin.committee;

import com.dbnt.kcscbackend.admin.committee.service.AdminCommitteeProgressStatusService;
import com.dbnt.kcscbackend.admin.config.entity.TcMenu;
import com.dbnt.kcscbackend.admin.config.entity.TnPartnerSite;
import com.dbnt.kcscbackend.admin.config.model.CreateCommitteeCodeManagementVO;
import com.dbnt.kcscbackend.admin.config.model.SetCommitteeCodeManagementVO;
import com.dbnt.kcscbackend.admin.config.service.AdminCommitteeCodeManagementService;
import com.dbnt.kcscbackend.admin.config.service.AdminConfigService;
import com.dbnt.kcscbackend.auth.entity.LoginVO;
import com.dbnt.kcscbackend.commonCode.entity.TcCodeGrp;
import com.dbnt.kcscbackend.commonCode.entity.TcCodeItem;
import com.dbnt.kcscbackend.commonCode.service.CommonCodeService;
import com.dbnt.kcscbackend.config.common.BaseController;
import com.dbnt.kcscbackend.config.common.ResponseCode;
import com.dbnt.kcscbackend.config.common.ResultVO;
import io.swagger.annotations.ApiParam;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.MediaType;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.Errors;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequiredArgsConstructor
@RequestMapping("/admin/committee")
@Tag(name="AdminCommitteeController", description = "사이트관리 위원회관리 메뉴 컨트롤러")
public class AdminCommitteeController extends BaseController {

  private final AdminConfigService adminConfigService;
  private final CommonCodeService commonCodeService;

  @Resource(name = "adminCommitteeProgressStatusService")
  private AdminCommitteeProgressStatusService adminCommitteeProgressStatusService;


  @Operation(
          summary = "'진행현황 관리' 페이지에서 목록 불러오는 API",
          description = "관리자 단에서 '위원회관리' > '진행현황 관리' 페이지에서 목록 불러오는 API",
          tags = {"AdminCommitteeController"}
  )
  @ApiResponses(value = {
          @ApiResponse(responseCode = "200", description = "조 회 성공"),
          @ApiResponse(responseCode = "303", description = "만료된 토큰"),
          @ApiResponse(responseCode = "403", description = "인가된 사용자가 아님")
  })
  @GetMapping(value = "/progress-status/list")
  public ResultVO getCommitteeProgressStatus(
          @AuthenticationPrincipal LoginVO user,
          HttpServletRequest request,
          Pageable pageable
          ) throws Exception {

    ResultVO resultVO = new ResultVO();
    if(user == null) {
      resultVO.setResultCode(ResponseCode.TOKEN_EXPIRED.getCode());
    } else {
      try {
        resultVO = adminCommitteeProgressStatusService.getCommitteeProgressStatus(resultVO, request, user, pageable);
      } catch (Exception e) {
        resultVO.setResultCode(ResponseCode.FAILED.getCode());
        resultVO.setResultMessage(e.getMessage());
      }
    }

    System.out.println(
      "\n--------------------------------------------------------------\n" +
      request.getRequestURI() + " OUT:" +
      "\n--------------------------------------------------------------\n" +
      "resultVO.toString():" + "\n" +
      resultVO.toString() + "\n" +
      "\n--------------------------------------------------------------\n"
    );

    return resultVO;
  }

  @Operation(
          summary = "'진행현황 관리' 페이지에서 코드명 눌렀을 때 보여질 상세 페이지 불러오는 API",
          description = "관리자 단에서 '위원회관리' > '진행현황 관리' 페이지에서 코드명 눌렀을 때 보여질 상세 페이지 목록 불러오는 API",
          tags = {"AdminCommitteeController"}
  )
  @ApiResponses(value = {
          @ApiResponse(responseCode = "200", description = "조회 성공")
  })
  @GetMapping(value = "/progress-status/{drftSeq}")
  public ResultVO getCommitteeProgressStatusDetail(
          HttpServletRequest request,
          @AuthenticationPrincipal LoginVO user,
          @PathVariable("drftSeq") Long drftSeq
  )
          throws Exception {

    ResultVO resultVO = new ResultVO();

    try {
      resultVO = adminCommitteeProgressStatusService.getCommitteeProgressStatusDetail(resultVO, request, user, drftSeq);
    } catch (Exception e) {
      resultVO.setResultCode(ResponseCode.FAILED.getCode());
      resultVO.setResultMessage(e.getMessage());
    }


    System.out.println(
            "\n--------------------------------------------------------------\n" +
                    request.getRequestURI() + " OUT:" +
                    "\n--------------------------------------------------------------\n" +
                    "resultVO.toString():" + "\n" +
                    resultVO.toString() + "\n" +
                    "\n--------------------------------------------------------------\n"
    );

    return resultVO;
  }


}
