package com.takensoft.cms.dept.web;

import com.takensoft.cms.author.service.AuthorService;
import com.takensoft.cms.author.vo.AuthorVO;
import com.takensoft.cms.dept.service.DeptService;
import com.takensoft.cms.dept.vo.DeptMbrVO;
import com.takensoft.cms.dept.vo.DeptVO;
import com.takensoft.common.HierachyVO;
import com.takensoft.common.util.ResponseData;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author  : takensoft
 * @since   : 2024.04.24
 *
 * 부서 정보 관련 컨트롤러
 */
@RestController
@RequiredArgsConstructor
@Slf4j
@RequestMapping("/admin/dept")
public class DeptController {

    private final DeptService deptService;
    private final AuthorService authorService;

    /**
     * @author takensoft
     * @since 2024.04.25
     * @param deptVO
     * @return
     * @throws Exception
     * 
     * 부서 등록
     */
    @PostMapping("/saveProc.json")
    public ResponseEntity<?> saveProc(@RequestBody DeptVO deptVO) throws Exception {
        // 부서 등록
        HashMap<String, Object> result = deptService.deptSave(deptVO);
        int insertResult = (int) result.get("insertResult");

        // 응답 처리
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(new MediaType("application", "json", Charset.forName("UTF-8")));
        ResponseData responseData = new ResponseData();

        if(insertResult > 0) {
            responseData.setStatus(HttpStatus.OK);
            responseData.setMessage("정상적으로 등록 처리되었습니다.");
            responseData.setData(result);
            return new ResponseEntity<>(responseData, headers, HttpStatus.OK);
        } else {
            responseData.setStatus(HttpStatus.INTERNAL_SERVER_ERROR);
            responseData.setMessage("등록에 실패하였습니다.\n담당자에게 문의하세요.");
            return new ResponseEntity<>(responseData, headers, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * @author takensoft
     * @since 2024.04.29
     * @param deptMbrVO
     * @return
     * @throws Exception
     *
     * 부서 사용자 등록
     */
    @PostMapping("/deptMbrSaveProc.json")
    public ResponseEntity<?> deptMbrSaveProc(@RequestBody DeptMbrVO deptMbrVO) throws Exception {
        // 부서 사용자 등록
        int result = deptService.deptMbrSave(deptMbrVO);

        // 응답 처리
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(new MediaType("application", "json", Charset.forName("UTF-8")));
        ResponseData responseData = new ResponseData();

        if(result > 0) {
            responseData.setStatus(HttpStatus.OK);
            responseData.setMessage("정상적으로 등록 처리되었습니다.");
            return new ResponseEntity<>(responseData, headers, HttpStatus.OK);
        } else {
            responseData.setStatus(HttpStatus.INTERNAL_SERVER_ERROR);
            responseData.setMessage("등록에 실패하였습니다.\n담당자에게 문의하세요.");
            return new ResponseEntity<>(responseData, headers, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * @author takensoft
     * @since 2024.04.25
     * @param
     * @return
     * @throws Exception
     *
     * 부서 목록 조회
     */
    @GetMapping(value = "/findAll.json")
    public ResponseEntity<?> findAll() throws Exception {
        // Tree용
        List<HierachyVO> hierachyList = deptService.findByTopNode();
        // 권한 목록 조회
        List<AuthorVO> authList = authorService.findAllSystem();

        Map<String, Object> result = new HashMap<String, Object>();
        result.put("hierachyList", hierachyList);
        result.put("authList", authList);
        result.put("newDept", new DeptVO());

        // 응답 처리
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(new MediaType("application", "json", Charset.forName("UTF-8")));
        ResponseData responseData = new ResponseData();
        responseData.setStatus(HttpStatus.OK);
        responseData.setMessage("정상적으로 조회가 처리되었습니다.");
        responseData.setData(result);
        return new ResponseEntity<>(responseData, headers, HttpStatus.OK);
    }

    /**
     * @author takensoft
     * @since 2024.04.26
     * @param
     * @return
     * @throws Exception
     *
     * 부서정보 상세 조회
     */
    @PostMapping(value = "/findByDept.json")
    public ResponseEntity<?> findByDept(@RequestBody HashMap<String, Object> params) throws Exception {
        // 부서정보 조회
        Map<String, Object> result = deptService.findByDept(params);

        // 응답 처리
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(new MediaType("application", "json", Charset.forName("UTF-8")));
        ResponseData responseData = new ResponseData();
        responseData.setStatus(HttpStatus.OK);
        responseData.setMessage("정상적으로 조회 처리되었습니다.");
        responseData.setData(result);
        return new ResponseEntity<>(responseData, headers, HttpStatus.OK);
    }

    /**
     * @author takensoft
     * @since 2024.04.26
     * @param deptVO
     * @return
     * @throws Exception
     *
     * 부서 수정
     */
    @PostMapping(value = "/updateProc.json")
    public ResponseEntity<?> updateProc(@RequestBody DeptVO deptVO) throws Exception {
        // 부서 수정
        int result = deptService.deptUpdate(deptVO);

        // 응답 처리
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(new MediaType("application", "json", Charset.forName("UTF-8")));
        ResponseData responseData = new ResponseData();

        if(result > 0) {
            responseData.setStatus(HttpStatus.OK);
            responseData.setMessage("정상적으로 수정 처리되었습니다.");
            return new ResponseEntity<>(responseData, headers, HttpStatus.OK);
        } else {
            responseData.setStatus(HttpStatus.INTERNAL_SERVER_ERROR);
            responseData.setMessage("수정에 실패하였습니다.\n담당자에게 문의하세요.");
            return new ResponseEntity<>(responseData, headers, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * @author takensoft
     * @since 2024.04.26
     * @param deptVO
     * @return
     * @throws Exception
     *
     * 부서 삭제
     */
    @PostMapping(value = "/deleteProc.json")
    public ResponseEntity<?> deleteProc(@RequestBody DeptVO deptVO) throws Exception {
        // 부서 수정
        int result = deptService.deptDelete(deptVO);

        // 응답 처리
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(new MediaType("application", "json", Charset.forName("UTF-8")));
        ResponseData responseData = new ResponseData();

        if(result > 0) {
            responseData.setStatus(HttpStatus.OK);
            responseData.setMessage("정상적으로 삭제 처리되었습니다.");
            return new ResponseEntity<>(responseData, headers, HttpStatus.OK);
        } else {
            responseData.setStatus(HttpStatus.INTERNAL_SERVER_ERROR);
            responseData.setMessage("삭제에 실패하였습니다.\n담당자에게 문의하세요.");
            return new ResponseEntity<>(responseData, headers, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * @author takensoft
     * @since 2024.04.26
     * @param
     * @return
     * @throws Exception
     *
     * 부서 사용자 삭제
     */
    @PostMapping(value = "/deptMbrDelProc.json")
    public ResponseEntity<?> deptMbrDelProc(@RequestBody List<DeptMbrVO> deptMbrList) throws Exception {
        // 부서 사용자 삭제
        int result = deptService.deptMbrDelete(deptMbrList);

        // 응답 처리
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(new MediaType("application", "json", Charset.forName("UTF-8")));
        ResponseData responseData = new ResponseData();

        if(result > 0) {
            responseData.setStatus(HttpStatus.OK);
            responseData.setMessage("정상적으로 등록 처리되었습니다.");
            return new ResponseEntity<>(responseData, headers, HttpStatus.OK);
        } else {
            responseData.setStatus(HttpStatus.INTERNAL_SERVER_ERROR);
            responseData.setMessage("등록에 실패하였습니다.\n담당자에게 문의하세요.");
            return new ResponseEntity<>(responseData, headers, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * @author takensoft
     * @since 2024.04.29
     * @param
     * @return
     * @throws Exception
     *
     * 부서에 등록되지 않는 사용자 조회
     */
    @PostMapping("/findByMbr.json")
    public ResponseEntity<?> findByMbr(@RequestBody Map<String, String> params) throws Exception {
        // 사용자 목록 조회
        Map<String, Object> result = deptService.findByMbr(params);

        // 응답 처리
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(new MediaType("application", "json", Charset.forName("UTF-8")));
        ResponseData responseData = new ResponseData();
        responseData.setStatus(HttpStatus.OK);
        responseData.setMessage("정상적으로 조회가 처리되었습니다.");
        responseData.setData(result);
        return new ResponseEntity<>(responseData, headers, HttpStatus.OK);
    }

    /**
     * @author 박정하
     * @since 2024.05.09
     * @param deptList
     * @return
     * @throws Exception
     *
     * 부서 목록 수정
     */
    @PostMapping(value = "/updateListProc.json")
    public ResponseEntity<?> updateListProc(@RequestBody List<HierachyVO> deptList) throws Exception {
        // 부서 목록 수정
        int result = deptService.updateList(deptList);

        // 응답 처리
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(new MediaType("application", "json", Charset.forName("UTF-8")));
        ResponseData responseData = new ResponseData();
        if(result > 0) {
            responseData.setStatus(HttpStatus.OK);
            responseData.setMessage("정상적으로 수정 처리되었습니다.");
            return new ResponseEntity<>(responseData, headers, HttpStatus.OK);
        } else {
            responseData.setStatus(HttpStatus.INTERNAL_SERVER_ERROR);
            responseData.setMessage("수정에 실패하였습니다.\n담당자에게 문의하세요.");
            return new ResponseEntity<>(responseData, headers, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
}
