-- Requester Submit Non Approving Request
SET @requester_submit_non_approve_request_state_id := (SELECT id
                                   FROM ohrm_workflow_state_machine
                                   WHERE workflow = 'REQUEST_WITHOUT_APPROVE'
                                     AND state = 'Saved'
                                     AND role = 'Requester'
                                     AND action = 'SUBMIT');

SET @system_initiates_non_approval_process_state_id := (SELECT id
                                                  FROM ohrm_workflow_state_machine
                                                  WHERE workflow = 'REQUEST_PROCESS_APPROVE'
                                                    AND state = 'INITIAL'
                                                    AND role = 'SYSTEM'
                                                    AND action = 'NON APPROVE SUBMIT');

INSERT INTO `ohrm_workflow_state_machine_dependancy`(`master_state_change_id`, `slave_state_change_id`, `trigger_verification`)
VALUES (@requester_submit_non_approve_request_state_id, @system_initiates_non_approval_process_state_id, 'AdvancedWorkflowRequestToProcessStateChangeTrigger');

-- Requester submits a newly created request. Initiate the approve process and initiate the approvers

-- 1) Approval process should be initiated
SET @requester_submit_request_state_id := (SELECT id
                                   FROM ohrm_workflow_state_machine
                                   WHERE workflow = 'REQUEST'
                                     AND state = 'Saved'
                                     AND role = 'Requester'
                                     AND action = 'SUBMIT');

SET @system_initiates_approval_process_state_id := (SELECT id
                                                  FROM ohrm_workflow_state_machine
                                                  WHERE workflow = 'REQUEST_PROCESS_APPROVE'
                                                    AND state = 'INITIAL'
                                                    AND role = 'SYSTEM'
                                                    AND action = 'INITIATE');

INSERT INTO `ohrm_workflow_state_machine_dependancy`(`master_state_change_id`, `slave_state_change_id`, `trigger_verification`)
VALUES (@requester_submit_request_state_id, @system_initiates_approval_process_state_id, 'AdvancedWorkflowRequestToProcessStateChangeTrigger');


-- Proceed the Approvers from Initial to In Progress

SET @approval_process_initiates_state_id := (SELECT id
                                             FROM ohrm_workflow_state_machine
                                             WHERE workflow = 'REQUEST_PROCESS_APPROVE'
                                               AND state = 'INITIAL'
                                               AND role = 'SYSTEM'
                                               AND action = 'INITIATE');

SET @system_initiate_approver_actors_state_id := (SELECT id
                                                  FROM ohrm_workflow_state_machine
                                                  WHERE workflow = 'REQUEST_PROCESS_ACTOR_APPROVER'
                                                    AND state = 'INITIAL'
                                                    AND role = 'SYSTEM'
                                                    AND action = 'INITIATE');

INSERT INTO `ohrm_workflow_state_machine_dependancy`(`master_state_change_id`, `slave_state_change_id`, `trigger_verification`, `options`)
VALUES (@approval_process_initiates_state_id, @system_initiate_approver_actors_state_id, 'AdvancedWorkflowProcessToActorStateChangeTrigger',
        '{"immediateNextLevelActorOnly":true}');

-- Proceed the next level approver from Initial to Initiated
SET @approver_approves_state_id := (SELECT id
                                    FROM ohrm_workflow_state_machine
                                    WHERE workflow = 'REQUEST_PROCESS_ACTOR_APPROVER'
                                      AND state = 'INITIATED'
                                      AND role = 'RequestApprover'
                                      AND action = 'APPROVE');

SET @admin_approves_state_id := (SELECT id
                                    FROM ohrm_workflow_state_machine
                                    WHERE workflow = 'REQUEST_PROCESS_ACTOR_APPROVER'
                                      AND state = 'INITIATED'
                                      AND role = 'Admin'
                                      AND action = 'APPROVE');

