/* COUNT REMINDERS LINKED TO MEDICATION THAT'S VALID */
/* =============================================================================================================== */
DROP FUNCTION IF EXISTS COUNT_VALID_REMINDERS;
COMMIT;

delimiter //

CREATE FUNCTION `COUNT_VALID_REMINDERS`(`p_userId` varchar(36), `p_start` datetime, `p_end` datetime)
RETURNS INT
BEGIN

	DECLARE noReminders INT DEFAULT 0;
	
	SET noReminders = (
    SELECT COUNT(*)
    FROM `Reminders`
    WHERE `userId` = p_userId
    AND `timestamp` >= p_start
    AND `timestamp` < p_end
    AND `status` NOT IN ('inactive', 'delete')
    AND `dependant` IS NULL
  );
	
	RETURN noReminders;

END 
//
COMMIT //

delimiter ;
