INSERT INTO `ohrm_user_role` (`name`, `display_name`, `is_assignable`, `is_predefined`, `type_id`) VALUES
('ActionOwner', 'Action Owner', '0', '1', NULL);

SET @admin_role_id := (SELECT `id` FROM ohrm_user_role WHERE `name` = 'Admin');
SET @supervisor_role_id := (SELECT `id` FROM ohrm_user_role WHERE `name` = 'Supervisor');
SET @action_owner_role_id := (SELECT `id` FROM ohrm_user_role WHERE `name` = 'ActionOwner');

INSERT INTO `ohrm_module` (`name`, `status`, `data_group_prefix`) VALUES ('discipline', 1, 'Discipline');
SET @discipline_module_id := (SELECT LAST_INSERT_ID());

INSERT INTO ohrm_screen (`name`, `module_id`, `action_url`, `route_name`) VALUES('Default Disciplinary View', @discipline_module_id, 'defaultDisciplinaryView', 'discipline_defaultDisciplinaryView');
SET @default_disciplinary_view_screen_id := (SELECT LAST_INSERT_ID());

INSERT INTO ohrm_screen (`name`, `module_id`, `action_url`, `route_name`) VALUES
('Disciplinary Cases', @discipline_module_id, 'viewDisciplinaryCases', 'discipline_viewDisciplinaryCases');
SET @view_disciplinary_cases_screen_id := (SELECT LAST_INSERT_ID());

INSERT INTO ohrm_screen (`name`, `module_id`, `action_url`, `route_name`) VALUES
('My Actions', @discipline_module_id, 'viewMyActions', 'discipline_viewMyActions');
SET @view_my_actions_screen_id := (SELECT LAST_INSERT_ID());

SET @people_management_pillar_menu_id := (SELECT `id` FROM ohrm_menu_item WHERE `menu_title` = 'People Management' AND parent_id IS NULL AND level = 1);
SET @employee_management_module_menu_id := (SELECT `id` FROM ohrm_menu_item WHERE `menu_title` = 'Employee Management' AND parent_id = @people_management_pillar_menu_id AND level = 2);

INSERT INTO ohrm_menu_item (`menu_title`, `screen_id`, `parent_id`, `level`, `order_hint`, `url_extras`, `status`) VALUES
('Discipline', @default_disciplinary_view_screen_id, @employee_management_module_menu_id, 3, 1300, NULL, 1);
SET @discipline_menu_item_id := (SELECT LAST_INSERT_ID());

INSERT INTO ohrm_menu_item (`menu_title`, `screen_id`, `parent_id`, `level`, `order_hint`, `url_extras`, `status`) VALUES
('Disciplinary Cases', @view_disciplinary_cases_screen_id, @discipline_menu_item_id, 4, 100, NULL, 1),
('My Actions', @view_my_actions_screen_id, @discipline_menu_item_id, 4, 200, NULL, 1);

INSERT INTO ohrm_user_role_screen (user_role_id, screen_id, can_read, can_create, can_update, can_delete) VALUES
(@admin_role_id, @view_disciplinary_cases_screen_id, 1, 1, 1, 1),
(@supervisor_role_id, @view_disciplinary_cases_screen_id, 1, 1, 1, 1),
(@admin_role_id, @default_disciplinary_view_screen_id, 1, 1, 1, 1),
(@supervisor_role_id, @default_disciplinary_view_screen_id, 1, 1, 1, 1),
(@action_owner_role_id, @default_disciplinary_view_screen_id, 1, 1, 1, 1),
(@action_owner_role_id, @view_my_actions_screen_id, 1, 1, 1, 1);

INSERT INTO `ohrm_data_group` (`name`, `description`, `can_read`, `can_create`, `can_update`, `can_delete`) VALUES
('disciplinary_case', 'Employee Management - Discipline - Cases', 1, 1, 1, 1);
SET @disciplinary_cases_data_group_id := (SELECT LAST_INSERT_ID());

