/* SYNC MEDICATIONS AND REMINDERS */
/* =============================================================================================================== */
drop procedure if exists updateMedicationsAndReminders;
commit;

delimiter //

CREATE PROCEDURE `updateMedicationsAndReminders`(`p_transaction` varchar(36), `p_medicationCount` int, `p_reminderCount` int)

BEGIN

DECLARE errno int;
DECLARE iMedicationCount int;
DECLARE iReminderCount int;

DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
  GET CURRENT DIAGNOSTICS CONDITION 1 errno = MYSQL_ERRNO;
  ROLLBACK;

	INSERT INTO debug_log(activity, ts_when, log) 
  SELECT 'PROC ERR: syncUMeds', now(), errno; 

  RESIGNAL;
END;

START TRANSACTION;

SELECT COUNT(`remoteId`)
FROM `TempMedications`
WHERE `transaction` = p_transaction
INTO iMedicationCount;
SELECT COUNT(`remoteId`)
FROM `TempReminders`
WHERE `transaction` = p_transaction
INTO iReminderCount;

IF iMedicationCount <> p_medicationCount THEN 
  SIGNAL SQLSTATE '45000'
  SET MESSAGE_TEXT = 'Temp medication count does not match provided medication count.';
ELSEIF iReminderCount <> p_reminderCount THEN
  SIGNAL SQLSTATE '45000'
  SET MESSAGE_TEXT = 'Temp reminder count does not match provided reminder count.';
END IF;

IF p_medicationCount IS NOT NULL AND p_medicationCount > 0 THEN
  INSERT INTO `Medications`(`remoteId`, `userId`, `dependant`, `medicineId`, `dosage`, `amount`, `remaining`, 
    `reminders`, `frequency`, `weeklyFrequency`, `startDate`, `endDate`, `times`, `isChronic`, `status`, 
    `updatedAt`, `createdAt`)
  SELECT `temp`.`remoteId`, `temp`.`userId`, `temp`.`dependant`, `temp`.`medicineId`, `temp`.`dosage`, 
    `temp`.`amount`, `temp`.`remaining`, `temp`.`reminders`, `temp`.`frequency`, `temp`.`weeklyFrequency`, 
    `temp`.`startDate`, `temp`.`endDate`, `temp`.`times`, `temp`.`isChronic`, `temp`.`status`, 
    `temp`.`updatedAt`, `temp`.`createdAt`
  FROM `TempMedications` `temp`
  WHERE `temp`.`transaction` = p_transaction
  ON DUPLICATE KEY
  UPDATE `dosage` = `temp`.`dosage`, 
    `remaining` = `temp`.`remaining`, 
    `frequency` = `temp`.`frequency`, 
    `weeklyFrequency` = `temp`.`weeklyFrequency`, 
    `startDate` = `temp`.`startDate`, 
    `times` = `temp`.`times`, 
    `isChronic` = `temp`.`isChronic`, 
    `status` = `temp`.`status`;
END IF;

INSERT INTO `Reminders`(`remoteId`, `userId`, `medicationId`, `medicineId`, `dosage`, `timestamp`, `dependant`, 
  `skippedReason`, `actionedAt`, `status`, `updatedAt`, `createdAt`)
SELECT `temp`.`remoteId`, `temp`.`userId`, `temp`.`medicationId`, `temp`.`medicineId`, `temp`.`dosage`, 
  `temp`.`timestamp`, `temp`.`dependant`, `temp`.`skippedReason`, `temp`.`actionedAt`, `temp`.`status`,
  `temp`.`updatedAt`, `temp`.`createdAt`
FROM `TempReminders` `temp`
WHERE `temp`.`transaction` = p_transaction
ON DUPLICATE KEY
UPDATE `dosage` = `temp`.`dosage`,
  `timestamp` = `temp`.`timestamp`,
  `skippedReason` = `temp`.`skippedReason`,
  `actionedAt` = `temp`.`actionedAt`,
  `status` = `temp`.`status`,
  `updatedAt` = `temp`.`updatedAt`;

CALL getMedicationSyncPayload(p_transaction);

COMMIT WORK;

END
//

commit //

delimiter ;
