
CREATE OR REPLACE PROCEDURE tcg.public.USP_TCG_MTG_Game_Player_Save (
      a_comment TEXT
    , a_guid UUID
    , a_user_id INT
	, OUT o_success BOOLEAN
)
LANGUAGE plpgsql
AS $$
DECLARE
	v_bad_data_error_type_id INT;
	v_change_set_id INT;
    v_comment TEXT;
	-- v_error_message TEXT;
    v_guid UUID;
	v_time_start TIMESTAMP;
    v_user_id INT;
	v_warning_error_type_id INT;
BEGIN
    v_comment := TRIM(COALESCE(a_comment, ''));
    v_guid := a_guid;
    v_user_id := a_user_id;

	v_bad_data_error_type_id := (SELECT ERROR_TYPE.error_type_id FROM tcg.public.Error_Type ERROR_TYPE WHERE ERROR_TYPE.code = 'BAD_DATA' LIMIT 1);
	v_warning_error_type_id := (SELECT ERROR_TYPE.error_type_id FROM tcg.public.Error_Type ERROR_TYPE WHERE ERROR_TYPE.code = 'WARNING' LIMIT 1);
	v_time_start := CURRENT_TIMESTAMP;
	
	DROP TABLE IF EXISTS Temp_MTG_Player_Save_Game;
	DROP TABLE IF EXISTS Temp_MTG_Player_Save_User;
	DROP TABLE IF EXISTS Temp_MTG_Player_Save_Deck;
	DROP TABLE IF EXISTS Temp_MTG_Player_Save_Player;
	DROP TABLE IF EXISTS Temp_MTG_Player_Save_User_Relationship;
	DROP TABLE IF EXISTS Temp_MTG_Player_Save_Error;
        
    CREATE TEMP TABLE Temp_MTG_Player_Save_Game (
          game_id INT NOT NULL
        , notes TEXT
        , is_commander BOOLEAN
        , is_draft BOOLEAN
        , is_sealed BOOLEAN
        , location_name TEXT
        , start_on TIMESTAMP
        , end_on TIMESTAMP
        , starting_life 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
    );
        
    CREATE TEMP TABLE Temp_MTG_Player_Save_User (
          user_id INT NOT NULL
        , user_auth0_id TEXT
        , firstname TEXT
        , surname TEXT
        , email TEXT
        , is_email_verified BOOLEAN
        , is_super_user 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
    );

    CREATE TEMP TABLE Temp_MTG_Player_Save_Deck (
          deck_id INT NOT NULL
        , 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
    );

    CREATE TEMP TABLE Temp_MTG_Player_Save_Player (
          temp_id INT NOT NULL
        , 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

        , is_new BOOLEAN
        , error_name TEXT
        , is_duplicate BOOLEAN
    );

    CREATE TEMP TABLE Temp_MTG_Player_Save_User_Relationship (
          relationship_id INT
        , follower_user_id INT
        , following_user_id INT
        , is_following BOOLEAN
        , is_blocked 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

        , is_new BOOLEAN
		, error_name TEXT
    );

	CREATE TEMP TABLE Temp_MTG_Player_Save_Error (
		  temp_id INT GENERATED ALWAYS AS IDENTITY
		, error_type_id INT
		, message TEXT
	);
	
	WITH Temp_Player AS (
		SELECT
			  PLAYER_T.temp_id
			, CASE WHEN COALESCE(PLAYER_T.player_id, 0) < 1
				THEN PLAYER.player_id
				ELSE PLAYER_T.player_id
			END AS player_id
	        , PLAYER_T.game_id
	        , PLAYER_T.user_id
	        , PLAYER_T.deck_id
	        , PLAYER_T.name
	        , PLAYER_T.notes
	        , PLAYER_T.display_order
	        , COALESCE(PLAYER_T.active, TRUE) AS active
	        , COALESCE(PLAYER_T.created_on, v_time_start) AS created_on
	        , COALESCE(PLAYER_T.created_by_user_id, v_user_id) AS created_by_user_id
	        , COALESCE(PLAYER_T.updated_last_on, v_time_start) AS updated_last_on
	        , COALESCE(PLAYER_T.updated_last_by_user_id, v_user_id) AS updated_last_by_user_id
		FROM tcg.public.TCG_MTG_Game_Player_Temp PLAYER_T
		LEFT JOIN tcg.public.TCG_MTG_Game_Player PLAYER
			ON 	PLAYER_T.player_id = PLAYER.player_id
			OR  (
					COALESCE(PLAYER_T.player_id, 0) < 1
				AND PLAYER_T.game_id = PLAYER.game_id
				AND PLAYER_T.display_order = PLAYER.display_order
			)
		WHERE PLAYER_T.guid = v_guid
	)
	INSERT INTO Temp_MTG_Player_Save_Player (
		  temp_id
        , player_id
        , game_id
        , user_id
        , deck_id
        , name
        , notes
        , display_order
        , active
        , created_on
        , created_by_user_id
        , updated_last_on
        , updated_last_by_user_id

		, is_new
		, error_name
	)
	SELECT
		  T_PLAYER.temp_id
        , T_PLAYER.player_id
        , T_PLAYER.game_id
        , T_PLAYER.user_id
        , T_PLAYER.deck_id
        , T_PLAYER.name
        , T_PLAYER.notes
        , T_PLAYER.display_order
        , T_PLAYER.active
        , T_PLAYER.created_on
        , T_PLAYER.created_by_user_id
        , T_PLAYER.updated_last_on
        , T_PLAYER.updated_last_by_user_id

		, CASE WHEN T_PLAYER.player_id IS NULL THEN TRUE ELSE FALSE END AS is_new
		, CONCAT(
			  'Player { id: '
			, CAST(T_PLAYER.player_id AS VARCHAR)
			, ', temp id: '
			, CAST(T_PLAYER.temp_id AS VARCHAR)
			, ', display order: '
			, CAST(T_PLAYER.display_order AS VARCHAR)
			, ', game id: '
			, CAST(T_PLAYER.game_id AS VARCHAR)
			, ', name: '
			, T_PLAYER.name
			, ' }'
		) -- error_name
	FROM Temp_Player T_PLAYER
	;
	
    INSERT INTO Temp_MTG_Player_Save_Game (
          game_id
        , notes
        , is_commander
        , is_draft
        , is_sealed
        , location_name
        , start_on
        , end_on
        , starting_life
        , active
        , created_on
        , created_by_user_id
        , updated_last_on
        , updated_last_by_user_id
        , change_set_id
    )
	SELECT DISTINCT
		  GAME.game_id
        , GAME.notes
        , GAME.is_commander
        , GAME.is_draft
        , GAME.is_sealed
        , GAME.location_name
        , GAME.start_on
        , GAME.end_on
        , GAME.starting_life
        , GAME.active
        , GAME.created_on
        , GAME.created_by_user_id
        , GAME.updated_last_on
        , GAME.updated_last_by_user_id
        , GAME.change_set_id
	FROM tcg.public.TCG_MTG_Game GAME
	INNER JOIN Temp_MTG_Player_Save_Player PLAYER_T ON GAME.game_id = PLAYER_T.game_id
	;

    INSERT INTO Temp_MTG_Player_Save_User (
          user_id
        , user_auth0_id
        , firstname
        , surname
        , email
        , is_email_verified
        , is_super_user
        , active
        , created_on
        , created_by_user_id
        , updated_last_on
        , updated_last_by_user_id
        , change_set_id
    )
	SELECT DISTINCT
          TCG_USER.user_id
        , TCG_USER.user_auth0_id
        , TCG_USER.firstname
        , TCG_USER.surname
        , TCG_USER.email
        , TCG_USER.is_email_verified
        , TCG_USER.is_super_user
        , TCG_USER.active
        , TCG_USER.created_on
        , TCG_USER.created_by_user_id
        , TCG_USER.updated_last_on
        , TCG_USER.updated_last_by_user_id
        , TCG_USER.change_set_id
	FROM tcg.public.TCG_User TCG_USER
	INNER JOIN Temp_MTG_Player_Save_Player PLAYER_T ON TCG_USER.user_id = PLAYER_T.user_id
	;

    INSERT INTO Temp_MTG_Player_Save_Deck (
          deck_id
        , name
        , is_commander
        , commander_bracket_id
        , active
        , created_on
        , created_by_user_id
        , updated_last_on
        , updated_last_by_user_id
        , change_set_id
    )
	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
	INNER JOIN Temp_MTG_Player_Save_Player PLAYER ON DECK.deck_id = DECK.deck_id
	;

	WITH
		  All_Game_User_Link AS (
			SELECT
				  T_GAME.game_id
				, T_USER.user_id
			FROM Temp_MTG_Player_Save_Game T_GAME
			CROSS JOIN Temp_MTG_Player_Save_User T_USER
			WHERE
					T_GAME.active
				AND T_USER.active
		)
		, All_User_Relationship AS (
			SELECT DISTINCT
		          RELATIONSHIP.relationship_id
		        , GAME_USER_FOLLOWER.user_id AS follower_user_id
		        , GAME_USER_FOLLOWING.user_id AS following_user_id
		        , COALESCE(RELATIONSHIP.is_following, TRUE) AS is_following
		        , COALESCE(RELATIONSHIP.is_blocked, FALSE) AS is_blocked
		        , COALESCE(RELATIONSHIP.active, TRUE) AS active
		        , COALESCE(RELATIONSHIP.created_on, v_time_start) AS created_on
		        , COALESCE(RELATIONSHIP.created_by_user_id, v_user_id) AS created_by_user_id
		        , COALESCE(RELATIONSHIP.updated_last_on, v_time_start) AS updated_last_on
		        , COALESCE(RELATIONSHIP.updated_last_by_user_id, v_user_id) AS updated_last_by_user_id
		        , RELATIONSHIP.change_set_id
		
		        , CASE WHEN RELATIONSHIP.relationship_id IS NULL THEN TRUE ELSE FALSE END AS Is_New
			FROM All_Game_User_Link GAME_USER_FOLLOWER
			CROSS JOIN All_Game_User_Link GAME_USER_FOLLOWING
		    LEFT JOIN tcg.public.TCG_User_Relationship RELATIONSHIP
		        ON  GAME_USER_FOLLOWER.user_id = RELATIONSHIP.follower_user_id
		        AND GAME_USER_FOLLOWING.user_id = RELATIONSHIP.following_user_id
			WHERE
					GAME_USER_FOLLOWER.user_id <> GAME_USER_FOLLOWING.user_id
				AND GAME_USER_FOLLOWER.game_id = GAME_USER_FOLLOWING.game_id
		)
    INSERT INTO Temp_MTG_Player_Save_User_Relationship (
          relationship_id
        , follower_user_id
        , following_user_id
        , is_following
        , is_blocked
        , active
        , created_on
        , created_by_user_id
        , updated_last_on
        , updated_last_by_user_id
        , change_set_id

        , is_new
		, error_name
    )
	SELECT
          ALL_RELATIONSHIP.relationship_id
        , ALL_RELATIONSHIP.follower_user_id
        , ALL_RELATIONSHIP.following_user_id
        , ALL_RELATIONSHIP.is_following
        , ALL_RELATIONSHIP.is_blocked
        , ALL_RELATIONSHIP.active
        , ALL_RELATIONSHIP.created_on
        , ALL_RELATIONSHIP.created_by_user_id
        , ALL_RELATIONSHIP.updated_last_on
        , ALL_RELATIONSHIP.updated_last_by_user_id
        , ALL_RELATIONSHIP.change_set_id

        , ALL_RELATIONSHIP.Is_New
		, CONCAT(
			  'User Relationship { id: '
			, CAST(ALL_RELATIONSHIP.relationship_id AS VARCHAR)
			, ', follower user id: '
			, CAST(ALL_RELATIONSHIP.follower_user_id AS VARCHAR)
			, ', following user id: '
			, CAST(ALL_RELATIONSHIP.following_user_id AS VARCHAR)
			, ', is following: '
			, CAST(ALL_RELATIONSHIP.is_following AS VARCHAR)
			, ', is blocked: '
			, CAST(ALL_RELATIONSHIP.is_blocked AS VARCHAR)
			, ' is new: '
			, CAST(ALL_RELATIONSHIP.is_new AS VARCHAR)
			, ' }'
		) -- error_name
	FROM All_User_Relationship ALL_RELATIONSHIP
	-- WHERE ALL_RELATIONSHIP.Is_New
	;

	-- Missing fields
	WITH Player_Missing_Field AS (
        SELECT
              T_PLAYER.temp_id
            , 'game_id' AS field
        FROM Temp_MTG_Player_Save_Player T_PLAYER
        WHERE T_PLAYER.game_id IS NULL
        UNION
        SELECT
              T_PLAYER.temp_id
            , 'user_id' AS field
        FROM Temp_MTG_Player_Save_Player T_PLAYER
        WHERE T_PLAYER.user_id IS NULL
        UNION
        SELECT
              T_PLAYER.temp_id
            , 'deck_id' AS field
        FROM Temp_MTG_Player_Save_Player T_PLAYER
        WHERE T_PLAYER.deck_id IS NULL
        UNION
        SELECT
              T_PLAYER.temp_id
            , 'display_order' AS field
        FROM Temp_MTG_Player_Save_Player T_PLAYER
        WHERE T_PLAYER.display_order IS NULL
    )
	INSERT INTO Temp_MTG_Player_Save_Error (
		  error_type_id
		, message
	)
	SELECT
		  v_bad_data_error_type_id -- error_type_id
		, CONCAT(
			'Missing mandatory field: '
			, PLAYER_MISSING_FIELD.field
			, ' on '
			, T_PLAYER.error_name
			, '.'
		) -- message
	FROM Temp_MTG_Player_Save_Player T_PLAYER
	INNER JOIN Player_Missing_Field PLAYER_MISSING_FIELD ON T_PLAYER.temp_id = PLAYER_MISSING_FIELD.temp_id
	;

	-- Failed join fields
	WITH Player_Failed_Join_Field AS (
        SELECT
              T_PLAYER.temp_id
            , 'game_id' AS field
        FROM Temp_MTG_Player_Save_Player T_PLAYER
        LEFT JOIN Temp_MTG_Player_Save_Game T_GAME ON T_PLAYER.game_id = T_GAME.game_id
        WHERE T_GAME.game_id IS NULL
        UNION
        SELECT
              T_PLAYER.temp_id
            , 'user_id' AS field
        FROM Temp_MTG_Player_Save_Player T_PLAYER
        LEFT JOIN Temp_MTG_Player_Save_User T_USER ON T_PLAYER.user_id = T_USER.user_id
        WHERE T_USER.user_id IS NULL
        UNION
        SELECT
              T_PLAYER.temp_id
            , 'deck_id' AS field
        FROM Temp_MTG_Player_Save_Player T_PLAYER
        LEFT JOIN Temp_MTG_Player_Save_Deck T_DECK ON T_PLAYER.deck_id = T_DECK.deck_id
        WHERE T_DECK.deck_id IS NULL
    )
	INSERT INTO Temp_MTG_Player_Save_Error (
		  error_type_id
		, message
	)
	SELECT
		  v_bad_data_error_type_id -- error_type_id
		, CONCAT(
			'Failed relationship: '
			, PLAYER_FAILED_JOIN_FIELD.field
			, ' on '
			, T_PLAYER.error_name
			, '.'
		) -- message
	FROM Temp_MTG_Player_Save_Player T_PLAYER
	INNER JOIN Player_Failed_Join_Field PLAYER_FAILED_JOIN_FIELD ON T_PLAYER.temp_id = PLAYER_FAILED_JOIN_FIELD.temp_id
	;

	-- Duplicate Player
    -- By display order or deck - -add this later
	WITH 
		  All_Player AS (
			SELECT
				  T_PLAYER.temp_id
                , T_PLAYER.game_id
                , T_PLAYER.deck_id
                , T_PLAYER.display_order
                , TRUE AS is_save_record
			FROM Temp_MTG_Player_Save_Player T_PLAYER
            UNION
			SELECT
				  NULL AS temp_id
                , PLAYER.game_id
                , PLAYER.deck_id
                , PLAYER.display_order
                , FALSE AS is_save_record
			FROM tcg.public.TCG_MTG_Game_Player PLAYER
            LEFT JOIN Temp_MTG_Player_Save_Player T_PLAYER ON PLAYER.player_id = T_PLAYER.player_id
            WHERE
                    T_PLAYER.player_id IS NULL
                AND PLAYER.active
		)
		, Player_Display_Order AS (
			SELECT
				  ALL_PLAYER.game_id
                , ALL_PLAYER.display_order
                , COUNT(*) AS count_display_order
			FROM All_Player ALL_PLAYER
            GROUP BY
                  ALL_PLAYER.game_id
                , ALL_PLAYER.display_order
		)
		, Player_Deck AS (
			SELECT
				  ALL_PLAYER.game_id
                , ALL_PLAYER.deck_id
                , COUNT(*) AS count_deck
			FROM All_Player ALL_PLAYER
            GROUP BY
                  ALL_PLAYER.game_id
                , ALL_PLAYER.deck_id
		)
		, Duplicate_Player AS (
			SELECT
				  ALL_PLAYER.temp_id
                , ALL_PLAYER.game_id
                , ALL_PLAYER.deck_id
                , ALL_PLAYER.display_order
				-- , CASE WHEN CARD.temp_id IS NULL THEN TRUE ELSE FALSE END AS is_unique
                , CASE WHEN PLAYER_DECK.count_deck = 1 THEN TRUE ELSE FALSE END AS is_unique_deck
                , CASE WHEN PLAYER_DISPLAY_ORDER.count_display_order = 1 THEN TRUE ELSE FALSE END AS is_unique_display_order
			FROM All_Player ALL_PLAYER
            INNER JOIN Player_Deck PLAYER_DECK
                ON  ALL_PLAYER.game_id = PLAYER_DECK.game_id
                AND ALL_PLAYER.deck_id = PLAYER_DECK.deck_id
            INNER JOIN Player_Display_Order PLAYER_DISPLAY_ORDER
                ON  ALL_PLAYER.game_id = PLAYER_DISPLAY_ORDER.game_id
                AND ALL_PLAYER.display_order = PLAYER_DISPLAY_ORDER.display_order
		)
	UPDATE Temp_MTG_Player_Save_Player T_PLAYER
	SET is_duplicate = CASE WHEN NOT (
            DUPLICATE_PLAYER.is_unique_deck
        AND DUPLICATE_PLAYER.is_unique_display_order
    ) THEN TRUE ELSE FALSE END
	FROM Duplicate_Player DUPLICATE_PLAYER
	WHERE T_PLAYER.temp_id = DUPLICATE_PLAYER.temp_id
	;
	
	IF EXISTS (
		SELECT *
		FROM Temp_MTG_Player_Save_Player T_PLAYER
		WHERE T_PLAYER.is_duplicate
		LIMIT 1
	) THEN
		INSERT INTO Temp_MTG_Player_Save_Error (
			  error_type_id
			, message
		)
		SELECT
			  v_bad_data_error_type_id -- error_type_id
			, CONCAT(
				  'Duplicate on '
				, T_PLAYER.error_name
			) -- message
		FROM Temp_MTG_Player_Save_Player T_PLAYER
		WHERE T_PLAYER.is_duplicate
		;
	END IF;

	-- Nothing to save
	IF NOT EXISTS (SELECT * FROM Temp_MTG_Player_Save_Player LIMIT 1) THEN
		INSERT INTO Temp_MTG_Player_Save_Error (
			  error_type_id
			, message
		)
		VALUES (
			  v_warning_error_type_id
			, 'Nothing to save.'
		);
	END IF;

	/*
		INSERT INTO Temp_MTG_Player_Save_Error (
			  error_type_id
			, message
		)
		SELECT
			  v_warning_error_type_id AS error_type_id
			, CONCAT(
				  'Users: '
				, STRING_AGG(T_PLAYER.error_name, ', ')
			) AS message
		FROM Temp_MTG_Player_Save_Player T_PLAYER
		UNION
		SELECT
			  v_warning_error_type_id AS error_type_id
			, CONCAT(
				  'User relationships: '
				, STRING_AGG(T_RELATIONSHIP.error_name, ', ')
			) AS message
		FROM Temp_MTG_Player_Save_User_Relationship T_RELATIONSHIP
		;
	*/

	-- Outputs
	-- Change Set
	INSERT INTO tcg.public.TCG_Change_Set (
		  comment
		, updated_last_by_user_id
	)
	VALUES (
		  v_comment
		, v_user_id
	)
	RETURNING change_set_id INTO v_change_set_id
	;
	
	IF NOT EXISTS (SELECT * FROM Temp_MTG_Player_Save_Error T_ERROR WHERE T_ERROR.error_type_id <> v_warning_error_type_id LIMIT 1) THEN
		
		-- User relationship
		INSERT INTO tcg.public.TCG_User_Relationship (
			  follower_user_id
			, following_user_id
			, is_following
			, is_blocked
			, active
			, created_on
			, created_by_user_id
			, updated_last_on
			, updated_last_by_user_id
			, change_set_id
		)
		SELECT
			  T_RELATIONSHIP.follower_user_id
			, T_RELATIONSHIP.following_user_id
			, T_RELATIONSHIP.is_following
			, T_RELATIONSHIP.is_blocked
			, T_RELATIONSHIP.active
			, T_RELATIONSHIP.created_on
			, T_RELATIONSHIP.created_by_user_id
			, T_RELATIONSHIP.updated_last_on
			, T_RELATIONSHIP.updated_last_by_user_id
			, v_change_set_id -- change_set_id
		FROM Temp_MTG_Player_Save_User_Relationship T_RELATIONSHIP
		WHERE T_RELATIONSHIP.is_new
		;

		-- Player
		INSERT INTO tcg.public.TCG_MTG_Game_Player (
              game_id
            , user_id
            , deck_id
            , name
            , notes
            , display_order
            , active
            , created_on
            , created_by_user_id
            , updated_last_on
            , updated_last_by_user_id
            , change_set_id
		)
		SELECT
			  T_PLAYER.game_id
            , T_PLAYER.user_id
            , T_PLAYER.deck_id
            , T_PLAYER.name
            , T_PLAYER.notes
            , T_PLAYER.display_order
		    , T_PLAYER.active
            , T_PLAYER.created_on
            , T_PLAYER.created_by_user_id
            , T_PLAYER.updated_last_on
		    , T_PLAYER.updated_last_by_user_id
            , v_change_set_id -- change_set_id
		FROM Temp_MTG_Player_Save_Player T_PLAYER
        LEFT JOIN Temp_MTG_Player_Save_Game T_GAME ON T_PLAYER.game_id = T_GAME.game_id
		WHERE
				T_PLAYER.is_new = TRUE
			AND T_PLAYER.active = TRUE
        ORDER BY
              T_GAME.created_on
            , T_PLAYER.display_order
		;
	
		UPDATE tcg.public.TCG_MTG_Game_Player PLAYER
		SET
              game_id = T_PLAYER.game_id
            , user_id = T_PLAYER.user_id
            , deck_id = T_PLAYER.deck_id
            , name = T_PLAYER.name
            , notes = T_PLAYER.notes
            , display_order = T_PLAYER.display_order
		    , active = T_PLAYER.active
			, updated_last_on = v_time_start
			, updated_last_by_user_id = v_user_id
		    , change_set_id = v_change_set_id
		FROM Temp_MTG_Player_Save_Player T_PLAYER
		WHERE
				PLAYER.player_id = T_PLAYER.player_id
			AND NOT T_PLAYER.is_new
		;

	END IF;

	-- Error
	INSERT INTO tcg.public.Error (
		  guid
		, error_type_id
		, message
		, display_order
		, created_on
		, created_by_user_id
		, updated_last_on
		, updated_last_by_user_id
		, change_set_id
	)
	SELECT
		  v_guid
		, T_ERROR.error_type_id
		, T_ERROR.message
		, T_ERROR.temp_id
		, v_time_start
		, v_user_id
		, v_time_start
		, v_user_id
		, v_change_set_id
	FROM Temp_MTG_Player_Save_Error T_ERROR
	LEFT JOIN tcg.public.Error_Type ERROR_TYPE ON T_ERROR.error_type_id = ERROR_TYPE.error_type_id
	ORDER BY T_ERROR.temp_id
	;

    COMMIT;
	
	o_success := NOT EXISTS (
		SELECT *
		FROM Temp_MTG_Player_Save_Error T_ERROR
		WHERE T_ERROR.error_type_id <> v_warning_error_type_id
		LIMIT 1
	);
	
	DELETE FROM tcg.public.TCG_MTG_Game_Player_Temp PLAYER_T WHERE PLAYER_T.guid = v_guid;

	DROP TABLE IF EXISTS Temp_MTG_Player_Save_Game;
	DROP TABLE IF EXISTS Temp_MTG_Player_Save_User;
	DROP TABLE IF EXISTS Temp_MTG_Player_Save_Deck;
	DROP TABLE IF EXISTS Temp_MTG_Player_Save_Player;
	DROP TABLE IF EXISTS Temp_MTG_Player_Save_User_Relationship;
	DROP TABLE IF EXISTS Temp_MTG_Player_Save_Error;
