
CREATE OR REPLACE FUNCTION tcg.public.FN_TCG_MTG_Deck_Commander_Bracket_Get_Many (
      a_get_all_commander_bracket BOOLEAN
    , a_get_inactive_commander_bracket BOOLEAN
	, a_commander_bracket_ids TEXT
	, a_commander_bracket_names TEXT
    , a_require_all_id_filters_met BOOLEAN
    , a_require_any_id_filters_met BOOLEAN
    , a_require_all_non_id_filters_met BOOLEAN
    , a_require_any_non_id_filters_met BOOLEAN
)
RETURNS TABLE (
      commander_bracket_id INT
    , name TEXT
    , description 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_get_all_commander_bracket BOOLEAN;
    v_get_inactive_commander_bracket BOOLEAN;
	v_commander_bracket_ids TEXT;
	v_commander_bracket_names 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_all_commander_bracket := COALESCE(a_get_all_commander_bracket, TRUE);
    v_get_inactive_commander_bracket := COALESCE(a_get_inactive_commander_bracket, FALSE);
	v_commander_bracket_ids := TRIM(COALESCE(a_commander_bracket_ids, ''));
	v_commander_bracket_names := TRIM(COALESCE(a_commander_bracket_names, ''));
    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, TRUE);
    v_require_all_non_id_filters_met := COALESCE(a_require_all_non_id_filters_met, FALSE);
    v_require_any_non_id_filters_met := COALESCE(a_require_any_non_id_filters_met, TRUE);
	
	-- Outputs
    RETURN QUERY SELECT
          COMMANDER_BRACKET.commander_bracket_id
        , COMMANDER_BRACKET.name
        , COMMANDER_BRACKET.description
        , COMMANDER_BRACKET.display_order
        , COMMANDER_BRACKET.active
        , COMMANDER_BRACKET.created_on
        , COMMANDER_BRACKET.created_by_user_id
        , COMMANDER_BRACKET.updated_last_on
        , COMMANDER_BRACKET.updated_last_by_user_id
        , COMMANDER_BRACKET.change_set_id
    FROM tcg.public.TCG_MTG_Deck_Commander_Bracket COMMANDER_BRACKET
    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_commander_bracket
                    OR  COMMANDER_BRACKET.commander_bracket_id = ANY(string_to_array(v_commander_bracket_ids, ',')::INT[])
                )
            )
            OR  (
                    NOT v_require_all_id_filters_met
                AND v_require_any_id_filters_met
                AND (
                        v_get_all_commander_bracket
                    OR  COMMANDER_BRACKET.commander_bracket_id = ANY(string_to_array(v_commander_bracket_ids, ',')::INT[])
                )
            )
        )
        AND (
                (
                    NOT v_require_all_non_id_filters_met
                AND NOT v_require_any_non_id_filters_met
            )
            OR  (
                    v_require_all_non_id_filters_met
                AND (
                        v_get_all_commander_bracket
                    OR  COMMANDER_BRACKET.name LIKE ANY (SELECT '%' || TRIM(s) || '%' FROM unnest(string_to_array(v_commander_bracket_names, ',')) s)
                )
            )
            OR  (
                    NOT v_require_all_non_id_filters_met
                AND v_require_any_non_id_filters_met
                AND (
                        v_get_all_commander_bracket
                    OR  COMMANDER_BRACKET.name LIKE ANY (SELECT '%' || TRIM(s) || '%' FROM unnest(string_to_array(v_commander_bracket_names, ',')) s)
                )
            )
        )
        AND (
                v_get_inactive_commander_bracket
            OR  COMMANDER_BRACKET.active
        )
	ORDER BY COMMANDER_BRACKET.display_order
    ;
END;
$$;


SELECT *
FROM tcg.public.FN_TCG_MTG_Deck_Commander_Bracket_Get_Many (
      a_get_all_commander_bracket := TRUE
    , a_get_inactive_commander_bracket := FALSE
    , a_commander_bracket_ids := CAST(NULL AS TEXT)
    , a_commander_bracket_names := CAST(NULL AS TEXT)
    , a_require_all_id_filters_met := FALSE
    , a_require_any_id_filters_met := FALSE
    , a_require_all_non_id_filters_met := FALSE
    , a_require_any_non_id_filters_met := FALSE
)
;

SELECT *
FROM tcg.public.TCG_MTG_Deck_Commander_Bracket
;


