
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_created_by_user_ids TEXT
	, a_include_commander_option BOOLEAN
    , a_get_all_game BOOLEAN
    , a_get_inactive_game BOOLEAN
	, a_game_ids TEXT
    , a_filter_by_game_session_not_player 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_created_by_user_ids TEXT;
	v_include_commander_option BOOLEAN;
    v_get_all_game BOOLEAN;
    v_get_inactive_game BOOLEAN;
	v_game_ids TEXT;
    v_filter_by_game_session_not_player 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_created_by_user_ids := TRIM(COALESCE(a_created_by_user_ids, ''));
	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_filter_by_game_session_not_player := COALESCE(a_filter_by_game_session_not_player, 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 DISTINCT
          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
    LEFT JOIN tcg.public.TCG_MTG_Game SESSION_GAME ON DECK.game_id = SESSION_GAME.game_id
    LEFT JOIN tcg.public.TCG_MTG_Game_Player PLAYER ON DECK.deck_id = PLAYER.deck_id
    LEFT JOIN tcg.public.TCG_MTG_Game PLAYER_GAME ON PLAYER.game_id = PLAYER_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_deck
                    OR  (
							DECK.deck_id = ANY(string_to_array(v_deck_ids, ',')::INT[])
						AND DECK.created_by_user_id = ANY(string_to_array(v_created_by_user_ids, ',')::INT[])
						AND (
                                v_get_all_game
                            OR  (
                                    v_filter_by_game_session_not_player
                                AND SESSION_GAME.game_id = ANY(string_to_array(v_game_ids, ',')::INT[])
                            )
                            OR  (
                                    NOT v_filter_by_game_session_not_player
                                AND PLAYER.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_deck
                    OR  DECK.deck_id = ANY(string_to_array(v_deck_ids, ',')::INT[])
					OR  DECK.created_by_user_id = ANY(string_to_array(v_created_by_user_ids, ',')::INT[])
                    OR  (
                            v_get_all_game
                        OR  (
                                v_filter_by_game_session_not_player
                            AND SESSION_GAME.game_id = ANY(string_to_array(v_game_ids, ',')::INT[])
                        )
                        OR  (
                                NOT v_filter_by_game_session_not_player
                            AND PLAYER.game_id = ANY(string_to_array(v_game_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
        )
        AND (
                v_get_inactive_game
            OR  (
                    v_filter_by_game_session_not_player
                AND SESSION_GAME.active
            )
            OR  (
                    NOT v_filter_by_game_session_not_player
                AND PLAYER_GAME.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_created_by_user_ids := '3'
    , a_get_all_game := FALSE
    , a_get_inactive_game := FALSE
    , a_game_ids := CAST(NULL AS TEXT)
    , a_filter_by_game_session_not_player := TRUE
    , a_require_all_id_filters_met := TRUE
    , 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
;