SET @next_level_approver_initiates_state_id := (SELECT id
                                                FROM ohrm_workflow_state_machine
                                                WHERE workflow = 'REQUEST_PROCESS_ACTOR_APPROVER'
                                                  AND state = 'INITIAL'
                                                  AND role = 'SYSTEM'
                                                  AND action = 'INITIATE');

INSERT INTO `ohrm_workflow_state_machine_dependancy`(`master_state_change_id`, `slave_state_change_id`, `trigger_verification`, `options`)
VALUES (@approver_approves_state_id, @next_level_approver_initiates_state_id, 'AdvancedWorkflowActorToActorStateChangeTrigger',
        '{"immediateNextLevelActorOnly":true}');

INSERT INTO `ohrm_workflow_state_machine_dependancy`(`master_state_change_id`, `slave_state_change_id`, `trigger_verification`, `options`)
VALUES (@admin_approves_state_id, @next_level_approver_initiates_state_id, 'AdvancedWorkflowActorToActorStateChangeTrigger',
        '{"immediateNextLevelActorOnly":true}');


-- Proceed the Approval process from In Progress to Completed
SET @approver_approves_state_id := (SELECT id
                                    FROM ohrm_workflow_state_machine
                                    WHERE workflow = 'REQUEST_PROCESS_ACTOR_APPROVER'
                                      AND state = 'INITIATED'
                                      AND role = 'RequestApprover'
                                      AND action = 'APPROVE');

SET @admin_approves_state_id := (SELECT id
                                    FROM ohrm_workflow_state_machine
                                    WHERE workflow = 'REQUEST_PROCESS_ACTOR_APPROVER'
                                      AND state = 'INITIATED'
                                      AND role = 'Admin'
                                      AND action = 'APPROVE');

SET @approval_process_competes_state_id := (SELECT id
                                            FROM ohrm_workflow_state_machine
                                            WHERE workflow = 'REQUEST_PROCESS_APPROVE'
                                              AND state = 'IN PROGRESS'
                                              AND role = 'SYSTEM'
                                              AND action = 'COMPLETE APPROVAL');

INSERT INTO `ohrm_workflow_state_machine_dependancy`(`master_state_change_id`, `slave_state_change_id`, `trigger_verification`)
VALUES (@approver_approves_state_id, @approval_process_competes_state_id, 'AdvancedWorkflowActorToProcessStateChangeTrigger'),
       (@admin_approves_state_id, @approval_process_competes_state_id, 'AdvancedWorkflowActorToProcessStateChangeTrigger');

-- Proceed the Request from Pending Approval to Approved
SET @approve_process_completes_state_id := (SELECT id
                                            FROM ohrm_workflow_state_machine
                                            WHERE workflow = 'REQUEST_PROCESS_APPROVE'
                                              AND state = 'IN PROGRESS'
                                              AND role = 'SYSTEM'
                                              AND action = 'COMPLETE APPROVAL');

SET @request_to_approved_state_id := (SELECT id
                                     FROM ohrm_workflow_state_machine
                                     WHERE workflow = 'REQUEST'
                                       AND state = 'In Progress'
                                       AND role = 'SYSTEM'
                                       AND action = 'APPROVE');

INSERT INTO `ohrm_workflow_state_machine_dependancy`(`master_state_change_id`, `slave_state_change_id`, `trigger_verification`)
VALUES (@approve_process_completes_state_id, @request_to_approved_state_id, 'AdvancedWorkflowProcessToRequestStateChangeTrigger');

-- if requester withdraws at submitted state set process to initial
SET @requester_withdraw_at_submitted_request_state_id := (SELECT id
                                   FROM ohrm_workflow_state_machine
                                   WHERE workflow = 'REQUEST'
                                     AND state = 'Submitted'
                                     AND role = 'Requester'
                                     AND action = 'WITHDRAW');

-- if admin withdraws at submitted state set process to initial
SET @admin_withdraw_request_at_submitted_state_id := (SELECT id
                                   FROM ohrm_workflow_state_machine
                                   WHERE workflow = 'REQUEST'
                                     AND state = 'Submitted'
                                     AND role = 'Admin'
                                     AND action = 'WITHDRAW');
