DROP PROCEDURE IF EXISTS `update_profile_details`;
DELIMITER ;;
CREATE PROCEDURE `update_profile_details` (
    -- IN p_password VARCHAR(100),
    IN p_name VARCHAR(50),
    IN p_location VARCHAR(200),
    IN p_bio VARCHAR(200),
    IN p_phone_number CHAR(11),
    IN p_session_id CHAR(64)
)
proc:BEGIN
    IF (SELECT COUNT(session_id) FROM sessions WHERE session_id = LOWER(p_session_id)) = 0 OR p_session_id IS NULL THEN
        SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'update_profile_details: No user logged in';
        LEAVE proc;
    END IF;
    SELECT user_id INTO @user_id FROM sessions WHERE session_id = LOWER(p_session_id);
    IF p_name != '' AND p_name IS NOT NULL THEN
        CALL update_user_name(p_name, @user_id, @status);
        IF @status != 0 THEN
            SIGNAL SQLSTATE '45001' SET MESSAGE_TEXT = 'update_profile_details: No name specified';
            LEAVE proc;
        END IF;
    END IF;
    IF p_location != '' AND p_location IS NOT NULL THEN
        CALL update_user_location(p_location, @user_id, @status);
        IF @status != 0 THEN
            SIGNAL SQLSTATE '45002' SET MESSAGE_TEXT = 'update_profile_details: No location specified';
            LEAVE proc;
        END IF;
    END IF;
    IF p_bio != '' AND p_bio IS NOT NULL THEN
        CALL update_user_bio(p_bio, @user_id, @status);
        IF @status != 0 THEN
            SIGNAL SQLSTATE '45003' SET MESSAGE_TEXT = 'update_profile_details: No bio specified';
            LEAVE proc;
        END IF;
    END IF;
    IF p_phone_number != '' AND p_phone_number IS NOT NULL THEN
        CALL update_user_phone_number(p_phone_number, @user_id, @status);
        IF @status != 0 THEN
            SIGNAL SQLSTATE '45004' SET MESSAGE_TEXT = 'update_profile_details: No phone number specified';
            LEAVE proc;
        END IF;
    END IF;
    SELECT 0 AS 'status', 'update_profile_details' AS 'proc';
END ;;
DELIMITER ;