INSERT INTO `ohrm_user_role_data_group` (`user_role_id`, `data_group_id`, `can_read`, `can_create`, `can_update`, `can_delete`, `self`) VALUES
(@admin_role_id, @disciplinary_cases_data_group_id, 1, 1, 1, 1, 0),
(@supervisor_role_id, @disciplinary_cases_data_group_id, 1, 1, 1, 1, 0),
(@action_owner_role_id, @disciplinary_cases_data_group_id, 1, 0, 0, 0, 0);

INSERT INTO `ohrm_data_group_screen`(`data_group_id`, `screen_id`, `permission`) VALUES
(@disciplinary_cases_data_group_id, @view_disciplinary_cases_screen_id, 1),
(@disciplinary_cases_data_group_id, @view_disciplinary_cases_screen_id, 2),
(@disciplinary_cases_data_group_id, @view_disciplinary_cases_screen_id, 3),
(@disciplinary_cases_data_group_id, @view_disciplinary_cases_screen_id, 4),
(@disciplinary_cases_data_group_id, @default_disciplinary_view_screen_id,1);

INSERT INTO ohrm_user_role_type_data_group(user_role_type_id, data_group_id) VALUES
(1, @disciplinary_cases_data_group_id),
(3, @disciplinary_cases_data_group_id);