-- if requester withdraws at in progress state set process to initial
SET @requester_withdraw_request_at_inprogress_state_id := (SELECT id
                                   FROM ohrm_workflow_state_machine
                                   WHERE workflow = 'REQUEST'
                                     AND state = 'In Progress'
                                     AND role = 'Requester'
                                     AND action = 'WITHDRAW');

-- if admin withdraws at in progress state set process to initial
SET @admin_withdraw_request_at_inprogress_state_id := (SELECT id
                                   FROM ohrm_workflow_state_machine
                                   WHERE workflow = 'REQUEST'
                                     AND state = 'In Progress'
                                     AND role = 'Admin'
                                     AND action = 'WITHDRAW');

-- if requester withdraws at approved state set process to initial
SET @requester_withdraw_request_at_approved_state_id := (SELECT id
                                   FROM ohrm_workflow_state_machine
                                   WHERE workflow = 'REQUEST'
                                     AND state = 'Approved'
                                     AND role = 'Requester'
                                     AND action = 'WITHDRAW');

-- if admin withdraws at approved state set process to initial
SET @admin_withdraw_request_at_approved_state_id := (SELECT id
                                   FROM ohrm_workflow_state_machine
                                   WHERE workflow = 'REQUEST'
                                     AND state = 'Approved'
                                     AND role = 'Admin'
                                     AND action = 'WITHDRAW');

SET @system_rollback_approval_process_state_id := (SELECT id
                                                  FROM ohrm_workflow_state_machine
                                                  WHERE workflow = 'REQUEST_PROCESS_APPROVE'
                                                    AND state = 'IN PROGRESS'
                                                    AND role = 'SYSTEM'
                                                    AND action = 'ROLLBACK');

INSERT INTO `ohrm_workflow_state_machine_dependancy`(`master_state_change_id`, `slave_state_change_id`, `trigger_verification`)
VALUES (@requester_withdraw_at_submitted_request_state_id, @system_rollback_approval_process_state_id, 'AdvancedWorkflowRequestToProcessStateChangeTrigger'),
(@admin_withdraw_request_at_submitted_state_id, @system_rollback_approval_process_state_id, 'AdvancedWorkflowRequestToProcessStateChangeTrigger'),
(@requester_withdraw_request_at_inprogress_state_id, @system_rollback_approval_process_state_id, 'AdvancedWorkflowRequestToProcessStateChangeTrigger'),
(@admin_withdraw_request_at_inprogress_state_id, @system_rollback_approval_process_state_id, 'AdvancedWorkflowRequestToProcessStateChangeTrigger'),
(@requester_withdraw_request_at_approved_state_id, @system_rollback_approval_process_state_id, 'AdvancedWorkflowRequestToProcessStateChangeTrigger'),
(@admin_withdraw_request_at_approved_state_id, @system_rollback_approval_process_state_id, 'AdvancedWorkflowRequestToProcessStateChangeTrigger');

-- revert approvers statuses when request withdrawn
SET @approval_process_rolledback_state_id := (SELECT id
                                           FROM ohrm_workflow_state_machine
                                           WHERE workflow = 'REQUEST_PROCESS_APPROVE'
                                             AND state = 'IN PROGRESS'
                                             AND role = 'SYSTEM'
                                             AND action = 'ROLLBACK');

SET @approvers_to_initial_state_id := (SELECT id
                                       FROM ohrm_workflow_state_machine
                                       WHERE workflow = 'REQUEST_PROCESS_ACTOR_APPROVER'
                                         AND state = 'INITIATED'
                                         AND role = 'SYSTEM'
                                         AND action = 'ROLLBACK');

