
CREATE OR REPLACE FUNCTION tcg.public.FN_TCG_MTG_Game_Get_Many (
      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 (
      game_id INT
    , notes TEXT
    , is_commander BOOLEAN
    , is_draft BOOLEAN
    , is_sealed BOOLEAN
    , location_name TEXT
    , start_on TIMESTAMP
    , end_on TIMESTAMP
    , starting_life INT
    , 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_bad_data_error_type_id INT;
	-- v_warning_error_type_id INT;
    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_bad_data_error_type_id := (SELECT ERROR_TYPE.error_type_id FROM tcg.public.Error_Type ERROR_TYPE WHERE ERROR_TYPE.code = 'BAD_DATA' LIMIT 1);
	-- v_warning_error_type_id := (SELECT ERROR_TYPE.error_type_id FROM tcg.public.Error_Type ERROR_TYPE WHERE ERROR_TYPE.code = 'WARNING' LIMIT 1);
    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
          GAME.game_id
        , GAME.notes
        , GAME.is_commander
        , GAME.is_draft
        , GAME.is_sealed
        , GAME.location_name
        , GAME.start_on
        , GAME.end_on
		, GAME.starting_life
        , GAME.active
        , GAME.created_on
        , GAME.created_by_user_id
        , GAME.updated_last_on
        , GAME.updated_last_by_user_id
    FROM tcg.public.TCG_MTG_Game GAME
    INNER JOIN tcg.public.TCG_User CREATOR_USER ON GAME.created_by_user_id = CREATOR_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  GAME.game_id = ANY(string_to_array(v_game_ids, ',')::INT[])
                )
                AND (
                        a_get_all_user
                    OR  CREATOR_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  GAME.game_id = ANY(string_to_array(v_game_ids, ',')::INT[])
                )
                AND (
                        v_get_all_user
                    OR  CREATOR_USER.user_id = ANY(string_to_array(v_user_ids, ',')::INT[])
                )
            )
        )
        AND (
                v_get_inactive_game
            OR  GAME.active
        )
        AND (
                v_get_inactive_user
            OR  CREATOR_USER.active
        )
	ORDER BY GAME.created_on DESC
    ;
END;
$$;


SELECT *
FROM tcg.public.FN_TCG_MTG_Game_Get_Many (
      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
;

SELECT *
FROM tcg.public.TCG_User
;

SELECT *
FROM tcg.public.TCG_MTG_Deck
;
*/
