package incheon.ags.dss.regen.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.egovframe.rte.ptl.mvc.tags.ui.pagination.PaginationInfo;
import org.springframework.web.multipart.MultipartFile;

import incheon.com.cmm.api.DefaultApiResponse;
import incheon.com.cmm.context.RequestContext;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import incheon.ags.dss.regen.service.UrbUserMdlDtlService;
import incheon.ags.dss.regen.vo.UrbUserMdlDtlVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@RestController 
@RequiredArgsConstructor
@Slf4j
@RequestMapping("/api/v1/dss/urb/userMdlDtl")
public class UrbUserMdlDtlApiController {

    private final UrbUserMdlDtlService urbUserMdlDtlService;

    @GetMapping("/list.do")
    public ResponseEntity<DefaultApiResponse> getUrbUserMdlList(
            @ModelAttribute UrbUserMdlDtlVO searchVO) throws Exception {
        
        log.info("API: getUrbUserMdlList (Page: {}, Keyword: {})", searchVO.getPageIndex(), searchVO.getSearchKeyword());
        
        PaginationInfo paginationInfo = new PaginationInfo();
        paginationInfo.setCurrentPageNo(searchVO.getPageIndex());
        paginationInfo.setRecordCountPerPage(searchVO.getRecordCountPerPage());
        paginationInfo.setPageSize(searchVO.getPageSize());

        searchVO.setFirstIndex(paginationInfo.getFirstRecordIndex());
        searchVO.setLOGIN_USER_ID(RequestContext.getCurrentUserId());

        List<UrbUserMdlDtlVO> list = urbUserMdlDtlService.selectUrbUserMdlDtlList(searchVO);
        int totalCount = urbUserMdlDtlService.selectUrbUserMdlDtlListCnt(searchVO);
        paginationInfo.setTotalRecordCount(totalCount);
        
        Map<String, Object> result = new HashMap<>();
        result.put("list", list);
        result.put("paging", paginationInfo);
        
        return ResponseEntity.ok(DefaultApiResponse.success(result, "조회되었습니다."));
    }

    @GetMapping("/detail.do")
    public ResponseEntity<DefaultApiResponse> getUrbUserMdlDetail(@RequestParam("userMdlNo") int userMdlNo) throws Exception {
        
        log.info("API: getUrbUserMdlDetail (userMdlNo: {})", userMdlNo);
        
        UrbUserMdlDtlVO vo = new UrbUserMdlDtlVO();
        vo.setUserMdlNo(userMdlNo);
        
        UrbUserMdlDtlVO resultSet = urbUserMdlDtlService.selectUrbUserMdlDtlDetail(vo);
        
        return ResponseEntity.ok(DefaultApiResponse.success(resultSet, "조회되었습니다."));
    }

    /**
     * 사용자 모델 정보 저장 (파일 업로드 포함)
     * (JSON이 아닌 'multipart/form-data'로 전송받음)
     */
    @PostMapping("/save.do")
    public ResponseEntity<DefaultApiResponse> saveUrbUserMdl(
            @ModelAttribute UrbUserMdlDtlVO vo, // 폼 메타데이터
            @RequestParam(value="file", required=false) MultipartFile file, // 업로드 파일
            HttpServletRequest request) throws Exception {
        
        log.info("API: saveUrbUserMdl (userMdlNo: {})", vo.getUserMdlNo());

        // ServiceImpl에서 파일 저장(if file exists) 및 DB 저장/수정
        int userMdlNo = urbUserMdlDtlService.saveUrbUserMdlDtl(vo, file);
        
        Map<String, Object> result = new HashMap<>();
        result.put("message", "저장되었습니다.");
        result.put("userMdlNo", userMdlNo);
        
        return ResponseEntity.ok(DefaultApiResponse.success(result, "저장되었습니다."));
    }

    @PostMapping("/delete.do")
    public ResponseEntity<DefaultApiResponse> deleteUrbUserMdl(
            @RequestParam("userMdlNo") int userMdlNo,
            HttpServletRequest request) throws Exception {
        
        log.info("API: deleteUrbUserMdl (userMdlNo: {})", userMdlNo);
        
        UrbUserMdlDtlVO vo = new UrbUserMdlDtlVO();
        vo.setUserMdlNo(userMdlNo);
        
        urbUserMdlDtlService.deleteUrbUserMdlDtl(vo);
        
        return ResponseEntity.ok(DefaultApiResponse.success(null, "삭제되었습니다."));
    }
}