
CREATE OR REPLACE FUNCTION tcg.public.FN_TCG_MTG_Game_Player_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 (
      player_id INT
	, game_id INT
    , user_id INT
    , deck_id INT
    , name TEXT
    , notes 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_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
	WITH User_Name AS (
		SELECT
			TCG_USER.user_id
			, CASE
				WHEN
						TRIM(COALESCE(TCG_USER.firstname, '')) <> ''
					AND TRIM(COALESCE(TCG_USER.surname, '')) <> ''
				THEN CONCAT(
					  TRIM(COALESCE(TCG_USER.firstname, ''))
					, ' '
					, TRIM(COALESCE(TCG_USER.surname, ''))
				)
				WHEN TRIM(COALESCE(TCG_USER.firstname, '')) <> ''
				THEN TRIM(COALESCE(TCG_USER.firstname, ''))
				ELSE TRIM(COALESCE(TCG_USER.surname, ''))
			END AS name
		FROM tcg.public.TCG_User TCG_USER
	)
	SELECT
          PLAYER.player_id
        , PLAYER.game_id
    	, PLAYER.user_id
    	, PLAYER.deck_id
        , CASE
			WHEN TRIM(COALESCE(PLAYER.name, '')) <> ''
			THEN TRIM(COALESCE(PLAYER.name, ''))
			WHEN
					USER_NAME.name <> ''
				AND TRIM(COALESCE(DECK.name, '')) <> ''
			THEN CONCAT(
				  USER_NAME.name
				, ' - '
				, TRIM(COALESCE(DECK.name, ''))
			)
			ELSE CONCAT(
				  'Player '
				, CAST(PLAYER.display_order AS VARCHAR)
			)
		END AS name
        , PLAYER.notes
        , PLAYER.display_order
        , PLAYER.active
        , PLAYER.created_on
        , PLAYER.created_by_user_id
        , PLAYER.updated_last_on
        , PLAYER.updated_last_by_user_id
        , PLAYER.change_set_id
    FROM tcg.public.TCG_MTG_Game_Player PLAYER
    INNER JOIN tcg.public.TCG_MTG_Game GAME ON PLAYER.game_id = GAME.game_id
	-- LEFT JOIN tcg.public.TCG_User TCG_USER ON PLAYER.user_id = TCG_USER.user_id
	LEFT JOIN User_Name USER_NAME ON PLAYER.user_id = USER_NAME.user_id
	LEFT JOIN tcg.public.TCG_MTG_Deck DECK ON PLAYER.deck_id = DECK.deck_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  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_game
                    OR  PLAYER.game_id = ANY(string_to_array(v_game_ids, ',')::INT[])
                )
            )
        )
        AND (
                v_get_inactive_game
            OR  GAME.active
        )
	ORDER BY
          GAME.created_on DESC
        , PLAYER.display_order
    ;
END;
$$;


SELECT *
FROM tcg.public.FN_TCG_MTG_Game_Player_Get_Many (
      a_get_all_game := FALSE
    , a_get_inactive_game := FALSE
    , a_game_ids := '34'
    , 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_Player_Temp
;
SELECT *
FROM tcg.public.TCG_MTG_Game_Player
;

/*
select *
from information_schema.tables T
where UPPER(T.table_name)  like '%PLAYER_T%'
;



INSERT INTO tcg.public.TCG_Change_Set (
	comment
	, updated_last_by_user_id
)
VALUES (
	'Alter player names'
	, 3
);
select *
from tcg.public.TCG_Change_Set cs
order by cs.change_set_id  desc

WITH PLAYER_NAME AS (
	SELECT
		PLAYER.player_id
		, CONCAT(
			COALESCE(TCG_USER.firstname, 'Error')
			, ' - '
			, COALESCE(DECK.name, 'Error')
		) AS name
	FROM tcg.public.TCG_MTG_Game_Player PLAYER
	LEFT JOIN tcg.public.TCG_User TCG_USER ON PLAYER.user_id = TCG_USER.user_id
	LEFT JOIN tcg.public.TCG_MTG_Deck DECK ON PLAYER.deck_id = DECK.deck_id
)
UPDATE tcg.public.TCG_MTG_Game_Player PLAYER
SET
	name = PLAYER_NAME.name
	, change_set_id = 110
FROM PLAYER_NAME
WHERE PLAYER.player_id = PLAYER_NAME.player_id
AND PLAYER.player_id >= 6


		INSERT INTO tcg.public.TCG_MTG_Game_Round (
			  game_id
		    , notes
		    , display_order
		    , active
			, created_by_user_id
			, updated_last_by_user_id
		    , change_set_id
		)
		VALUES (
			  11 -- game_id
            , null -- notes
		    , 1 -- display_order
            , TRUE -- active
			, 3 -- created_by_user_id
			, 3 -- updated_last_by_user_id
		    , 109 -- change_set_id
        );
*/

