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.UrbBcrnImgDtlService;
import incheon.ags.dss.regen.vo.UrbBcrnImgDtlVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

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

    private final UrbBcrnImgDtlService urbBcrnImgDtlService;

    @GetMapping("/list.do")
    public ResponseEntity<DefaultApiResponse> getUrbBcrnImgDtlList(
            @ModelAttribute UrbBcrnImgDtlVO searchVO) throws Exception {
        
        log.info("API: getUrbBcrnImgDtlList (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<UrbBcrnImgDtlVO> list = urbBcrnImgDtlService.selectUrbBcrnImgDtlList(searchVO);
        int totalCount = urbBcrnImgDtlService.selectUrbBcrnImgDtlListCnt(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> getUrbBcrnImgDtlDetail(@RequestParam("imgNo") int imgNo) throws Exception {
        
        log.info("API: getUrbBcrnImgDtlDetail (imgNo: {})", imgNo);
        
        UrbBcrnImgDtlVO vo = new UrbBcrnImgDtlVO();
        vo.setImgNo(imgNo);
        
        UrbBcrnImgDtlVO resultSet = urbBcrnImgDtlService.selectUrbBcrnImgDtlDetail(vo);
        
        return ResponseEntity.ok(DefaultApiResponse.success(resultSet, "조회되었습니다."));
    }

    /**
     * 배경 이미지 정보 저장 (파일 업로드 포함)
     * (JSON이 아닌 'multipart/form-data'로 전송받음)
     */
    @PostMapping("/save.do")
    public ResponseEntity<DefaultApiResponse> saveUrbBcrnImgDtl(
            @ModelAttribute UrbBcrnImgDtlVO vo, // 4개 좌표(WKT) 및 imgNo 등
            @RequestParam(value="file", required=false) MultipartFile file, // 업로드 파일
            HttpServletRequest request) throws Exception {
        
        log.info("API: saveUrbBcrnImgDtl (imgNo: {})", vo.getImgNo());
        
        // ServiceImpl에서 파일 저장(if file exists) 및 DB 저장/수정
        int imgNo = urbBcrnImgDtlService.saveUrbBcrnImgDtl(vo, file);
        
        Map<String, Object> result = new HashMap<>();
        result.put("message", "저장되었습니다.");
        result.put("imgNo", imgNo);
        
        return ResponseEntity.ok(DefaultApiResponse.success(result, "저장되었습니다."));
    }

    @PostMapping("/delete.do")
    public ResponseEntity<DefaultApiResponse> deleteUrbBcrnImgDtl(
            @RequestParam("imgNo") int imgNo,
            HttpServletRequest request) throws Exception {
        
        log.info("API: deleteUrbBcrnImgDtl (imgNo: {})", imgNo);
        
        UrbBcrnImgDtlVO vo = new UrbBcrnImgDtlVO();
        vo.setImgNo(imgNo);
        
        urbBcrnImgDtlService.deleteUrbBcrnImgDtl(vo);
        
        return ResponseEntity.ok(DefaultApiResponse.success(null, "삭제되었습니다."));
    }
}