
CREATE OR REPLACE FUNCTION tcg.public.FN_TCG_User_Relationship_Get_Many (
      a_get_inactive_user_relationship BOOLEAN
    , a_get_all_user_follower BOOLEAN
	, a_user_follower_ids TEXT
    , a_get_all_user_following BOOLEAN
	, a_user_following_ids TEXT
    , a_get_inactive_user BOOLEAN
    , a_require_all_id_filters_met BOOLEAN
    , a_require_any_id_filters_met BOOLEAN
)
RETURNS TABLE (
      relationship_id INT
    , follower_user_id INT
    , following_user_id INT
    , is_following BOOLEAN
    , is_blocked BOOLEAN
    , active BOOLEAN
    , created_on TIMESTAMP
    , created_by_user_id INT
	, updated_last_on TIMESTAMP
	, updated_last_by_user_id INT
    , change_set_id INT
)
LANGUAGE plpgsql
AS $$
DECLARE
    v_get_inactive_user_relationship BOOLEAN;
    v_get_all_user_follower BOOLEAN;
	v_user_follower_ids TEXT;
    v_get_all_user_following BOOLEAN;
	v_user_following_ids TEXT;
    v_get_inactive_user BOOLEAN;
    v_require_all_id_filters_met BOOLEAN;
    v_require_any_id_filters_met BOOLEAN;
BEGIN
    v_get_inactive_user_relationship := COALESCE(a_get_inactive_user_relationship, FALSE);
	v_get_all_user_follower := COALESCE(a_get_all_user_follower, FALSE);
	v_user_follower_ids := TRIM(COALESCE(a_user_follower_ids, ''));
	v_get_all_user_following := COALESCE(a_get_all_user_following, FALSE);
	v_user_following_ids := TRIM(COALESCE(a_user_following_ids, ''));
    v_get_inactive_user := COALESCE(a_get_inactive_user, FALSE);
    v_require_all_id_filters_met := COALESCE(a_require_all_id_filters_met, FALSE);
    v_require_any_id_filters_met := COALESCE(a_require_any_id_filters_met, FALSE);
	
	-- Outputs
    RETURN QUERY SELECT
          RELATIONSHIP.relationship_id
        , RELATIONSHIP.follower_user_id
        , RELATIONSHIP.following_user_id
        , RELATIONSHIP.is_following
        , RELATIONSHIP.is_blocked
        , RELATIONSHIP.active
        , RELATIONSHIP.created_on
        , RELATIONSHIP.created_by_user_id
        , RELATIONSHIP.updated_last_on
        , RELATIONSHIP.updated_last_by_user_id
        , RELATIONSHIP.change_set_id
    FROM tcg.public.TCG_User_Relationship RELATIONSHIP
    INNER JOIN tcg.public.TCG_User USER_FOLLOWER ON RELATIONSHIP.follower_user_id = USER_FOLLOWER.user_id
    INNER JOIN tcg.public.TCG_User USER_FOLLOWING ON RELATIONSHIP.following_user_id = USER_FOLLOWING.user_id
    WHERE
            (
                (
                    NOT v_require_all_id_filters_met
                AND NOT v_require_any_id_filters_met
            )
            OR  (
                    v_require_all_id_filters_met
                AND (
                        v_get_all_user_follower
                    OR  USER_FOLLOWER.user_id = ANY(string_to_array(v_user_follower_ids, ',')::INT[])
                )
                AND (
                        v_get_all_user_following
                    OR  USER_FOLLOWING.user_id = ANY(string_to_array(v_user_following_ids, ',')::INT[])
                )
            )
            OR  (
                    NOT v_require_all_id_filters_met
                AND v_require_any_id_filters_met
                AND (
                    (
                            v_get_all_user_follower
                        OR  USER_FOLLOWER.user_id = ANY(string_to_array(v_user_follower_ids, ',')::INT[])
                    )
                    OR  (
                            v_get_all_user_following
                        OR  USER_FOLLOWING.user_id = ANY(string_to_array(v_user_following_ids, ',')::INT[])
                    )
                )
            )
        )
        AND (
                v_get_inactive_user_relationship
            OR  RELATIONSHIP.active
        )
        AND (
                v_get_inactive_user
            OR  (
                    USER_FOLLOWER.active
                AND USER_FOLLOWING.active
            )
        )
	ORDER BY
          RELATIONSHIP.follower_user_id
        , NOT RELATIONSHIP.is_following
        , NOT RELATIONSHIP.is_blocked
        , RELATIONSHIP.following_user_id
    ;
END;
$$;


SELECT *
FROM tcg.public.FN_TCG_User_Relationship_Get_Many (
      a_get_inactive_user_relationship := FALSE
    , a_get_all_user_follower := TRUE
	, a_user_follower_ids := ''
    , a_get_all_user_following := TRUE
	, a_user_following_ids := ''
    , a_get_inactive_user := FALSE
    , a_require_all_id_filters_met := TRUE 
    , a_require_any_id_filters_met := FALSE 
)
;


SELECT *
FROM tcg.public.TCG_User_Relationship
;
