package com.eactive.eai.rms.onl.apim.portalstatistics;

import com.eactive.eai.rms.common.base.BaseAnnotationController;
import com.eactive.eai.rms.common.combo.ComboService;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Map;

@Controller
public class PortalStatisticsDailyApiTableManController extends BaseAnnotationController {

	private final PortalStatisticsDailyApiTableManService portalStatisticsDailyApiTableManService;
	private final ComboService comboService;

	public PortalStatisticsDailyApiTableManController(PortalStatisticsDailyApiTableManService portalStatisticsDailyApiTableManService,
													  ComboService comboService) {
		this.portalStatisticsDailyApiTableManService = portalStatisticsDailyApiTableManService;
		this.comboService = comboService;
	}

	@GetMapping(value = "/onl/apim/portalstatistics/portalStatisticsDailyApiTableMan.view")
	public void view() {
		// view
	}

	@PostMapping(value = "/onl/apim/portalstatistics/portalStatisticsDailyApiTableMan.json", params = "cmd=LIST")
	public ResponseEntity<Map<String, Object>> selectStatisticsList(
			@RequestParam String startDate,
			@RequestParam String endDate,
			@RequestParam(required = false) String appId) {

		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
		LocalDate start = LocalDate.parse(startDate, formatter);
		LocalDate end = LocalDate.parse(endDate, formatter);

		Map<String, Object> result = portalStatisticsDailyApiTableManService.selectStatisticsList(start, end, appId);
		return ResponseEntity.ok(result);
	}

	@PostMapping(value = "/onl/apim/portalstatistics/portalStatisticsDailyApiTableMan.json", params = "cmd=LIST_INIT_COMBO")
	public ResponseEntity<Map<String, Object>> initCombo() {
		Map<String, Object> result = portalStatisticsDailyApiTableManService.getInitialComboData();
		return ResponseEntity.ok(result);
	}
}