INSERT INTO `ohrm_workflow_state_machine_dependancy`(`master_state_change_id`, `slave_state_change_id`, `trigger_verification`)
VALUES (@approval_process_rolledback_state_id, @approvers_to_initial_state_id, 'AdvancedWorkflowProcessToActorStateChangeTrigger');

-- Any approver Rejects. Should update the status of approval process, review process, reviewers, approvers (revert any ongoing approvers to initial stage) and the request.

-- Proceed the Approval Process from In Progress to Initial
SET @approver_rejects_state_id := (SELECT id
                                   FROM ohrm_workflow_state_machine
                                   WHERE workflow = 'REQUEST_PROCESS_ACTOR_APPROVER'
                                     AND state = 'INITIATED'
                                     AND role = 'RequestApprover'
                                     AND action = 'REJECT');


SET @admin_rejects_state_id := (SELECT id
                                   FROM ohrm_workflow_state_machine
                                   WHERE workflow = 'REQUEST_PROCESS_ACTOR_APPROVER'
                                     AND state = 'INITIATED'
                                     AND role = 'Admin'
                                     AND action = 'REJECT');


SET @approval_process_rejects_state_id := (SELECT id
                                           FROM ohrm_workflow_state_machine
                                           WHERE workflow = 'REQUEST_PROCESS_APPROVE'
                                             AND state = 'IN PROGRESS'
                                             AND role = 'SYSTEM'
                                             AND action = 'REJECT');

INSERT INTO `ohrm_workflow_state_machine_dependancy`(`master_state_change_id`, `slave_state_change_id`, `trigger_verification`, `options`)
VALUES (@approver_rejects_state_id, @approval_process_rejects_state_id, 'AdvancedWorkflowActorToProcessStateChangeTrigger',
        '{"validateSameStateForAllActors":false}');

INSERT INTO `ohrm_workflow_state_machine_dependancy`(`master_state_change_id`, `slave_state_change_id`, `trigger_verification`, `options`)
VALUES (@admin_rejects_state_id, @approval_process_rejects_state_id, 'AdvancedWorkflowActorToProcessStateChangeTrigger',
        '{"validateSameStateForAllActors":false}');


SET @approval_process_rejects_state_id := (SELECT id
                                           FROM ohrm_workflow_state_machine
                                           WHERE workflow = 'REQUEST_PROCESS_APPROVE'
                                             AND state = 'IN PROGRESS'
                                             AND role = 'SYSTEM'
                                             AND action = 'REJECT');

SET @approvers_to_initial_state_id := (SELECT id
                                       FROM ohrm_workflow_state_machine
                                       WHERE workflow = 'REQUEST_PROCESS_ACTOR_APPROVER'
                                         AND state = 'INITIATED'
                                         AND role = 'SYSTEM'
                                         AND action = 'ROLLBACK');

INSERT INTO `ohrm_workflow_state_machine_dependancy`(`master_state_change_id`, `slave_state_change_id`, `trigger_verification`)
VALUES (@approval_process_rejects_state_id, @approvers_to_initial_state_id, 'AdvancedWorkflowProcessToActorStateChangeTrigger');

-- Proceed the request from Pending Approval to Created
SET @approval_process_rejects_state_id := (SELECT id
                                           FROM ohrm_workflow_state_machine
                                           WHERE workflow = 'REQUEST_PROCESS_APPROVE'
                                             AND state = 'IN PROGRESS'
                                             AND role = 'SYSTEM'
                                             AND action = 'REJECT');

SET @request_to_rejected_state_id := (SELECT id
                                     FROM ohrm_workflow_state_machine
                                     WHERE workflow = 'REQUEST'
                                       AND state = 'In Progress'
                                       AND role = 'SYSTEM'
                                       AND action = 'REJECT');

INSERT INTO `ohrm_workflow_state_machine_dependancy`(`master_state_change_id`, `slave_state_change_id`, `trigger_verification`)
VALUES (@approval_process_rejects_state_id, @request_to_rejected_state_id, 'AdvancedWorkflowProcessToRequestStateChangeTrigger');
