-- Add new task email

SET @other_notification_type := (SELECT id FROM ohrm_notification_type where `name`='Other');
SET @email_media := (SELECT id FROM ohrm_notification_media WHERE `name` = 'Email');
SET @onboarding_module_id := (SELECT `id` FROM `ohrm_module` WHERE  `name` ='onboarding');
SET @admin_user_role := (SELECT id FROM ohrm_user_role WHERE `name` = 'Admin');
SET @supervisor_user_role := (SELECT id FROM ohrm_user_role WHERE `name` = 'Supervisor');
SET @task_owner_user_role := (SELECT id FROM ohrm_user_role WHERE `name` = 'OhrmTaskOwner');
Delete from `ohrm_notification` where module_id = @onboarding_module_id;
INSERT INTO `ohrm_notification`
(`name`, `is_enable`,`type_id`, `generator_class`, `parameters`, `workflow_id`, `workflow_action`,`module_id`, `hidden`) VALUES
    ('On/Offboarding - New Task Created Notification', 1, @other_notification_type, 'LegacyEmailProcessorWrapperEventNotification',
     '{"email_processor":"OnBoardingTaskEmailProcessor"}', NULL, NULL,@onboarding_module_id, 0);
SET @notification_id := LAST_INSERT_ID();


INSERT INTO `ohrm_notification_subscriber_subscription_template`
(`notification_id`,`media_id`,`locale`,`name`,`subject`,`body`) VALUES
    (@notification_id, @email_media, 'en_US',
     'New Task Created Notification Template',
     'On/Offboarding - New Tasks Notification - %recipientFirstName%',
     '<p>Hi %recipientFirstName%,<br /><br />New tasks were assigned to you.<br />%pendingTasks%<br /><br />Please check OrangeHRM for more details.<br />Thank you.<br /><br />This is an automated notification.</p>');


SET @template_id := LAST_INSERT_ID();

INSERT INTO `ohrm_notification_subscription`
(notification_id,  workflow_item_id, hidden) VALUES
    (@notification_id,  NULL, 0);
SET @last_subscription_id := LAST_INSERT_ID();


INSERT INTO `ohrm_notification_subscriber`(media_id, parameters, type, name) VALUES
    (@email_media, '{"prevent_delete":true}', 'Custom','Task Owner');

SET @subscriber_id := LAST_INSERT_ID();

INSERT INTO `ohrm_notification_subscriber_subscription`(subscription_id, subscriber_id, template_id) SELECT
                                                                                                         s.id, @subscriber_id, @template_id FROM ohrm_notification_subscription s WHERE s.id >= @last_subscription_id;


-- Event added notification
SET @other_notification_type := (SELECT id FROM ohrm_notification_type where `name`='Other');
SET @email_media := (SELECT id FROM ohrm_notification_media WHERE `name` = 'Email');
SET @onboarding_module_id := (SELECT `id` FROM `ohrm_module` WHERE  `name` ='onboarding');
SET @admin_user_role := (SELECT id FROM ohrm_user_role WHERE `name` = 'Admin');
SET @supervisor_user_role := (SELECT id FROM ohrm_user_role WHERE `name` = 'Supervisor');
SET @job_owner_user_role := (SELECT id FROM ohrm_user_role WHERE `name` = 'OhrmJobOwner');

INSERT INTO `ohrm_notification`
(`name`, `is_enable`,`type_id`, `generator_class`, `parameters`, `workflow_id`, `workflow_action`,`module_id`, `hidden`) VALUES
    ('On/Offboarding - New Event Created Notification', 1, @other_notification_type, 'LegacyEmailProcessorWrapperEventNotification',
     '{"email_processor":"OnBoardingEmailProcessor"}', NULL, NULL,@onboarding_module_id, 0);
SET @notification_id := LAST_INSERT_ID();


INSERT INTO `ohrm_notification_subscriber_subscription_template`
(`notification_id`,`media_id`,`locale`,`name`,`subject`,`body`) VALUES
    (@notification_id, @email_media, 'en_US',
     'New Event Created Notification Template',
     'On/Offboarding - New Event Notification - %recipientFirstName%',
     '<p>Hi %recipientFirstName%,<br /><br />You are now the event owner for %eventName%, which is due on %dueDate%.<br />Please check OrangeHRM for further details.<br/>%eventUrl%<br /><br />Thank you.<br /><br />This is an automated notification.</p>');


