
CREATE OR REPLACE FUNCTION tcg.public.FN_TCG_MTG_Deck_Get_Many (
      a_get_all_deck BOOLEAN
    , a_get_inactive_deck BOOLEAN
	, a_deck_ids TEXT
	, a_deck_names TEXT
	, a_commander_bracket_ids TEXT
	, a_include_commander_option BOOLEAN
    , 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 (
      deck_id INT
    , name TEXT
    , is_commander BOOLEAN
    , commander_bracket_id 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_deck BOOLEAN;
    v_get_inactive_deck BOOLEAN;
	v_deck_ids TEXT;
	v_deck_names TEXT;
	v_include_commander_option BOOLEAN;
    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_deck := COALESCE(a_get_all_deck, FALSE);
    v_get_inactive_deck := COALESCE(a_get_inactive_deck, FALSE);
	v_deck_ids := TRIM(COALESCE(a_deck_ids, ''));
	v_deck_names := TRIM(COALESCE(a_deck_names, ''));
    v_include_commander_option := COALESCE(a_include_commander_option, TRUE);
    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);
    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, FALSE);
	
	-- Outputs
    RETURN QUERY SELECT
          DECK.deck_id
        , DECK.name
        , DECK.is_commander
        , DECK.commander_bracket_id
        , DECK.active
        , DECK.created_on
        , DECK.created_by_user_id
        , DECK.updated_last_on
        , DECK.updated_last_by_user_id
        , DECK.change_set_id
    FROM tcg.public.TCG_MTG_Deck DECK
    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_deck
                    OR  DECK.deck_id = ANY(string_to_array(v_deck_ids, ',')::INT[])
                )
            )
            OR  (
                    NOT v_require_all_id_filters_met
                AND v_require_any_id_filters_met
                AND (
                        v_get_all_deck
                    OR  DECK.deck_id = ANY(string_to_array(v_deck_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_deck
                    OR  DECK.name LIKE ANY (SELECT '%' || TRIM(s) || '%' FROM unnest(string_to_array(v_deck_names, ',')) s)
                )
            )
            OR  (
                    NOT v_require_all_non_id_filters_met
                AND v_require_any_non_id_filters_met
                AND (
                        v_get_all_deck
                    OR  DECK.name LIKE ANY (SELECT '%' || TRIM(s) || '%' FROM unnest(string_to_array(v_deck_names, ',')) s)
                )
            )
        )
        AND (
				v_include_commander_option
			OR  (
	                NOT v_include_commander_option
	            AND NOT DECK.is_commander
			)
        )
        AND (
                v_get_inactive_deck
            OR  DECK.active
        )
	ORDER BY DECK.name
    ;
END;
$$;


SELECT *
FROM tcg.public.FN_TCG_MTG_Deck_Get_Many (
      a_get_all_deck := TRUE
    , a_get_inactive_deck := FALSE
    , a_deck_ids := CAST(NULL AS TEXT)
    , a_deck_names := CAST(NULL AS TEXT)
    , a_commander_bracket_ids := CAST(NULL AS TEXT)
    , a_include_commander_option := TRUE
    , 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
;