CREATE TABLE IF NOT EXISTS `ohrm_disciplinary_cases` (
      `id` int(11) not null auto_increment,
      `emp_number` int(7) NOT NULL,
      `case_id` int(7) NOT NULL,
      `description` text DEFAULT NULL,
      `created_by` int(7) NOT NULL,
      `created_date` date NOT NULL,
      `status` int(4) NOT NULL,
      `is_delete` int(4) DEFAULT NULL,
      PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `ohrm_action` (
        `id` int(7) AUTO_INCREMENT,
        `work_flow_id` int(7) NOT NULL,
        `entity_type_id` int(7),
        `entity_id` int(7),
        `owner` int(7),
        `created_by` int(7),
        `description` VARCHAR(2048),
        `due_date` DATE,
        `completed_date` DATE,
        `status` int(7),
        `emp_number` int(7),
        `template_id` int(7),
        PRIMARY KEY(id)
) ENGINE = INNODB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `ohrm_case` (
        `id` int(7) AUTO_INCREMENT,
        `emp_number` int(7),
        `case_name` VARCHAR(100),
        PRIMARY KEY(id)
) ENGINE = INNODB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `ohrm_entity_type` (
    `id` int(7) AUTO_INCREMENT,
    `entity_id` int(7),
    `entity_type` VARCHAR(100),
    `entity_display_action` VARCHAR(100),
    `module_name` VARCHAR(100),
    `entity_display_name` VARCHAR(100),
    `is_delete` int(4) DEFAULT NULL,
    PRIMARY KEY(id)
) ENGINE = INNODB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `ohrm_action_template` (
    `id` int(7) AUTO_INCREMENT,
    `action_id` int(7),
    `entity_id` int(7),
    `emp_number` int(7),
    `created_by` int(7),
    `created_date` DATE,
    `template_id` int(7),
    `document_content` LONGBLOB DEFAULT NULL,
    `file_type` VARCHAR(200) NOT NULL DEFAULT 'pdf',
    PRIMARY KEY(id)
) ENGINE = INNODB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `ohrm_action_statuses` (
    `id` int(7) AUTO_INCREMENT,
    `status_id` int(7),
    `status_name` VARCHAR(100),
    PRIMARY KEY(id)
) ENGINE = INNODB DEFAULT CHARSET=utf8;

INSERT INTO `ohrm_action_statuses` (`status_id`, `status_name`) VALUES
(1, 'Open'),
(2, 'In Progress'),
(3, 'Completed');

CREATE TABLE IF NOT EXISTS `ohrm_case_statuses` (
    `id` int(7) AUTO_INCREMENT,
    `status_id` int(7),
    `status_name` VARCHAR(100),
    PRIMARY KEY(id)
) ENGINE = INNODB DEFAULT CHARSET=utf8;

INSERT INTO `ohrm_case_statuses` (`status_id`, `status_name`) VALUES
(1, 'INITIATE'),
(2, 'IN PROGRESS'),
(3, 'CLOSE');

CREATE TABLE IF NOT EXISTS `ohrm_disciplinary_case_workflow_action` (
    `name` VARCHAR(100),
    `action_text` VARCHAR(100)
) ENGINE = INNODB DEFAULT CHARSET=utf8;

INSERT INTO `ohrm_disciplinary_case_workflow_action` (`name`, `action_text`) VALUES
('INITIATE', 'Initiate'),
('initiateCase', 'In Progress'),
('closeDisciplinaryCase', 'Close Case');

INSERT INTO `ohrm_data_group` (`name`, `description`, `can_read`, `can_create`, `can_update`, `can_delete`) VALUES
('add_action', 'Add Action', 1, 1, 1, 1);

SET @add_action_data_group_id := (SELECT LAST_INSERT_ID());
SET @core_module_id := (SELECT `id` FROM ohrm_module WHERE `name` = 'core');

INSERT INTO `ohrm_document_template_enabled_data_group_and_workflow`(`description`, `data_group_id`, `module_id`) VALUES
('Add Action', @add_action_data_group_id, @core_module_id);

INSERT INTO `ohrm_user_role_data_group` (`user_role_id`, `data_group_id`, `can_read`, `can_create`, `can_update`, `can_delete`, `self`) VALUES
(@admin_role_id, @add_action_data_group_id, 1, 1, 1, 1, 0),
(@supervisor_role_id, @add_action_data_group_id, 1, 1, 1, 1, 0),
(@action_owner_role_id, @add_action_data_group_id, 1, 0, 0, 0, 0);

-- INSERT INTO `ohrm_entity_type` (`entity_type`, `entity_display_action`, `module_name`, `entity_display_name`) VALUES
-- ('Disciplinary Case', 'saveDisciplinaryCase', 'discipline', 'View Disciplinary Case');

INSERT INTO `ohrm_workflow` (`id`, `name`, `label`, `available_to`, `default_available_to`, `group_name`, `group_label`, `module_id`, `order_no`) VALUES
('DISCIPLINE', 'discipline', 'Discipline', '1',NULL , NULL, NULL, (SELECT `id` FROM `ohrm_module` WHERE `name`='discipline'), 9);

INSERT INTO `ohrm_workflow_state_machine`(`workflow`,`state`,`role`,`action`,`resulting_state`, `roles_to_notify`, `priority`) VALUES
    ('DISCIPLINE','INITIATE','ADMIN','initiateCase','IN PROGRESS', '', 0),
    ('DISCIPLINE','DISCIPLINARY_HEARING','ADMIN','addDisciplinaryHearing','IN PROGRESS', '', 0),
    ('DISCIPLINE','VERBAL_WARNING','ADMIN','giveVerbalWarning','IN PROGRESS', '', 0),
    ('DISCIPLINE','WRITTEN_WARNING','ADMIN','giveWrittenWarning','IN PROGRESS', '', 0),
    ('DISCIPLINE','ADMINISTRATIVE_LEAVE','ADMIN','giveAdministrativeLeave','IN PROGRESS', '', 0),
    ('DISCIPLINE','PROVIDE_COUNSELLING','ADMIN','giveCounselling','IN PROGRESS', '', 0),
    ('DISCIPLINE','SUSPEND','ADMIN','giveSuspendition','IN PROGRESS', '', 0),
    ('DISCIPLINE','PROBATION','ADMIN','addToProbation','IN PROGRESS', '', 0),
    ('DISCIPLINE','TERMINATION','ADMIN','giveTermination','IN PROGRESS', '', 0),
    ('DISCIPLINE','IN PROGRESS','ADMIN','closeDisciplinaryCase','CLOSE', '', 0);

INSERT INTO `ohrm_workflow_state_machine`(`workflow`,`state`,`role`,`action`,`resulting_state`, `roles_to_notify`, `priority`) VALUES
    ('DISCIPLINE','DISCIPLINARY_HEARING','ACTIONOWNER','addDisciplinaryHearing','IN PROGRESS', '', 0),
    ('DISCIPLINE','VERBAL_WARNING','ACTIONOWNER','giveVerbalWarning','IN PROGRESS', '', 0),
    ('DISCIPLINE','WRITTEN_WARNING','ACTIONOWNER','giveWrittenWarning','IN PROGRESS', '', 0),
    ('DISCIPLINE','ADMINISTRATIVE_LEAVE','ACTIONOWNER','giveAdministrativeLeave','IN PROGRESS', '', 0),
    ('DISCIPLINE','PROVIDE_COUNSELLING','ACTIONOWNER','giveCounselling','IN PROGRESS', '', 0),
    ('DISCIPLINE','SUSPEND','ACTIONOWNER','giveSuspendition','IN PROGRESS', '', 0),
    ('DISCIPLINE','PROBATION','ACTIONOWNER','addToProbation','IN PROGRESS', '', 0),
    ('DISCIPLINE','IN PROGRESS','ACTIONOWNER','addDisciplinaryHearing','IN PROGRESS', '', 0),
    ('DISCIPLINE','TERMINATION','ACTIONOWNER','giveTermination','IN PROGRESS', '', 0);

SET @disciplinary_hearing_id := (SELECT `id` FROM ohrm_workflow_state_machine WHERE `action` = 'addDisciplinaryHearing' AND `role` = 'ADMIN');
SET @verbal_warning_id := (SELECT `id` FROM ohrm_workflow_state_machine WHERE `action` = 'giveVerbalWarning' AND `role` = 'ADMIN');
SET @written_warning_id := (SELECT `id` FROM ohrm_workflow_state_machine WHERE `action` = 'giveWrittenWarning'AND `role` = 'ADMIN');
SET @administrative_leave_id := (SELECT `id` FROM ohrm_workflow_state_machine WHERE `action` = 'giveAdministrativeLeave' AND `role` = 'ADMIN');
SET @counsiling_id := (SELECT `id` FROM ohrm_workflow_state_machine WHERE `action` = 'giveCounselling' AND `role` = 'ADMIN');
SET @suspend_id := (SELECT `id` FROM ohrm_workflow_state_machine WHERE `action` = 'giveSuspendition' AND `role` = 'ADMIN');
SET @probation_id := (SELECT `id` FROM ohrm_workflow_state_machine WHERE `action` = 'addToProbation' AND `role` = 'ADMIN');
SET @terminate_id := (SELECT `id` FROM ohrm_workflow_state_machine WHERE `action` = 'giveTermination' AND `role` = 'ADMIN');

INSERT INTO `ohrm_workflow_action` (`workflow_id`, `workflow`, `action_entity`, `action_display_text`, `support_doc_creation`, `module_name`, `action_name`, `parent_action`, `action_type_text`) VALUES
    (@disciplinary_hearing_id, 'DISCIPLINE', 'discipline', 'Have disciplinary hearing', 1, 'discipline', 'addDisciplinaryHearing', 'defaultAction', 'Disciplinary Case'),
    (@verbal_warning_id, 'DISCIPLINE', 'discipline', 'Give verbal warning', 1, 'discipline', 'giveVerbalWarning', 'defaultAction', 'Disciplinary Case'),
    (@written_warning_id, 'DISCIPLINE', 'discipline', 'Give written warning', 1, 'discipline', 'giveWrittenWarning', 'defaultAction', 'Disciplinary Case'),
    (@administrative_leave_id, 'DISCIPLINE', 'discipline', 'Send on administrative leave', 1, 'discipline', 'giveAdministrativeLeave', 'defaultAction', 'Disciplinary Case'),
    (@counsiling_id, 'DISCIPLINE', 'discipline', 'Provide Counselling', 1, 'discipline', 'giveCounselling', 'defaultAction', 'Disciplinary Case'),
    (@suspend_id, 'DISCIPLINE', 'discipline', 'Suspend', 1, 'discipline', 'giveSuspendition', 'defaultAction', 'Disciplinary Case'),
    (@probation_id, 'DISCIPLINE', 'discipline', 'Put on probation', 1, 'discipline', 'addToProbation', 'defaultAction', 'Disciplinary Case'),
    (@terminate_id, 'DISCIPLINE', 'discipline', 'Terminate', 1, 'discipline', 'employeeTerminate', 'defaultAction', 'Disciplinary Case');

SET @disciplinary_hearing_action_id := (SELECT `id` FROM ohrm_workflow_action WHERE `action_display_text` = 'Have disciplinary hearing');
SET @verbal_warning_action_id := (SELECT `id` FROM ohrm_workflow_action WHERE `action_display_text` = 'Give verbal warning');
SET @written_warning_action_id := (SELECT `id` FROM ohrm_workflow_action WHERE `action_display_text` = 'Give written warning');
SET @administrative_leave_action_id := (SELECT `id` FROM ohrm_workflow_action WHERE `action_display_text` = 'Send on administrative leave');
SET @counsiling_action_id := (SELECT `id` FROM ohrm_workflow_action WHERE `action_display_text` = 'Provide Counselling');
SET @suspend_action_id := (SELECT `id` FROM ohrm_workflow_action WHERE `action_display_text` = 'Suspend');
SET @probation_action_id := (SELECT `id` FROM ohrm_workflow_action WHERE `action_display_text` = 'Put on probation');
SET @terminate_action_id := (SELECT `id` FROM ohrm_workflow_action WHERE `action_display_text` = 'Terminate');

INSERT INTO `ohrm_document_template_enabled_workflow_action` (`workflow_action_id`, `workflow`) VALUES
    (@disciplinary_hearing_action_id, 'DISCIPLINE'),
    (@verbal_warning_action_id, 'DISCIPLINE'),
    (@written_warning_action_id, 'DISCIPLINE'),
    (@administrative_leave_action_id, 'DISCIPLINE'),
    (@counsiling_action_id, 'DISCIPLINE'),
    (@suspend_action_id, 'DISCIPLINE'),
    (@probation_action_id, 'DISCIPLINE'),
    (@terminate_action_id, 'DISCIPLINE');

-- Document template for Disciplinary module
SET @max_screen_id := (SELECT MAX(`id`) FROM `ohrm_screen`);
SET @disciplinary_doc_template_list_screen_id := @max_screen_id + 1;
SET @add_disciplinary_doc_template_screen_id := @max_screen_id + 2;
SET @delete_disciplinary_doc_template_screen_id := @max_screen_id + 3;
SET @discipline_module_id := (SELECT `id` FROM ohrm_module WHERE `name` = 'discipline');

INSERT INTO `ohrm_screen` (`id`, `name`, `module_id`, `action_url`, `route_name`) VALUES
    (@disciplinary_doc_template_list_screen_id, 'Disciplinary Document Templates', @discipline_module_id, 'viewDisciplinaryDocumentTemplates', 'discipline_viewDisciplinaryDocumentTemplates'),
    (@add_disciplinary_doc_template_screen_id, 'Disciplinary Add Document Templates', @discipline_module_id, 'addDisciplinaryDocumentTemplates', NULL),
    (@delete_disciplinary_doc_template_screen_id, 'Disciplinary Delete Document Templates', @discipline_module_id, 'deleteDisciplinaryDocumentTemplates', NULL);

INSERT INTO ohrm_menu_item (`menu_title`, `screen_id`, `parent_id`, `level`, `order_hint`, `url_extras`, `status`) VALUES
    ('Document Templates', @disciplinary_doc_template_list_screen_id, @discipline_menu_item_id, 4, 900, NULL, 1);

INSERT INTO `ohrm_data_group` (`name`, `description`, `can_read`, `can_create`, `can_update`, `can_delete`) VALUES
    ('disciplinary_document_templates', 'Employee Management - Discipline - Configure - Document Templates', 1, 1, 1, 1);
SET @disciplinary_doc_template_data_group_id := (SELECT LAST_INSERT_ID());

INSERT INTO `ohrm_user_role_screen` (`user_role_id`, `screen_id`, `can_read`, `can_create`, `can_update`, `can_delete`) VALUES
    (1, @disciplinary_doc_template_list_screen_id, 1, 1, 1, 1),
    (1, @add_disciplinary_doc_template_screen_id, 1, 1, 1, 1),
    (1, @delete_disciplinary_doc_template_screen_id, 1, 1, 1, 1);

INSERT INTO `ohrm_user_role_data_group` (`user_role_id`, `data_group_id`, `can_read`, `can_create`, `can_update`, `can_delete`, `self`) VALUES
    (1, @disciplinary_doc_template_data_group_id, 1, 1, 1, 1, 0);

INSERT INTO `ohrm_data_group_screen`(`data_group_id`, `screen_id`, `permission`) VALUES
    (@disciplinary_doc_template_data_group_id, @disciplinary_doc_template_list_screen_id, 1),
    (@disciplinary_doc_template_data_group_id, @add_disciplinary_doc_template_screen_id, 1),
    (@disciplinary_doc_template_data_group_id, @add_disciplinary_doc_template_screen_id, 2),
    (@disciplinary_doc_template_data_group_id, @add_disciplinary_doc_template_screen_id, 3),
    (@disciplinary_doc_template_data_group_id, @delete_disciplinary_doc_template_screen_id, 4),
    (@disciplinary_doc_template_data_group_id, @default_disciplinary_view_screen_id, 1);

INSERT INTO ohrm_user_role_type_data_group(user_role_type_id, data_group_id) VALUES
    (1, @disciplinary_doc_template_data_group_id);

INSERT INTO `ohrm_event_listener` (`event`, `listener_class`, `enabled`, `position`) VALUES
    ('action.change_action_status', 'DisciplinaryCaseListener', '1', '26');

-- disciplinary details tokens
INSERT INTO `ohrm_token`(`name`, `display_name`, `class_name`, `entity_name`, `value_getters`, `data_group_id`, `is_date`, `localize`) VALUES
    ('caseName', 'Disciplinary Case Name', null, 'CaseEntity', 'getCaseName', @disciplinary_cases_data_group_id, 0, 1),
    ('description', 'Disciplinary Case Description', null, 'DisciplinaryCase', 'getDescription', @disciplinary_cases_data_group_id, 0, 1),
    ('createdDate', 'Disciplinary Case Created Date', null, 'DisciplinaryCase', 'getCreatedDate', @disciplinary_cases_data_group_id, 1, 1);

SET @token_case_name_id := (SELECT `id` FROM ohrm_token WHERE `name` = 'caseName');
SET @token_description_id := (SELECT `id` FROM ohrm_token WHERE `name` = 'description');
SET @token_create_date_id := (SELECT `id` FROM ohrm_token WHERE `name` = 'createdDate');

INSERT INTO `ohrm_restricted_module_token`(`token_id`, `restricted_module`) VALUES
    (@token_case_name_id, @discipline_module_id),
    (@token_description_id, @discipline_module_id),
    (@token_create_date_id, @discipline_module_id);