SET @template_id := LAST_INSERT_ID();

INSERT INTO `ohrm_notification_subscription`
(notification_id,  workflow_item_id, hidden) VALUES
    (@notification_id,  NULL, 0);
SET @last_subscription_id := LAST_INSERT_ID();


INSERT INTO `ohrm_notification_subscriber`(media_id, parameters, type, name) VALUES
    (@email_media, '{"prevent_delete":true}', 'Custom','Event Owner');

SET @subscriber_id := LAST_INSERT_ID();

INSERT INTO `ohrm_notification_subscriber_subscription`(subscription_id, subscriber_id, template_id) SELECT
                                                                                                         s.id, @subscriber_id, @template_id FROM ohrm_notification_subscription s WHERE s.id >= @last_subscription_id;

-- reminder notification
SET @other_notification_type := (SELECT id FROM ohrm_notification_type where `name`='Other');
SET @email_media := (SELECT id FROM ohrm_notification_media WHERE `name` = 'Email');
SET @onboarding_module_id := (SELECT `id` FROM `ohrm_module` WHERE  `name` ='onboarding');
SET @admin_user_role := (SELECT id FROM ohrm_user_role WHERE `name` = 'Admin');
SET @supervisor_user_role := (SELECT id FROM ohrm_user_role WHERE `name` = 'Supervisor');
SET @job_owner_user_role := (SELECT id FROM ohrm_user_role WHERE `name` = 'OhrmJobOwner');

INSERT INTO `ohrm_notification`
(`name`, `is_enable`,`type_id`, `generator_class`, `parameters`, `workflow_id`, `workflow_action`,`module_id`, `hidden`) VALUES
    ('On/Offboarding - Reminder Task Notification', 1, @other_notification_type, 'LegacyEmailProcessorWrapperEventNotification',
     '{"email_processor":"OnBoardingTaskReminderEmailProcessor"}', NULL, NULL,@onboarding_module_id, 0);
SET @notification_id := LAST_INSERT_ID();


INSERT INTO `ohrm_notification_subscriber_subscription_template`
(`notification_id`,`media_id`,`locale`,`name`,`subject`,`body`) VALUES
    (@notification_id, @email_media, 'en_US',
     'Reminder Task Notification Template',
     'On/Offboarding - Reminder Task Notification - %recipientFirstName%',
     '<p>Hi,<br /><br />%message%<br /><br />Event: %event%<br />Task: %pendingTask%<br />Description: %description%<br />Task Owner: %taskowner%<br />Participant: %newHire%<br />Due Date: %dueDate%<br />Status: %status%<br />Priority: %priority%<br /><br />Thank you.<br /><br />This is an automated notification.</p>');


SET @template_id := LAST_INSERT_ID();

INSERT INTO `ohrm_notification_subscription`
(notification_id,  workflow_item_id, hidden) VALUES
    (@notification_id,  NULL, 0);
SET @last_subscription_id := LAST_INSERT_ID();


INSERT INTO `ohrm_notification_subscriber`(media_id, parameters, type, name) VALUES
    (@email_media, '{"prevent_delete":true}', 'Custom','Task Owner');

SET @subscriber_id := LAST_INSERT_ID();

INSERT INTO `ohrm_notification_subscriber_subscription`(subscription_id, subscriber_id, template_id) SELECT
                                                                                                         s.id, @subscriber_id, @template_id FROM ohrm_notification_subscription s WHERE s.id >= @last_subscription_id;


-- pending notification
SET @other_notification_type := (SELECT id FROM ohrm_notification_type where `name`='Other');
SET @email_media := (SELECT id FROM ohrm_notification_media WHERE `name` = 'Email');
SET @onboarding_module_id := (SELECT `id` FROM `ohrm_module` WHERE  `name` ='onboarding');
SET @admin_user_role := (SELECT id FROM ohrm_user_role WHERE `name` = 'Admin');
SET @supervisor_user_role := (SELECT id FROM ohrm_user_role WHERE `name` = 'Supervisor');
SET @job_owner_user_role := (SELECT id FROM ohrm_user_role WHERE `name` = 'OhrmJobOwner');

