package com.bankaudit.rest.process.controller;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;

import org.jboss.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.bankaudit.dto.process.ComplianceTrackingReportDto;
import com.bankaudit.dto.process.LettersAuditDto;
import com.bankaudit.dto.process.RatingFinancialParameterDto;
import com.bankaudit.dto.process.ReportDto;
import com.bankaudit.dto.process.ReportMISDto;
import com.bankaudit.rest.process.service.ReportService;
import com.bankaudit.util.BankAuditUtil;
import com.bankaudit.util.ServiceStatus;



@RestController

@RequestMapping("/api/reportController")
public class ReportController {
	
	@Autowired
	ReportService reportService;

	static final Logger logger = Logger.getLogger(ReportController.class);
	
	
@RequestMapping(value = "/getListofRisks", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus getListofRisks(@RequestParam(value = "legalEntityCode") String legalEntityCodeStr,
			@RequestParam(required=true,value = "userId") String userId,
			@RequestParam(required=true,value = "role") String role,
			@RequestParam(required=true,value = "unitId") String unitId,
			@RequestParam(required=false,value="asOfDate")
			@DateTimeFormat(pattern = "yyyy-MM-dd")LocalDate asOfDate,
			@RequestParam(required=false,value = "reportType") String reportType) {
		ServiceStatus serviceStatus = new ServiceStatus();

		logger.info("Inside getListofRisks .. "+ legalEntityCodeStr +" userId .."+ userId);
		if (!BankAuditUtil.isEmptyString(legalEntityCodeStr)) {

			try {
				List<List<ReportDto>> reports = reportService.getListofRisks(Integer.parseInt(legalEntityCodeStr), userId, unitId, role, asOfDate, reportType);

				if (reports != null && reports.size() > 0) {
					serviceStatus.setResult(reports);
					serviceStatus.setStatus("success");
					serviceStatus.setMessage("successfully retrieved");
				} else {
					serviceStatus.setStatus("failure");
					serviceStatus.setMessage("no documents found ");
				}

			} catch (Exception e) {

				e.printStackTrace();
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failure");

			}
		} else {

			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("Invalid payload");
		}

		return serviceStatus;
	}
	
	
@RequestMapping(value = "/getHealthIndexforRCSAUnits", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus getHealthIndexforRCSAUnits(@RequestParam(value = "legalEntityCode") String legalEntityCodeStr,
			@RequestParam(required=true,value = "userId") String userId,
			@RequestParam(required=true,value = "role") String role,
			@RequestParam(required=true,value = "unitId") String unitId,
			@RequestParam(required=false,value="asOfDate")
			@DateTimeFormat(pattern = "yyyy-MM-dd")LocalDate asOfDate,
			@RequestParam(required=false,value = "reportType") String reportType) {
		ServiceStatus serviceStatus = new ServiceStatus();

		logger.info("Inside getListofRisks .. "+ legalEntityCodeStr +" userId .."+ userId+" role.. "+role +" asOfDate.. "+asOfDate);
		if (!BankAuditUtil.isEmptyString(legalEntityCodeStr)) {

			try {
				List<List<ReportDto>> reports = reportService.getHealthIndexforRCSAUnits(Integer.parseInt(legalEntityCodeStr), userId, unitId, role, asOfDate, reportType);

				if (reports != null && reports.size() > 0) {
					serviceStatus.setResult(reports);
					serviceStatus.setStatus("success");
					serviceStatus.setMessage("successfully retrieved");
				} else {
					serviceStatus.setStatus("failure");
					serviceStatus.setMessage("no documents found ");
				}

			} catch (Exception e) {

				e.printStackTrace();
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failure");

			}
		} else {

			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("Invalid payload");
		}

		return serviceStatus;
	}
	
	
@RequestMapping(value = "/getRCSAProgressStatus", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus getRCSAProgressStatus(@RequestParam(value = "legalEntityCode") String legalEntityCodeStr,
			@RequestParam(required=true,value = "userId") String userId,
			@RequestParam(required=true,value = "role") String role,
			@RequestParam(required=true,value = "unitId") String unitId,
			@RequestParam(required=false,value="asOfDate")
			@DateTimeFormat(pattern = "yyyy-MM-dd")LocalDate asOfDate,
			@RequestParam(required=false,value = "reportType") String reportType) {
		ServiceStatus serviceStatus = new ServiceStatus();

		logger.info("Inside getListofRisks .. "+ legalEntityCodeStr +" userId .."+ userId+" role.. "+role +" asOfDate.. "+asOfDate);
		if (!BankAuditUtil.isEmptyString(legalEntityCodeStr)) {
			try {
				List<List<ReportDto>> reports = reportService.getRCSAProgressStatus(Integer.parseInt(legalEntityCodeStr), userId, unitId, role, asOfDate, reportType);
				if (reports != null && reports.size() > 0) {
					serviceStatus.setResult(reports);
					serviceStatus.setStatus("success");
					serviceStatus.setMessage("successfully retrieved");
				} else {
					serviceStatus.setStatus("failure");
					serviceStatus.setMessage("no documents found ");
				}
			} catch (Exception e) {
				e.printStackTrace();
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failure");
			}
		} else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("Invalid payload");
		}
		return serviceStatus;
	}
	
	
	/*********** This Service to be moved to the different Controller or to be deleted from here ***************/
	
@RequestMapping(value = "/updateDynamicStatements", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus updateDynamicStatements(@RequestParam(value = "legalEntityCode") String legalEntityCodeStr,
			@RequestParam(required=true,value = "auditId") String auditId,
			@RequestParam(required=true,value = "auditTypeCode") String auditTypeCode,
			@RequestParam(required=false,value="statementId") String statementId) {
		ServiceStatus serviceStatus = new ServiceStatus();

		logger.info("Inside updateDynamicStatements .. "+ legalEntityCodeStr +" auditId .."+ auditId+" auditTypeCode.. "+auditTypeCode +" statementId.. "+statementId);
		if (!BankAuditUtil.isEmptyString(legalEntityCodeStr)) {
			try {
				String status=reportService.updateDynamicStatements(Integer.parseInt(legalEntityCodeStr), auditId, auditTypeCode, statementId);
				if (status != null ) {
					serviceStatus.setResult(status);
					serviceStatus.setStatus("success");
					serviceStatus.setMessage("successfully retrieved");
				} else {
					serviceStatus.setStatus("failure");
					serviceStatus.setMessage("no documents found ");
				}
			} catch (Exception e) {
				e.printStackTrace();
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failure");
			}
		} else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("Invalid payload");
		}
		return serviceStatus;
	}

	
@GetMapping("/financialYearList")
	public ServiceStatus getFinancialYearList(@RequestParam(value = "legalEntityCode") String legalEntityCode) {
		ServiceStatus serviceStatus = new ServiceStatus();
		logger.info("Inside updateDynamicStatements .. "
					+ legalEntityCode);
		if (!BankAuditUtil.isEmptyString(legalEntityCode)) {
			try {
				List<Date> list = reportService.getFinancialYearList(legalEntityCode);
				serviceStatus.setResult(list);
				serviceStatus.setStatus("success");
				serviceStatus.setMessage("successfully retrieved");
			} catch (Exception e) {
				e.printStackTrace();
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failure");
			}
		} else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("Invalid payload");
		}
		return serviceStatus;
	}

	
@GetMapping("/complianceTrackingReport")
	public ServiceStatus getComplianceTrackingReport(@RequestParam(required = true, value = "legalEntityCode") Integer legalEntityCode,
			@RequestParam(required = true, value = "financialYear") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate financialYear,
			@RequestParam(required = true, value = "userId") String userId, @RequestParam(required = false, value = "auditTypeCode") String auditTypeCode) {
		ServiceStatus serviceStatus = new ServiceStatus();
		logger.info("Inside getComplianceTrackingReport .. " + legalEntityCode);
		try {
			List<ComplianceTrackingReportDto> list = reportService.getComplianceTrackingReport(legalEntityCode, userId, financialYear, auditTypeCode);
			serviceStatus.setResult(list);
			serviceStatus.setStatus("success");
			serviceStatus.setMessage("successfully retrieved");
		} catch (Exception e) {
			e.printStackTrace();
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("failure");
		}
		return serviceStatus;
	}
	
	
@GetMapping("/getAuditRatingList")
	public ServiceStatus getAuditRatingList(@RequestParam(required = true, value = "legalEntityCode") Integer legalEntityCode,
			@RequestParam(required = true, value = "financialYear") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate financialYear,
			@RequestParam(required = true, value = "userId") String userId, @RequestParam(required = false, value = "auditTypeCode") String auditTypeCode) {
		ServiceStatus serviceStatus = new ServiceStatus();
		logger.info("Inside getAuditRatingList .. " + legalEntityCode);
		try {
			List<RatingFinancialParameterDto> list = reportService.getAuditRatingList(legalEntityCode, userId, financialYear, auditTypeCode);
			serviceStatus.setResult(list);
			serviceStatus.setStatus("success");
			serviceStatus.setMessage("successfully retrieved");
		} catch (Exception e) {
			e.printStackTrace();
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("failure");
		}
		return serviceStatus;
	}
	
	
@GetMapping("/getAuditFinancialParameterList")
	public ServiceStatus getAuditFinancialParameterList(@RequestParam(required = true, value = "legalEntityCode") Integer legalEntityCode,
			@RequestParam(required = true, value = "financialYear") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate financialYear,
			@RequestParam(required = true, value = "userId") String userId, @RequestParam(required = false, value = "auditTypeCode") String auditTypeCode) {
		ServiceStatus serviceStatus = new ServiceStatus();
		logger.info("Inside getAuditRatingList .. " + legalEntityCode);
		try {
			List<RatingFinancialParameterDto> list = reportService.getAuditFinancialParameterList(legalEntityCode, userId, financialYear, auditTypeCode);
			serviceStatus.setResult(list);
			serviceStatus.setStatus("success");
			serviceStatus.setMessage("successfully retrieved");
		 } catch (Exception e) {
			e.printStackTrace();
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("failure");
		}
		return serviceStatus;
	}
	
	
@GetMapping("/getInspectionsDetail")
	public ServiceStatus getInspectionsDetail(
			@RequestParam(required = true, value = "legalEntityCode") Integer legalEntityCode,
			@RequestParam(required = true, value = "financialYear") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate financialYear,
			@RequestParam(required = true, value = "userId") String userId, 
			@RequestParam(required = false, value = "auditTypeCode") String auditTypeCode) {
		ServiceStatus serviceStatus = new ServiceStatus();
		logger.info("Inside getInspectionsDetail .. " + legalEntityCode);
		try {
			List<LettersAuditDto> list = reportService.getAuditDetail(legalEntityCode, userId, financialYear, auditTypeCode);
			serviceStatus.setResult(list);
			serviceStatus.setStatus("success");
			serviceStatus.setMessage("successfully retrieved");
		 } catch (Exception e) {
			e.printStackTrace();
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("failure");
		}
		return serviceStatus;
	}
	
	
@GetMapping("/getLettersDetail")
	public ServiceStatus getLettersDetail(
			@RequestParam(required = true, value = "legalEntityCode") Integer legalEntityCode,
			@RequestParam(required = true, value = "financialYear") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate financialYear,
			@RequestParam(required = true, value = "userId") String userId, 
			@RequestParam(required = false, value = "auditTypeCode") String auditTypeCode) {
		ServiceStatus serviceStatus = new ServiceStatus();
		logger.info("Inside getInspectionsDetail .. " + legalEntityCode);
		try {
			List<LettersAuditDto> list = reportService.getLettersDetail(legalEntityCode, userId, financialYear, auditTypeCode);
			serviceStatus.setResult(list);
			serviceStatus.setStatus("success");
			serviceStatus.setMessage("successfully retrieved");
		 } catch (Exception e) {
			e.printStackTrace();
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("failure");
		}
		return serviceStatus;
	}	
	
	/********************Start MIS Reports ***********************/
	
	
@GetMapping("/reportStatusFinalisationDateReport")
	public ServiceStatus reportStatusFinalisationDateMISReport(
			@RequestParam(required = true, value = "legalEntityCode") Integer legalEntityCode,
			@RequestParam(required = true, value = "financialYear") 
			@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate financialYear,
			@RequestParam(required = true, value = "userId") String userId, 
			@RequestParam(required = false, value = "auditTypeCode") String auditTypeCode) {
		ServiceStatus serviceStatus = new ServiceStatus();
		logger.info("Inside reportStatusFinalisationDateReport .. " + legalEntityCode);
		try {
			List<ReportMISDto> listReports = reportService.reportStatusFinalisationDateMISReport(legalEntityCode, userId, financialYear, auditTypeCode);
			serviceStatus.setResult(listReports);
			serviceStatus.setStatus("success");
			serviceStatus.setMessage("successfully retrieved");
		} catch (Exception e) {
			e.printStackTrace();
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("failure");
		}
		return serviceStatus;
	}
	
	
@GetMapping("/auditStatusReport")
	public ServiceStatus auditStatusMISReport(
			@RequestParam(required = true, value = "legalEntityCode") Integer legalEntityCode,
			@RequestParam(required = true, value = "financialYear") 
			@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate financialYear,
			@RequestParam(required = true, value = "userId") String userId, 
			@RequestParam(required = false, value = "auditTypeCode") String auditTypeCode) {
		ServiceStatus serviceStatus = new ServiceStatus();
		logger.info("Inside auditStatusReport .. " + legalEntityCode);
		try {
			List<ReportMISDto> listReports = reportService.auditStatusMISReport(legalEntityCode, userId, financialYear, auditTypeCode);
			serviceStatus.setResult(listReports);
			serviceStatus.setStatus("success");
			serviceStatus.setMessage("successfully retrieved");
		} catch (Exception e) {
			e.printStackTrace();
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("failure");
		}
		return serviceStatus;
	}
	
	
@GetMapping("/complianceStatusReport")
	public ServiceStatus complianceStatusMISReport(
			@RequestParam(required = true, value = "legalEntityCode") Integer legalEntityCode,
			@RequestParam(required = true, value = "financialYear") 
			@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate financialYear,
			@RequestParam(required = true, value = "userId") String userId, 
			@RequestParam(required = false, value = "auditTypeCode") String auditTypeCode) {
		ServiceStatus serviceStatus = new ServiceStatus();
		logger.info("Inside getComplianceTrackingReport .. " + legalEntityCode);
		try {
			List<ReportMISDto> listReports = reportService.complianceStatusMISReport(legalEntityCode, userId, financialYear, auditTypeCode);
			serviceStatus.setResult(listReports);
			serviceStatus.setStatus("success");
			serviceStatus.setMessage("successfully retrieved");
		} catch (Exception e) {
			e.printStackTrace();
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("failure");
		}
		return serviceStatus;
	}
	
	
@GetMapping("/supervisoryRatingMISReport")
	public ServiceStatus supervisoryRatingMISReport(
			@RequestParam(required = true, value = "legalEntityCode") Integer legalEntityCode,
			@RequestParam(required = true, value = "financialYear") 
			@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate financialYear,
			@RequestParam(required = true, value = "userId") String userId, 
			@RequestParam(required = false, value = "auditTypeCode") String auditTypeCode) {
		ServiceStatus serviceStatus = new ServiceStatus();
		logger.info("Inside supervisoryRatingMISReport .. " + legalEntityCode);
		try {
			List<ReportMISDto> listReports = reportService.supervisoryRatingMISReport(legalEntityCode, userId, financialYear, auditTypeCode);
			serviceStatus.setResult(listReports);
			serviceStatus.setStatus("success");
			serviceStatus.setMessage("successfully retrieved");
		} catch (Exception e) {
			e.printStackTrace();
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("failure");
		}
		return serviceStatus;
	}
	
	
@GetMapping("/supervisoryRating3YearsMISReport")
	public ServiceStatus supervisoryRating3YearsMISReport(
			@RequestParam(required = true, value = "legalEntityCode") Integer legalEntityCode,
			@RequestParam(required = true, value = "financialYear") 
			@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate financialYear,
			@RequestParam(required = true, value = "userId") String userId, 
			@RequestParam(required = false, value = "auditTypeCode") String auditTypeCode) {
		ServiceStatus serviceStatus = new ServiceStatus();
		logger.info("Inside supervisoryRating3YearsMISReport .. " + legalEntityCode);
		try {
			List<ReportMISDto> listReports = reportService.supervisoryRating3YearsMISReport(legalEntityCode, userId, financialYear, auditTypeCode);
			serviceStatus.setResult(listReports);
			serviceStatus.setStatus("success");
			serviceStatus.setMessage("successfully retrieved");
		} catch (Exception e) {
			e.printStackTrace();
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("failure");
		}
		return serviceStatus;
	}
	
	
@GetMapping("/lettersMISReport")
	public ServiceStatus lettersMISReport(
			@RequestParam(required = true, value = "legalEntityCode") Integer legalEntityCode,
			@RequestParam(required = true, value = "financialYear") 
			@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate financialYear,
			@RequestParam(required = true, value = "userId") String userId, 
			@RequestParam(required = false, value = "auditTypeCode") String auditTypeCode) {
		ServiceStatus serviceStatus = new ServiceStatus();
		logger.info("Inside lettersMISReport .. " + legalEntityCode);
		try {
			List<ReportMISDto> listReports = reportService.lettersMISReport(legalEntityCode, userId, financialYear, auditTypeCode);
			serviceStatus.setResult(listReports);
			serviceStatus.setStatus("success");
			serviceStatus.setMessage("successfully retrieved");
		} catch (Exception e) {
			e.printStackTrace();
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("failure");
		}
		return serviceStatus;
	}
	
	
@GetMapping("/qsoGradingMISReport")
	public ServiceStatus qsoGradingMISReport(
			@RequestParam(required = true, value = "legalEntityCode") Integer legalEntityCode,
			@RequestParam(required = true, value = "financialYear") 
			@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate financialYear,
			@RequestParam(required = true, value = "userId") String userId, 
			@RequestParam(required = false, value = "auditTypeCode") String auditTypeCode) {
		ServiceStatus serviceStatus = new ServiceStatus();
		logger.info("Inside qsoGradingMISReport .. " + legalEntityCode);
		try {
			List<ReportMISDto> listReports = reportService.qsoGradingMISReport(legalEntityCode, userId, financialYear, auditTypeCode);
			serviceStatus.setResult(listReports);
			serviceStatus.setStatus("success");
			serviceStatus.setMessage("successfully retrieved");
		} catch (Exception e) {
			e.printStackTrace();
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("failure");
		}
		return serviceStatus;
	}
	
	
@GetMapping("/complianceRatingMISReport")
	public ServiceStatus complianceRatingMISReport(
			@RequestParam(required = true, value = "legalEntityCode") Integer legalEntityCode,
			@RequestParam(required = true, value = "financialYear") 
			@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate financialYear,
			@RequestParam(required = true, value = "userId") String userId, 
			@RequestParam(required = false, value = "auditTypeCode") String auditTypeCode) {
		ServiceStatus serviceStatus = new ServiceStatus();
		logger.info("Inside complianceRatingMISReport .. " + legalEntityCode);
		try {
			List<ReportMISDto> listReports = reportService.complianceRatingMISReport(legalEntityCode, userId, financialYear, auditTypeCode);
			serviceStatus.setResult(listReports);
			serviceStatus.setStatus("success");
			serviceStatus.setMessage("successfully retrieved");
		} catch (Exception e) {
			e.printStackTrace();
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("failure");
		}
		return serviceStatus;
	}
	
	
@GetMapping("/complianceRating3YearsMISReport")
	public ServiceStatus complianceRating3YearsMISReport(
			@RequestParam(required = true, value = "legalEntityCode") Integer legalEntityCode,
			@RequestParam(required = true, value = "financialYear") 
			@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate financialYear,
			@RequestParam(required = true, value = "userId") String userId, 
			@RequestParam(required = false, value = "auditTypeCode") String auditTypeCode) {
		ServiceStatus serviceStatus = new ServiceStatus();
		logger.info("Inside complianceRating3YearsMISReport .. " + legalEntityCode);
		try {
			List<ReportMISDto> listReports = reportService.complianceRating3YearsMISReport(legalEntityCode, userId, financialYear, auditTypeCode);
			serviceStatus.setResult(listReports);
			serviceStatus.setStatus("success");
			serviceStatus.setMessage("successfully retrieved");
		} catch (Exception e) {
			e.printStackTrace();
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("failure");
		}
		return serviceStatus;
	}
	
	/********************End MIS Reports   ***********************/
}
