<?php
	define('PREPEND_PATH', '');
	include_once(__DIR__ . '/lib.php');

	// accept a record as an assoc array, return transformed row ready to insert to table
	$transformFunctions = [
		'patients' => function($data, $options = []) {
			if(isset($data['birth_date'])) $data['birth_date'] = guessMySQLDateTime($data['birth_date']);
			if(isset($data['home_phone'])) $data['home_phone'] = str_replace('-', '', $data['home_phone']);
			if(isset($data['work_phone'])) $data['work_phone'] = str_replace('-', '', $data['work_phone']);
			if(isset($data['mobile'])) $data['mobile'] = str_replace('-', '', $data['mobile']);
			if(isset($data['filed'])) $data['filed'] = guessMySQLDateTime($data['filed']);
			if(isset($data['last_modified'])) $data['last_modified'] = guessMySQLDateTime($data['last_modified']);

			return $data;
		},
		'disease_symptoms' => function($data, $options = []) {

			return $data;
		},
		'medical_records' => function($data, $options = []) {
			if(isset($data['patient'])) $data['patient'] = pkGivenLookupText($data['patient'], 'medical_records', 'patient');

			return $data;
		},
		'events' => function($data, $options = []) {
			if(isset($data['date'])) $data['date'] = guessMySQLDateTime($data['date']);
			if(isset($data['name_patient'])) $data['name_patient'] = pkGivenLookupText($data['name_patient'], 'events', 'name_patient');

			return $data;
		},
	];

	// accept a record as an assoc array, return a boolean indicating whether to import or skip record
	$filterFunctions = [
		'patients' => function($data, $options = []) { return true; },
		'disease_symptoms' => function($data, $options = []) { return true; },
		'medical_records' => function($data, $options = []) { return true; },
		'events' => function($data, $options = []) { return true; },
	];

	/*
	Hook file for overwriting/amending $transformFunctions and $filterFunctions:
	hooks/import-csv.php
	If found, it's included below

	The way this works is by either completely overwriting any of the above 2 arrays,
	or, more commonly, overwriting a single function, for example:
		$transformFunctions['tablename'] = function($data, $options = []) {
			// new definition here
			// then you must return transformed data
			return $data;
		};

	Another scenario is transforming a specific field and leaving other fields to the default
	transformation. One possible way of doing this is to store the original transformation function
	in GLOBALS array, calling it inside the custom transformation function, then modifying the
	specific field:
		$GLOBALS['originalTransformationFunction'] = $transformFunctions['tablename'];
		$transformFunctions['tablename'] = function($data, $options = []) {
			$data = call_user_func_array($GLOBALS['originalTransformationFunction'], [$data, $options]);
			$data['fieldname'] = 'transformed value';
			return $data;
		};
	*/

	@include(__DIR__ . '/hooks/import-csv.php');

	$ui = new CSVImportUI($transformFunctions, $filterFunctions);
