USE `gateguard`;

DROP PROCEDURE IF EXISTS `sp_updateUsers`;

DELIMITER $$
CREATE PROCEDURE `sp_updateUsers` (
    IN p_ID INT,
	IN p_USERNAME VARCHAR(64),
    IN p_FIRST_NAME VARCHAR(255),
	IN p_LAST_NAME VARCHAR(255),
    IN p_PASSWORD VARCHAR(255),
    IN p_EMAIL VARCHAR(255),
    IN p_MOBILE_NUMBER VARCHAR(255),	
    IN p_MODIFIED_BY INT,
	OUT v_ERROR_CODE VARCHAR(255),
	OUT v_ERROR_MESSAGE VARCHAR(255)
)
BEGIN
    DECLARE EXIT HANDLER FOR SQLEXCEPTION, SQLWARNING
    BEGIN
         GET DIAGNOSTICS CONDITION 1 @sqlstate = RETURNED_SQLSTATE, @errno = MYSQL_ERRNO, @text = MESSAGE_TEXT;
		SET v_ERROR_MESSAGE = @text;
		SET v_ERROR_CODE = CONCAT(@errno, '|', @sqlstate);
		ROLLBACK;
    END;

    SET AUTOCOMMIT=0;
    START TRANSACTION;



    INSERT INTO users_audit (
        MODIFIED_BY, 
        MODIFIED_ON, 
        MODIFICATION, 
        ID, 
        USERNAME,
        FIRST_NAME,
	    LAST_NAME, 
        PASSWORD,
        EMAIL,
        MOBILE_NUMBER
    ) VALUES (
        p_MODIFIED_BY,
        SYSDATE(),
        'UPDATED',
        p_ID,
        p_USERNAME,
        p_FIRST_NAME,
	    p_LAST_NAME, 
        p_PASSWORD,
        p_EMAIL,
        p_MOBILE_NUMBER
    );

    COMMIT;
END$$
DELIMITER ;
