/**
 * Copyright (c) 2023, WSO2 LLC. (https://www.wso2.com).
 *
 * WSO2 LLC. licenses this file to you under the Apache License,
 * Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
DROP PROCEDURE IF EXISTS `WSO2_OB_CONSENT_CLEANUP_DATA_RESTORE_SP`;
DELIMITER $$
CREATE PROCEDURE `WSO2_OB_CONSENT_CLEANUP_DATA_RESTORE_SP`()

BEGIN

DECLARE rowCount INT;
DECLARE enableLog BOOLEAN;
DECLARE logLevel VARCHAR(10);

-- ------------------------------------------
-- CONFIGURABLE ATTRIBUTES
-- ------------------------------------------
SET enableLog = TRUE; -- ENABLE LOGGING [DEFAULT : TRUE]
SET logLevel := 'TRACE'; -- SET LOG LEVELS : TRACE



IF (enableLog) THEN
SELECT  'CLEANUP DATA RESTORATION STARTED .... !';
END IF;


-- ---------------------

SELECT COUNT(1) INTO rowcount  FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA IN (SELECT DATABASE())  AND TABLE_NAME IN ('OB_CONSENT');
IF (rowcount = 1)
THEN
IF (enableLog AND logLevel IN ('TRACE')) THEN
SELECT  'CLEANUP DATA RESTORATION STARTED ON OB_CONSENT TABLE !';
END IF;
INSERT INTO OB_CONSENT SELECT A.* FROM BAK_OB_CONSENT A LEFT JOIN OB_CONSENT B ON A.CONSENT_ID = B.CONSENT_ID WHERE B.CONSENT_ID IS NULL;
SELECT row_count() INTO rowcount;
IF (enableLog ) THEN
SELECT  'CLEANUP DATA RESTORATION COMPLETED ON OB_CONSENT WITH %',rowcount;
END IF;
END IF;

-- ---------------------

SELECT COUNT(1) INTO rowcount  FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA IN (SELECT DATABASE())  AND TABLE_NAME IN ('OB_CONSENT_ATTRIBUTE');
IF (rowcount = 1)
THEN
IF (enableLog AND logLevel IN ('TRACE')) THEN
SELECT  'CLEANUP DATA RESTORATION STARTED ON OB_CONSENT_ATTRIBUTE TABLE !';
END IF;
INSERT INTO OB_CONSENT_ATTRIBUTE SELECT A.* FROM BAK_OB_CONSENT_ATTRIBUTE A LEFT JOIN OB_CONSENT_ATTRIBUTE B ON A.CONSENT_ID = B.CONSENT_ID WHERE B.CONSENT_ID IS NULL;
SELECT row_count() INTO rowcount;
IF (enableLog ) THEN
SELECT  'CLEANUP DATA RESTORATION COMPLETED ON OB_CONSENT_ATTRIBUTE WITH %',rowcount;
END IF;
END IF;

-- ---------------------

SELECT COUNT(1) INTO rowcount  FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA IN (SELECT DATABASE())  AND TABLE_NAME IN ('OB_CONSENT_FILE');
IF (rowcount = 1)
THEN
IF (enableLog AND logLevel IN ('TRACE')) THEN
SELECT  'CLEANUP DATA RESTORATION STARTED ON OB_CONSENT_FILE TABLE !';
END IF;
INSERT INTO OB_CONSENT_FILE SELECT A.* FROM BAK_OB_CONSENT_FILE A LEFT JOIN OB_CONSENT_FILE B ON A.CONSENT_ID = B.CONSENT_ID WHERE B.CONSENT_ID IS NULL;
SELECT row_count() INTO rowcount;
IF (enableLog ) THEN
SELECT  'CLEANUP DATA RESTORATION COMPLETED ON OB_CONSENT_FILE WITH %',rowcount;
END IF;
END IF;

-- ---------------------

SELECT COUNT(1) INTO rowcount  FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA IN (SELECT DATABASE())  AND TABLE_NAME IN ('OB_CONSENT_STATUS_AUDIT');
IF (rowcount = 1)
THEN
IF (enableLog AND logLevel IN ('TRACE')) THEN
SELECT  'CLEANUP DATA RESTORATION STARTED ON OB_CONSENT_STATUS_AUDIT TABLE !';
END IF;
INSERT INTO OB_CONSENT_STATUS_AUDIT SELECT A.* FROM BAK_OB_CONSENT_STATUS_AUDIT A LEFT JOIN OB_CONSENT_STATUS_AUDIT B ON A.CONSENT_ID = B.CONSENT_ID WHERE B.CONSENT_ID IS NULL;
SELECT row_count() INTO rowcount;
IF (enableLog ) THEN
SELECT  'CLEANUP DATA RESTORATION COMPLETED ON OB_CONSENT_STATUS_AUDIT WITH %',rowcount;
END IF;
END IF;

-- --------------------

SELECT COUNT(1) INTO rowcount  FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA IN (SELECT DATABASE())  AND TABLE_NAME IN ('OB_CONSENT_AUTH_RESOURCE');
IF (rowcount = 1)
THEN
IF (enableLog AND logLevel IN ('TRACE')) THEN
SELECT  'CLEANUP DATA RESTORATION STARTED ON OB_CONSENT_AUTH_RESOURCE TABLE !';
END IF;
INSERT INTO OB_CONSENT_AUTH_RESOURCE SELECT A.* FROM BAK_OB_CONSENT_AUTH_RESOURCE A LEFT JOIN OB_CONSENT_AUTH_RESOURCE B ON A.CONSENT_ID = B.CONSENT_ID WHERE B.CONSENT_ID IS NULL;
SELECT row_count() INTO rowcount;
IF (enableLog ) THEN
SELECT  'CLEANUP DATA RESTORATION COMPLETED ON OB_CONSENT_AUTH_RESOURCE WITH %',rowcount;
END IF;
END IF;

-- -------------------

SELECT COUNT(1) INTO rowcount  FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA IN (SELECT DATABASE())  AND TABLE_NAME IN ('OB_CONSENT_MAPPING');
IF (rowcount = 1)
THEN
IF (enableLog AND logLevel IN ('TRACE')) THEN
SELECT  'CLEANUP DATA RESTORATION STARTED ON OB_CONSENT_MAPPING TABLE !';
END IF;
INSERT INTO OB_CONSENT_MAPPING SELECT A.* FROM BAK_OB_CONSENT_MAPPING A LEFT JOIN OB_CONSENT_MAPPING B ON A.MAPPING_ID = B.MAPPING_ID WHERE B.MAPPING_ID IS NULL;
SELECT row_count() INTO rowcount;
IF (enableLog ) THEN
SELECT  'CLEANUP DATA RESTORATION COMPLETED ON OB_CONSENT_MAPPING WITH %',rowcount;
END IF;
END IF;

IF (enableLog) THEN
SELECT  'CLEANUP DATA RESTORATION COMPLETED .... !';
END IF;


END$$

DELIMITER ;
