
CREATE OR REPLACE FUNCTION tcg.public.FN_TCG_MTG_Game_Round_Get_Many (
      a_access_user_id INT
	, 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 (
      round_id INT
	, game_id INT
    , notes TEXT
    , display_order INT
    , 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_access_user_id INT;
	v_can_admin 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;
BEGIN
	v_access_user_id := a_access_user_id;
	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);
	
    IF NOT EXISTS (SELECT * FROM tcg.public.TCG_User TCG_USER WHERE TCG_USER.user_id = v_access_user_id LIMIT 1) THEN
		RAISE EXCEPTION 'Access User ID required.';
    END IF;

	v_can_admin := COALESCE((SELECT TCG_USER.is_super_user FROM tcg.public.TCG_User TCG_USER WHERE TCG_USER.user_id = v_access_user_id LIMIT 1), FALSE);
	
	-- Outputs

    RETURN QUERY
	WITH Access_Game AS (
		SELECT
			  GAME.game_id
			, GAME.active
			, GAME.created_on
			, GAME.created_by_user_id
		FROM tcg.public.FN_TCG_MTG_Game_Get_Many (
			  a_access_user_id := v_access_user_id
			, a_get_all_game := v_get_all_game
		    , a_get_inactive_game := v_get_inactive_game
		    , a_game_ids := v_game_ids
		    , a_get_all_user := v_get_all_user
		    , a_get_inactive_user := v_get_inactive_user
		    , a_user_ids := v_user_ids
		    , a_require_all_id_filters_met := v_require_all_id_filters_met
		    , a_require_any_id_filters_met := v_require_any_id_filters_met
		) GAME
	)
	SELECT
          TCG_ROUND.round_id
        , TCG_ROUND.game_id
        , TCG_ROUND.notes
        , TCG_ROUND.display_order
        , TCG_ROUND.active
        , TCG_ROUND.created_on
        , TCG_ROUND.created_by_user_id
        , TCG_ROUND.updated_last_on
        , TCG_ROUND.updated_last_by_user_id
        , TCG_ROUND.change_set_id
    FROM tcg.public.TCG_MTG_Game_Round TCG_ROUND
	INNER JOIN Access_Game ACCESS_GAME ON TCG_ROUND.game_id = ACCESS_GAME.game_id
    INNER JOIN tcg.public.TCG_MTG_Game GAME ON TCG_ROUND.game_id = GAME.game_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  TCG_ROUND.game_id = ANY(string_to_array(v_game_ids, ',')::INT[])
                )
            )
            OR  (
                    NOT v_require_all_id_filters_met
                AND v_require_any_id_filters_met
                AND (
                        v_get_all_game
                    OR  TCG_ROUND.game_id = ANY(string_to_array(v_game_ids, ',')::INT[])
                )
            )
        )
        AND (
                v_get_inactive_game
            OR  TCG_ROUND.active
        )
	ORDER BY
          GAME.created_on DESC
        , TCG_ROUND.display_order
    ;
END;
$$;


SELECT *
FROM tcg.public.FN_TCG_MTG_Game_Round_Get_Many (
      a_access_user_id := 3
	, a_get_all_game := FALSE
    , a_get_inactive_game := FALSE
    , a_game_ids := '11'
	, 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
;
SELECT *
FROM tcg.public.TCG_MTG_Game_Round
;
