
CREATE OR REPLACE FUNCTION tcg.public.FN_TCG_MTG_Game_Session_Member_Get_Many (
      a_get_inactive_session_member BOOLEAN
    , a_get_all_game BOOLEAN
    , a_get_inactive_game BOOLEAN
	, a_game_ids TEXT
    , a_get_all_user BOOLEAN
    , a_get_inactive_user BOOLEAN
	, a_user_ids TEXT
    , a_require_all_id_filters_met BOOLEAN
    , a_require_any_id_filters_met BOOLEAN
)
RETURNS TABLE (
      member_id INT
    , game_id INT
    , user_id INT
    , is_permitted BOOLEAN
    , active BOOLEAN
    , created_on TIMESTAMP
    , created_by_user_id INT
    , updated_last_on TIMESTAMP
    , updated_last_by_user_id INT
)
LANGUAGE plpgsql
AS $$
DECLARE
    v_get_inactive_session_member BOOLEAN;
    v_get_all_game BOOLEAN;
    v_get_inactive_game BOOLEAN;
	v_game_ids TEXT;
    v_get_all_user BOOLEAN;
    v_get_inactive_user BOOLEAN;
	v_user_ids TEXT;
    v_require_all_id_filters_met BOOLEAN;
    v_require_any_id_filters_met BOOLEAN;
    -- , v_require_all_non_id_filters_met BOOLEAN
    -- , v_require_any_non_id_filters_met BOOLEAN
BEGIN
    v_get_inactive_session_member := COALESCE(a_get_inactive_session_member, FALSE);
    v_get_all_game := COALESCE(a_get_all_game, FALSE);
    v_get_inactive_game := COALESCE(a_get_inactive_game, FALSE);
	v_game_ids := TRIM(COALESCE(a_game_ids, ''));
    v_get_all_user := COALESCE(a_get_all_user, FALSE);
    v_get_inactive_user := COALESCE(a_get_inactive_user, FALSE);
	v_user_ids := TRIM(COALESCE(a_user_ids, ''));
    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
          SESSION_MEMBER.member_id
        , SESSION_MEMBER.game_id
        , SESSION_MEMBER.user_id
        , SESSION_MEMBER.is_permitted
        , SESSION_MEMBER.active
        , SESSION_MEMBER.created_on
        , SESSION_MEMBER.created_by_user_id
        , SESSION_MEMBER.updated_last_on
        , SESSION_MEMBER.updated_last_by_user_id
    FROM tcg.public.TCG_MTG_Game_Session_Member SESSION_MEMBER
    INNER JOIN tcg.public.TCG_MTG_Game GAME ON SESSION_MEMBER.game_id = GAME.game_id
    INNER JOIN tcg.public.TCG_User MEMBER_USER ON SESSION_MEMBER.user_id = MEMBER_USER.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_game
                    OR  SESSION_MEMBER.game_id = ANY(string_to_array(v_game_ids, ',')::INT[])
                )
                AND (
                        a_get_all_user
                    OR  MEMBER_USER.user_id = ANY(string_to_array(v_user_ids, ',')::INT[])
                )
            )
            OR  (
                    NOT v_require_all_id_filters_met
                AND v_require_any_id_filters_met
                AND (
                        v_get_all_game
                    OR  SESSION_MEMBER.game_id = ANY(string_to_array(v_game_ids, ',')::INT[])
                )
                AND (
                        v_get_all_user
                    OR  MEMBER_USER.user_id = ANY(string_to_array(v_user_ids, ',')::INT[])
                )
            )
        )
        AND (
                v_get_inactive_session_member
            OR  SESSION_MEMBER.active
        )
        AND (
                v_get_inactive_game
            OR  GAME.active
        )
        AND (
                v_get_inactive_user
            OR  MEMBER_USER.active
        )
	ORDER BY
          GAME.game_id
        , MEMBER_USER.firstname
        , MEMBER_USER.surname
    ;
END;
$$;


SELECT *
FROM tcg.public.FN_TCG_MTG_Game_Session_Member_Get_Many (
      a_get_inactive_session_member := FALSE
	, a_get_all_game := TRUE
    , a_get_inactive_game := FALSE
    , a_game_ids := ''
    , a_get_all_user := TRUE
    , a_get_inactive_user := FALSE 
    , a_user_ids := ''
    , a_require_all_id_filters_met := TRUE 
    , a_require_any_id_filters_met := FALSE 
)
;

/*
SELECT *
FROM tcg.public.TCG_MTG_Game_Session_Member
;

SELECT *
FROM tcg.public.TCG_User
;

SELECT *
FROM tcg.public.TCG_MTG_Deck
;
*/
