package nuclibook.routes;

import javafx.util.Pair;
import nuclibook.constants.P;
import nuclibook.entity_utils.AbstractEntityUtils;
import nuclibook.entity_utils.ActionLogger;
import nuclibook.entity_utils.PatientUtils;
import nuclibook.entity_utils.SecurityUtils;
import nuclibook.models.*;
import spark.Request;
import spark.Response;

import javax.swing.plaf.synth.SynthTextAreaUI;
import java.io.IOException;

public class ImportRoute extends DefaultRoute {

	@Override
	public Object handle(Request request, Response response) throws Exception {
		prepareToHandle();

		// get request info
		String table = request.queryParams("table");
		String csvData = request.queryParams("csv-data");

		if (table.equals("patients")) {
			if (SecurityUtils.getCurrentUser() == null || !SecurityUtils.getCurrentUser().hasPermission(P.EDIT_PATIENTS)) {
				return "no_permission";
			}

			Integer[] importResult = new Integer[]{0, 0};
			try {
				 importResult = PatientUtils.importPatientsCSV(csvData);
			} catch (Exception e) {
				return "failed_validation";
			}

			return "OKAY:" + importResult[0].toString() + " rows successfully imported; " + importResult[1].toString() + " failed to import.";
		}

		// fail safe
		return "error";
	}
}

@@ -824,11 +824,13 @@
 n(P.
-EDI
+IMPOR
 T_PA
@@ -836,24 +836,153 @@
 ATIENTS)) %7B%0A
+%09%09%09%09ActionLogger.logAction(ActionLogger.ATTEMPT_IMPORT_PATIENTS, 0, %22Failed as user does not have permissions for this action%22);%0A
 %09%09%09%09return %22
@@ -1187,16 +1187,77 @@
 ;%0A%09%09%09%7D%0A%0A
+%09%09%09ActionLogger.logAction(ActionLogger.IMPORT_PATIENTS, 0);%0A%0A
 %09%09%09retur

Add log + permissions to import