
CREATE OR REPLACE FUNCTION tcg.public.FN_TCG_MTG_Game_Round_Player_Damage_Get_Many (
      a_get_all_game BOOLEAN
    , a_get_inactive_game BOOLEAN
	, a_game_ids TEXT
    , a_require_all_id_filters_met BOOLEAN
    , a_require_any_id_filters_met BOOLEAN
)
RETURNS TABLE (
      damage_id INT
    , round_id INT
    , player_id INT
    , received_from_commander_player_id INT
    , life_gain INT
    , life_loss INT
    , commander_deaths INT
    , is_eliminated BOOLEAN
    , 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_game BOOLEAN;
    v_get_inactive_game BOOLEAN;
	v_game_ids TEXT;
    v_require_all_id_filters_met BOOLEAN;
    v_require_any_id_filters_met BOOLEAN;
BEGIN
	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_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
          PLAYER_DAMAGE.damage_id
        , PLAYER_DAMAGE.round_id
        , PLAYER_DAMAGE.player_id
        , PLAYER_DAMAGE.received_from_commander_player_id
        , PLAYER_DAMAGE.life_gain
        , PLAYER_DAMAGE.life_loss
        , PLAYER_DAMAGE.commander_deaths
		, PLAYER_DAMAGE.is_eliminated
        , PLAYER_DAMAGE.active
        , PLAYER_DAMAGE.created_on
        , PLAYER_DAMAGE.created_by_user_id
        , PLAYER_DAMAGE.updated_last_on
        , PLAYER_DAMAGE.updated_last_by_user_id
        , PLAYER_DAMAGE.change_set_id
    FROM tcg.public.TCG_MTG_Game_Round_Player_Damage PLAYER_DAMAGE
    INNER JOIN tcg.public.TCG_MTG_Game_Round GAME_ROUND ON PLAYER_DAMAGE.round_id = GAME_ROUND.round_id
    INNER JOIN tcg.public.TCG_MTG_Game GAME ON GAME_ROUND.game_id = GAME.game_id
	INNER JOIN tcg.public.TCG_MTG_Game_Player PLAYER ON PLAYER_DAMAGE.player_id = PLAYER.player_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[])
                )
            )
            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_inactive_game
            OR  PLAYER_DAMAGE.active
        )
	ORDER BY
          GAME.created_on DESC
        , GAME_ROUND.display_order
        , PLAYER.display_order
		, PLAYER_DAMAGE.damage_id
    ;
END;
$$;


SELECT *
FROM tcg.public.FN_TCG_MTG_Game_Round_Player_Damage_Get_Many (
      a_get_all_game := FALSE
    , a_get_inactive_game := FALSE
    , a_game_ids := '30'
    , 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_Player_Damage
;
