package com.demo1.board.api;

import java.io.IOException;
import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.demo1.board.api.dto.BoardRequest;
import com.demo1.board.api.dto.BoardResponse;
import com.demo1.board.domain.Board;
import com.demo1.board.service.BoardService;
import com.demo1.exception.CustomExceptionCode;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.lotte.chamomile.core.exception.BusinessException;
import net.lotte.chamomile.module.excel.ExcelExporter;
import net.lotte.chamomile.module.excel.ExcelImporter;
import net.lotte.chamomile.module.file.entity.FileMetadataInfoEntity;
import net.lotte.chamomile.module.file.util.FileDownloader;
import net.lotte.chamomile.module.file.util.FileUploader;
import net.lotte.chamomile.module.logging.LoggingUtils;
import net.lotte.chamomile.module.logging.type.UserAccessActionType;

@RestController
@RequestMapping("/api/boards")
@RequiredArgsConstructor
@Slf4j
public class BoardController implements BoardControllerDoc {
	
	//private Logger log = LoggerFactory.getLogger(BoardController.class);

	private final BoardService boardService;
	private final FileDownloader fileDownloader;
	private final FileUploader fileUploader;

	@GetMapping("")
	public ResponseEntity<List<BoardResponse>> getBoards() {
		List<Board> boards = boardService.getBoards();
		List<BoardResponse> response = BoardResponse.listOf(boards);
		LoggingUtils.userAccessLogging("test", UserAccessActionType.LOGIN_SUCCESS);
		return ResponseEntity.ok().body(response);
	}
	
	@GetMapping("/page")
	public ResponseEntity<Page<BoardResponse>> pageBoards(@PageableDefault(size = 10) Pageable pageable) {
		Page<Board> boards = boardService.pageBoards(pageable);
		Page<BoardResponse> response = BoardResponse.pageOf(boards);
		return ResponseEntity.ok().body(response);
	}

	@GetMapping("/exception")
	public void exception() throws BusinessException {
		log.info("HelloRestController.exception() called.");
		throw CustomExceptionCode.LOGIN_ERROR.exception();
	}

	@GetMapping("/exception2")
	public void exception2() {
		log.info("HelloRestController.exception2() called.");
		throw new IllegalArgumentException("exception occurred");
	}

	@GetMapping("/{id}")
	public ResponseEntity<BoardResponse> getBoard(@PathVariable Long id) {
		Board board = boardService.getBoard(id);
		BoardResponse response = BoardResponse.of(board);
		return ResponseEntity.ok().body(response);
	}

	@PostMapping("")
	public ResponseEntity<BoardResponse> createBoard(@RequestBody BoardRequest request) {
		Board board = boardService.createBoard(request);
		BoardResponse response = BoardResponse.of(board);
		return ResponseEntity.ok().body(response);
	}

	@PutMapping("/{id}")
	public ResponseEntity<BoardResponse> updateBoard(@PathVariable Long id, @RequestBody BoardRequest request) {
		Board board = boardService.updateBoard(id, request);
		BoardResponse response = BoardResponse.of(board);
		return ResponseEntity.ok().body(response);
	}

	@DeleteMapping("/{id}")
	public ResponseEntity<Long> deleteBoard(@PathVariable Long id) {
		Long deletedBoardId = boardService.deleteBoard(id);
		return ResponseEntity.ok().body(deletedBoardId);
	}
	
	@GetMapping("/template")
	public ResponseEntity<byte[]> template() {
		return fileDownloader.fileDownload("sample_board.xlsx", "template.xlsx");
	}
	
	@PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
	public void fileUpload(@RequestParam("file") MultipartFile file) throws Exception {
	    FileMetadataInfoEntity[] fileVo = fileUploader.fileUpload(
	        new MultipartFile[] {file});
	    List<BoardRequest> list = new ExcelImporter().toCustomClass(fileVo[0].getRealFilePath(), BoardRequest.class);
//	    for (BoardRequest board : list) {
//			log.warn("{}", board);
//			//boardService.createBoard(board);
//		}
	    boardService.insertBulk(list);
	}
	
	@GetMapping("/download")
	public ResponseEntity<byte[]> download() throws IOException {
		ExcelExporter excel = new ExcelExporter("board_list");
		List<Board> boards = boardService.getBoards();
		excel.addDataList(boards);
		return excel.toHttpResponse();
	}
}