END;
$$;


-- Call it with:
DO $$
DECLARE
	v_comment TEXT := 'MTG Game Player Save.';
	v_guid UUID;
    v_user_id INT := 3;
    v_success BOOLEAN;
BEGIN
	v_guid := gen_random_uuid();

	INSERT INTO tcg.public.TCG_MTG_Game_Player_Temp (
		  guid
        , player_id
        , game_id
        , user_id
        , deck_id
        , name
        , notes
        , display_order
        , active
	)
	VALUES
          (
              v_guid
            , NULL -- player_id
            , 4 -- game_id
            , 3 -- user_id
            , 1 -- deck_id
            , 'Teddy' -- name
            , NULL -- notes
            , 1 -- display_order
            , TRUE -- active
        )
        , (
              v_guid
            , NULL -- player_id
            , 4 -- game_id
            , 3 -- user_id
            , 2 -- deck_id
            , 'Teddy' -- name
            , NULL -- notes
            , 2 -- display_order
            , TRUE -- active
        )
        , (
              v_guid
            , NULL -- player_id
            , 4 -- game_id
            , 4 -- user_id
            , 3 -- deck_id
            , 'Teddy' -- name
            , NULL -- notes
            , 3 -- display_order
            , TRUE -- active
        )
        , (
              v_guid
            , NULL -- player_id
            , 4 -- game_id
            , 5 -- user_id
            , 4 -- deck_id
            , 'Teddy' -- name
            , NULL -- notes
            , 4 -- display_order
            , TRUE -- active
        )
        , (
              v_guid
            , NULL -- player_id
            , 4 -- game_id
            , 3 -- user_id
            , 5 -- deck_id
            , 'Teddy' -- name
            , NULL -- notes
            , 5 -- display_order
            , TRUE -- active
        )
    ;

    CALL tcg.public.USP_TCG_MTG_Game_Player_Save (
		  v_comment -- a_comment
		, v_guid -- a_guid
		, v_user_id -- a_user_id
		, v_success -- o_success
	);
    RAISE NOTICE 'Success: %', CASE WHEN v_success THEN 'TRUE' ELSE 'FALSE' END;
END;
$$;

SELECT *
-- DELETE
FROM tcg.public.Error ERROR
LEFT JOIN tcg.public.Error_Type ERROR_TYPE ON ERROR.error_type_id = ERROR_TYPE.error_type_id
;
SELECT *
FROM tcg.public.Error_Type ERROR_TYPE
;
SELECT *
FROM tcg.public.TCG_Change_Set CHANGE_SET
ORDER BY CHANGE_SET.change_set_id  DESC
;
/*
SELECT *
FROM tcg.public.TCG_MTG_Game
;
*/
SELECT *
-- DELETE
FROM tcg.public.TCG_MTG_Game_Player_Temp
;
SELECT *
-- DELETE
FROM tcg.public.TCG_MTG_Game_Player PLAYER
LEFT JOIN tcg.public.TCG_MTG_Game GAME ON PLAYER.game_id = GAME.game_id
;

SELECT *
-- DELETE
FROM tcg.public.TCG_User_Relationship_Audit RELATIONSHIP_AUDIT
;
SELECT *
-- DELETE
FROM tcg.public.TCG_User_Relationship RELATIONSHIP
;



