
CREATE OR REPLACE PROCEDURE tcg.public.USP_TCG_MTG_Game_Save (
      a_comment TEXT
    , a_end_on TIMESTAMP
	, a_game_id INT
    , a_guid UUID
    , a_location_name TEXT
    , a_notes TEXT
    , a_starting_life INT
    , a_start_on TIMESTAMP
    , a_user_id INT
    , a_do_delete BOOLEAN
    , a_is_commander BOOLEAN
    , a_is_draft BOOLEAN
    , a_is_sealed BOOLEAN
	, OUT o_game_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_do_delete BOOLEAN;
    v_end_on TIMESTAMP;
	v_game_id INT;
    v_guid UUID;
    v_is_commander BOOLEAN;
    v_is_draft BOOLEAN;
    v_is_new BOOLEAN;
    v_is_sealed BOOLEAN;
    v_location_name TEXT;
    v_notes TEXT;
    v_starting_life INT;
    v_start_on TIMESTAMP;
	v_success BOOLEAN;
	v_time_start TIMESTAMP;
    v_user_id INT;
	v_warning_error_type_id INT;
BEGIN
    v_comment := TRIM(COALESCE(a_comment, ''));
    v_end_on := a_end_on;
	v_game_id := CASE WHEN COALESCE(a_game_id, 0) > 0 THEN a_game_id ELSE NULL END;
    v_guid := a_guid;
    v_location_name := TRIM(COALESCE(a_location_name, ''));
    v_notes := TRIM(COALESCE(a_notes, ''));
	v_starting_life := CASE WHEN COALESCE(a_starting_life, 0) > 0 THEN a_starting_life ELSE 40 END;
    v_start_on := a_start_on;
    v_user_id := CASE WHEN COALESCE(a_user_id, 0) > 0 THEN a_user_id ELSE NULL END;
    v_do_delete := COALESCE(a_do_delete, FALSE);
    v_is_commander := COALESCE(a_is_commander, TRUE);
    v_is_draft := COALESCE(a_is_draft, FALSE);
    v_is_sealed := COALESCE(a_is_sealed, FALSE);

	-- 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;
	
    v_is_new := CASE WHEN COALESCE(v_game_id, 0) < 1 THEN TRUE ELSE FALSE END;
	
	-- 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
	;
	
	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
	)
	VALUES (
		  v_guid
		, v_warning_error_type_id
		, CONCAT(
			'Is New: '
			, v_is_new::TEXT
			, ', Game: '
			, v_game_id::TEXT
		)
		, 1 -- display_order
		, v_time_start -- created_on
		, v_user_id -- created_by_user_id
		, v_time_start -- updated_last_on
		, v_user_id -- updated_last_by_user_id
		, v_change_set_id -- change_set_id
	);
	
	IF v_is_new THEN
		INSERT INTO tcg.public.TCG_MTG_Game (
			  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
		)
		VALUES (
			  v_notes -- notes
		    , v_is_commander -- is_commander
		    , v_is_draft -- is_draft
		    , v_is_sealed -- is_sealed
		    , v_location_name -- location_name
            , v_start_on -- start_on
            , v_end_on -- end_on
            , v_starting_life -- starting_life
            , TRUE -- active
			, v_time_start -- created_on
			, v_user_id -- created_by_user_id
			, v_time_start -- updated_last_on
			, v_user_id -- updated_last_by_user_id
		    , v_change_set_id -- change_set_id
        )
	    RETURNING game_id INTO v_game_id
		;

		INSERT INTO tcg.public.TCG_MTG_Game_Round (
			  game_id
		    , notes
		    , display_order
		    , active
			, created_on
			, created_by_user_id
			, updated_last_on
			, updated_last_by_user_id
		    , change_set_id
		)
		VALUES (
			  v_game_id -- game_id
            , v_notes -- notes
		    , 1 -- display_order
            , TRUE -- active
			, v_time_start -- created_on
			, v_user_id -- created_by_user_id
			, v_time_start -- updated_last_on
			, v_user_id -- updated_last_by_user_id
		    , v_change_set_id -- change_set_id
        )
		;
    ELSE
		UPDATE tcg.public.TCG_MTG_Game GAME
		SET
			  notes = v_notes
		    , is_commander = v_is_commander
		    , is_draft = v_is_draft
		    , is_sealed = v_is_sealed
		    , location_name = v_location_name
		    , start_on = v_start_on
		    , end_on = v_end_on
            , starting_life = v_starting_life
		    , active = NOT v_do_delete
			, updated_last_on = v_time_start
			, updated_last_by_user_id = v_user_id
		    , change_set_id = v_change_set_id
		WHERE GAME.game_id = v_game_id
		;
	END IF;

    COMMIT;
	
    o_game_id := v_game_id;
	o_success := TRUE;
END;
$$;


-- Call it with:
DO $$
DECLARE
	v_comment TEXT := 'Edit Commander Game!';
    v_end_on TIMESTAMP;
	v_game_id_in INT := 7;
    v_guid UUID;
    v_location_name TEXT := 'Egg shell home';
    v_notes TEXT := 'Juice in the box';
    v_starting_life INT := 69;
    v_start_on TIMESTAMP := '2026-02-05 11:45:00'::TIMESTAMP;
    v_user_id INT := 3;
    v_do_delete BOOLEAN := FALSE;
    v_is_commander BOOLEAN := TRUE;
    v_is_draft BOOLEAN := FALSE;
    v_is_sealed BOOLEAN := FALSE;
	v_game_id_out INT;
	v_success_out BOOLEAN;
BEGIN
	v_guid := gen_random_uuid();

    CALL tcg.public.USP_TCG_MTG_Game_Save (
		  a_comment := v_comment -- a_comment
        , a_end_on := v_end_on -- a_end_on
		, a_game_id := v_game_id_in -- a_game_id
		, a_guid := v_guid -- a_guid
        , a_location_name := v_location_name -- a_location_name
        , a_notes := v_notes -- a_notes
        , a_starting_life := v_starting_life -- a_starting_life
        , a_start_on := v_start_on -- a_start_on
		, a_user_id := v_user_id -- a_user_id
        , a_do_delete := v_do_delete -- a_do_delete
        , a_is_commander := v_is_commander -- a_is_commander
        , a_is_draft := v_is_draft -- a_is_draft
        , a_is_sealed := v_is_sealed -- a_is_sealed
		, o_game_id := v_game_id_out -- o_game_id
		, o_success := v_success_out -- o_success
	);
    RAISE NOTICE
		  'Game: %, Success: %, GUID: %'
		, CAST(v_game_id_out AS VARCHAR)
		, CAST(v_success_out AS VARCHAR)
		, CAST(v_guid AS VARCHAR)
	;
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 GAME
ORDER BY GAME.game_id DESC
;

/*
SELECT COUNT(*)
from tcg.public.TCG_MTG_Inventory I
;
SELECT
--	COUNT(*)
	*
from tcg.public.TCG_MTG_Inventory_Temp I_T
;
*/
