CREATE DEFINER=`computraceqa`@`%` PROCEDURE `computraceqa`.`InsertOrUpdateUser`(
    IN p_user_id INT,
    IN p_user_first_name VARCHAR(100),
    IN p_user_last_name VARCHAR(100),
    IN p_user_email VARCHAR(100),
    IN p_user_status VARCHAR(100),
    IN p_user_role_id INT,
    IN p_is_active BOOL,
    IN p_is_deleted BOOL
  
)
BEGIN
    -- Check if the user already exists
    IF EXISTS (SELECT 1 FROM user_details WHERE user_id = p_user_id) THEN
        -- Update the existing record
        UPDATE user_details
        SET
            user_first_name = p_user_first_name,
            user_last_name = p_user_last_name,
            user_email = p_user_email,
            user_status = p_user_status,
            user_role_id = p_user_role_id,
            is_active = p_is_active,
            is_deleted = p_is_deleted,
            updated_on = CURRENT_TIMESTAMP
        WHERE user_id = p_user_id;
        
    ELSE
        -- Insert a new record
        INSERT INTO user_details (
            user_first_name,
            user_last_name,
            user_email,
            user_status,
            user_role_id,
            is_active,
            is_deleted
        ) VALUES (
            p_user_first_name,
            p_user_last_name,
            p_user_email,
            p_user_status,
            p_user_role_id,
            p_is_active,
            p_is_deleted
        );
        
    END IF;
END