INSERT INTO `ohrm_notification`
(`name`, `is_enable`,`type_id`, `generator_class`, `parameters`, `workflow_id`, `workflow_action`,`module_id`, `hidden`) VALUES
    ('On/Offboarding - Pending Task Notification', 1, @other_notification_type, 'LegacyEmailProcessorWrapperEventNotification',
     '{"email_processor":"OnBoardingPendingTaskReminderEmailProcessor"}', NULL, NULL,@onboarding_module_id, 0);
SET @notification_id := LAST_INSERT_ID();


INSERT INTO `ohrm_notification_subscriber_subscription_template`
(`notification_id`,`media_id`,`locale`,`name`,`subject`,`body`) VALUES
    (@notification_id, @email_media, 'en_US',
     'Pending Task Notification Template',
     'On/Offboarding - Pending Task Notification',
     'Hi,<br/><br/>

Pending Tasks as of: %date%<br/><br/>
%pendingTasks%
<br/>
Thank you.<br/>
<br/>
This is an automated notification');


SET @template_id := LAST_INSERT_ID();

INSERT INTO `ohrm_notification_subscription`
(notification_id,  workflow_item_id, hidden) VALUES
    (@notification_id,  NULL, 0);
SET @last_subscription_id := LAST_INSERT_ID();


INSERT INTO `ohrm_notification_subscriber`(media_id, parameters, type, name) VALUES
    (@email_media, '{"prevent_delete":true}', 'Custom','Event Owner');

SET @subscriber_id := LAST_INSERT_ID();

INSERT INTO `ohrm_notification_subscriber_subscription`(subscription_id, subscriber_id, template_id) SELECT
                                                                                                         s.id, @subscriber_id, @template_id FROM ohrm_notification_subscription s WHERE s.id >= @last_subscription_id;


INSERT INTO `ohrm_notification_subscription`
(notification_id,  workflow_item_id, hidden) VALUES
    (@notification_id,  NULL, 0);
SET @last_subscription_id := LAST_INSERT_ID();


INSERT INTO `ohrm_notification_subscriber`(media_id, parameters, type, name) VALUES
    (@email_media, '{"prevent_delete":true}', 'Custom','Task Owner');

SET @subscriber_id := LAST_INSERT_ID();

INSERT INTO `ohrm_notification_subscriber_subscription`(subscription_id, subscriber_id, template_id) SELECT
                                                                                                         s.id, @subscriber_id, @template_id FROM ohrm_notification_subscription s WHERE s.id >= @last_subscription_id;

INSERT INTO `ohrm_event_listener` ( `event`, `listener_class`, `enabled`, `position`) VALUES ('onboarding.task_due_date_updated', 'OnboardingTaskUpdateEventListener', '1', '1');
INSERT INTO `ohrm_event_listener` ( `event`, `listener_class`, `enabled`, `position`) VALUES ('onboarding.task_deleted', 'OnboardingTaskDeletedEventListener', '1', '2');

-- Due date Changed email

SET @other_notification_type := (SELECT id FROM ohrm_notification_type where `name`='Other');
SET @email_media := (SELECT id FROM ohrm_notification_media WHERE `name` = 'Email');
SET @onboarding_module_id := (SELECT `id` FROM `ohrm_module` WHERE  `name` ='onboarding');
SET @admin_user_role := (SELECT id FROM ohrm_user_role WHERE `name` = 'Admin');
SET @supervisor_user_role := (SELECT id FROM ohrm_user_role WHERE `name` = 'Supervisor');
SET @task_owner_user_role := (SELECT id FROM ohrm_user_role WHERE `name` = 'OhrmTaskOwner');

INSERT INTO `ohrm_notification`
(`name`, `is_enable`,`type_id`, `generator_class`, `parameters`, `workflow_id`, `workflow_action`,`module_id`, `hidden`) VALUES
    ('On/Offboarding - Task Due date Updated Notification', 1, @other_notification_type, 'LegacyEmailProcessorWrapperEventNotification',
     '{"email_processor":"OnBoardingTaskUpdateEmailProcessor"}', NULL, NULL,@onboarding_module_id, 0);
SET @notification_id := LAST_INSERT_ID();


INSERT INTO `ohrm_notification_subscriber_subscription_template`
(`notification_id`,`media_id`,`locale`,`name`,`subject`,`body`) VALUES
    (@notification_id, @email_media, 'en_US',
     'Task Timeline updated with New Due Date',
     'On/Offboarding - Task Timeline updated with New Due Date',
     '<p>Hi %recipientFirstName%,<br /><br />Following task’s timeline is updated with new due date.<br />Task Name: %taskName%<br /><br />Task Link: %taskURL%<br /><br />Existing Due Date: %oldDueDate%<br /><br />Updated Due Date: %newDueDate%<br /><br />Thank you.<br /><br />You are getting this email because you are a subscriber.<br />This is an automated notification.</p>');


SET @template_id := LAST_INSERT_ID();

INSERT INTO `ohrm_notification_subscription`
(notification_id,  workflow_item_id, hidden) VALUES
    (@notification_id,  NULL, 0);
SET @last_subscription_id := LAST_INSERT_ID();


INSERT INTO `ohrm_notification_subscriber`(media_id, parameters, type, name) VALUES
    (@email_media, '{"prevent_delete":true}', 'Custom','Task Owner');

SET @subscriber_id := LAST_INSERT_ID();

INSERT INTO `ohrm_notification_subscriber_subscription`(subscription_id, subscriber_id, template_id) SELECT
                                                                                                         s.id, @subscriber_id, @template_id FROM ohrm_notification_subscription s WHERE s.id >= @last_subscription_id;


-- Task remove notification

INSERT INTO `ohrm_notification`
(`name`, `is_enable`,`type_id`, `generator_class`, `parameters`, `workflow_id`, `workflow_action`,`module_id`, `hidden`) VALUES
    ('On/Offboarding - Task Cancellation Notification', 1, @other_notification_type, 'LegacyEmailProcessorWrapperEventNotification',
     '{"email_processor":"OnBoardingTaskDeleteEmailProcessor"}', NULL, NULL,@onboarding_module_id, 0);
SET @notification_id := LAST_INSERT_ID();


INSERT INTO `ohrm_notification_subscriber_subscription_template`
(`notification_id`,`media_id`,`locale`,`name`,`subject`,`body`) VALUES
    (@notification_id, @email_media, 'en_US',
     'Task Cancellation Notification',
     'On/Offboarding - Task Cancellation Notification - %taskName%',
     '<p>Hi %recipientFirstName%,<br /><br />Following task was cancelled/removed from %eventUrl%.<br />Task Name: %taskName%<br /><br />Please check OrangeHRM for more details.</br>
<br/>Thank you.<br/><br />This is an automated notification.</p>');


SET @template_id := LAST_INSERT_ID();

INSERT INTO `ohrm_notification_subscription`
(notification_id,  workflow_item_id, hidden) VALUES
    (@notification_id,  NULL, 0);
SET @last_subscription_id := LAST_INSERT_ID();


INSERT INTO `ohrm_notification_subscriber`(media_id, parameters, type, name) VALUES
    (@email_media, '{"prevent_delete":true}', 'Custom','Event Owner');

SET @subscriber_id := LAST_INSERT_ID();

INSERT INTO `ohrm_notification_subscriber_subscription`(subscription_id, subscriber_id, template_id) SELECT
                                                                                                         s.id, @subscriber_id, @template_id FROM ohrm_notification_subscription s WHERE s.id >= @last_subscription_id;

-- Send task cancellation notification to task owner
SET @notification_id := (SELECT id from ohrm_notification where name ='On/Offboarding - Task Cancellation Notification');

SET @other_notification_type := (SELECT id FROM ohrm_notification_type where `name`='Other');
SET @email_media := (SELECT id FROM ohrm_notification_media WHERE `name` = 'Email');
SET @onboarding_module_id := (SELECT `id` FROM `ohrm_module` WHERE  `name` ='onboarding');
INSERT INTO `ohrm_notification_subscriber_subscription_template`
(`notification_id`,`media_id`,`locale`,`name`,`subject`,`body`) VALUES
    (@notification_id, @email_media, 'en_US',
     'Task Cancellation Notification Sent to Task Owner',
     'On/Offboarding - Task Cancellation Notification - %taskName%',
     '<p>Hi %recipientFirstName%,<br /><br />Following task was cancelled/removed from %eventName%.<br />Task Name: %taskName%<br /><br />Please check OrangeHRM for more details.</br>
<br/>Thank you.<br/><br />This is an automated notification.</p>');


SET @template_id := LAST_INSERT_ID();

INSERT INTO `ohrm_notification_subscription`
(notification_id,  workflow_item_id, hidden) VALUES
    (@notification_id,  NULL, 0);
SET @last_subscription_id := LAST_INSERT_ID();


INSERT INTO `ohrm_notification_subscriber`(media_id, parameters, type, name) VALUES
    (@email_media, '{"prevent_delete":true}', 'Custom','Task Owner');

SET @subscriber_id := LAST_INSERT_ID();

INSERT INTO `ohrm_notification_subscriber_subscription`(subscription_id, subscriber_id, template_id) SELECT
                                                                                                         s.id, @subscriber_id, @template_id FROM ohrm_notification_subscription s WHERE s.id >= @last_subscription_id;

-- Event Due date Changed email

INSERT INTO `ohrm_event_listener` ( `event`, `listener_class`, `enabled`, `position`) VALUES ('onboarding.event_due_date_updated', 'OnboardingEventUpdateEventListener', '1', '1');


SET @other_notification_type := (SELECT id FROM ohrm_notification_type where `name`='Other');
SET @email_media := (SELECT id FROM ohrm_notification_media WHERE `name` = 'Email');
SET @onboarding_module_id := (SELECT `id` FROM `ohrm_module` WHERE  `name` ='onboarding');
SET @admin_user_role := (SELECT id FROM ohrm_user_role WHERE `name` = 'Admin');
SET @supervisor_user_role := (SELECT id FROM ohrm_user_role WHERE `name` = 'Supervisor');
SET @task_owner_user_role := (SELECT id FROM ohrm_user_role WHERE `name` = 'OhrmTaskOwner');

INSERT INTO `ohrm_notification`
(`name`, `is_enable`,`type_id`, `generator_class`, `parameters`, `workflow_id`, `workflow_action`,`module_id`, `hidden`) VALUES
    ('On/Offboarding - Event Due Date Updated Notification', 1, @other_notification_type, 'LegacyEmailProcessorWrapperEventNotification',
     '{"email_processor":"OnBoardingEventUpdateEmailProcessor"}', NULL, NULL,@onboarding_module_id, 0);
SET @notification_id := LAST_INSERT_ID();


INSERT INTO `ohrm_notification_subscriber_subscription_template`
(`notification_id`,`media_id`,`locale`,`name`,`subject`,`body`) VALUES
    (@notification_id, @email_media, 'en_US',
     'Event Timeline updated with New Due Date',
     'On/Offboarding - Event Timeline updated with New Due Date',
     '<p>Hi %recipientFirstName%,<br /><br />Following event’s timeline is updated with the new due date.<br />Event Name: %eventName%<br /><br />Event Link: %eventUrl%<br /><br />Current Due Date: %oldDueDate%<br /><br />New Due Date: %dueDate%<br /><br />Thank you.<br /><br />You are getting this email because you are a subscriber.<br />This is an automated notification.</p>');


SET @template_id := LAST_INSERT_ID();

INSERT INTO `ohrm_notification_subscription`
(notification_id,  workflow_item_id, hidden) VALUES
    (@notification_id,  NULL, 0);
SET @last_subscription_id := LAST_INSERT_ID();


INSERT INTO `ohrm_notification_subscriber`(media_id, parameters, type, name) VALUES
    (@email_media, '{"prevent_delete":true}', 'Custom','Event Owner');

SET @subscriber_id := LAST_INSERT_ID();

INSERT INTO `ohrm_notification_subscriber_subscription`(subscription_id, subscriber_id, template_id) SELECT
                                                                                                         s.id, @subscriber_id, @template_id FROM ohrm_notification_subscription s WHERE s.id >= @last_subscription_id;