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 @ess_role_id := (SELECT `id` FROM ohrm_user_role WHERE `name` = 'ESS');

SET @module_id := (SELECT `id` FROM `ohrm_module` WHERE `name` = 'attendance');
SET @hod_id := (SELECT `id` FROM `ohrm_user_role` WHERE `name` = 'HeadOfDepartment');
SET @supervisor_user_role_id := 3;

INSERT INTO ohrm_screen (`name`, `module_id`, `action_url`, `route_name`) VALUES
('Export Attendance Data to CSV', @module_id, 'viewAttendanceDataExtract', 'attendance_viewAttendanceDataExtract');

SET @export_csv_screen_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
(@admin_role_id, @export_csv_screen_id, 1, 0, 0, 0),
(@supervisor_role_id, @export_csv_screen_id, 1, 0, 0, 0),
(@hod_id, @export_csv_screen_id, 1, 0, 0, 0);

INSERT INTO `ohrm_data_group` (`name`, `description`, `can_read`, `can_create`, `can_update`, `can_delete`) VALUES
('attendance_export_to_csv', 'Attendance - Export Attendance Records', 1, 1, 1, 0);

SET @attendance_export_to_csv_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, @attendance_export_to_csv_data_group_id, 1, 1, 1, 0, 0),
(@supervisor_role_id, @attendance_export_to_csv_data_group_id, 1, 1, 0, 0, 0),
(@ess_role_id, @attendance_export_to_csv_data_group_id, 1, 0, 0, 0, 1);


SET @view_time_module_screen_id := (SELECT `id` FROM ohrm_screen WHERE `action_url` = 'viewTimeModule');

INSERT INTO `ohrm_data_group_screen`(`data_group_id`, `screen_id`, `permission`) VALUES
(@attendance_export_to_csv_data_group_id, @export_csv_screen_id, 1),
(@attendance_export_to_csv_data_group_id, @view_time_module_screen_id, 1);

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

INSERT INTO ohrm_user_role_type_data_group (user_role_type_id, data_group_id, can_read, can_create, can_update, can_delete) VALUES
(2, @attendance_export_to_csv_data_group_id, 1, 0, 0, 0),
(3, @attendance_export_to_csv_data_group_id, 1, 1, 0, 0);

-- HOD user role permission

insert into ohrm_user_role_data_group(user_role_id,data_group_id,can_read,can_create,can_update,can_delete,self)
select @hod_id,data_group_id,can_read,can_create,can_update,can_delete,self from ohrm_user_role_data_group where user_role_id=@supervisor_user_role_id and data_group_id in (SELECT id FROM ohrm_data_group WHERE name in
('attendance_export_to_csv','attendance_details_report'));
