
CREATE OR REPLACE FUNCTION tcg.public.FN_before_insert_TCG_MTG_Game()
RETURNS TRIGGER AS $$
DECLARE
    r_change_set RECORD;
BEGIN
    NEW.created_on = COALESCE(NEW.created_on, CURRENT_TIMESTAMP);
    NEW.updated_last_on = COALESCE(NEW.updated_last_on, CURRENT_TIMESTAMP);

    IF NEW.change_set_id IS NULL THEN
		RAISE EXCEPTION 'Change Set ID must be provided.';
    END IF;

    SELECT *
    INTO r_change_set
    FROM tcg.public.TCG_Change_Set CHANGE_SET
    WHERE NEW.change_set_id = CHANGE_SET.change_set_id
    ;

    IF FOUND THEN
        NEW.created_by_user_id := COALESCE(NEW.created_by_user_id, r_change_set.updated_last_by_user_id);
        NEW.updated_last_by_user_id := COALESCE(NEW.updated_last_by_user_id, r_change_set.updated_last_by_user_id);
        NEW.created_on := COALESCE(NEW.created_on, r_change_set.updated_last_on);
    ELSE
        RAISE EXCEPTION 'Change Set % not found.', NEW.change_set_id;
    END IF;
    
    RETURN NEW;
END;
$$ LANGUAGE plpgsql
;

CREATE OR REPLACE FUNCTION tcg.public.FN_before_update_TCG_MTG_Game()
RETURNS TRIGGER AS $$
BEGIN
    NEW.updated_last_on = CURRENT_TIMESTAMP;

    IF OLD.change_set_id IS NOT DISTINCT FROM NEW.change_set_id THEN
		RAISE EXCEPTION 'New Change Set ID must be provided.';
    END IF;

    INSERT INTO tcg.public.TCG_MTG_Game_Audit (
		  game_id
        , name_field
        , value_prev
        , value_new
        , change_set_id
    )
	-- Changed notes
	SELECT NEW.game_id, 'notes', OLD.notes::TEXT, NEW.notes::TEXT, NEW.change_set_id
		WHERE OLD.notes IS NOT DISTINCT FROM NEW.notes
	UNION
	-- Changed is_commander
	SELECT NEW.game_id, 'is_commander', OLD.is_commander::TEXT, NEW.is_commander::TEXT, NEW.change_set_id
		WHERE OLD.is_commander IS NOT DISTINCT FROM NEW.is_commander
	UNION
	-- Changed is_draft
	SELECT NEW.game_id, 'is_draft', OLD.is_draft::TEXT, NEW.is_draft::TEXT, NEW.change_set_id
		WHERE OLD.is_draft IS NOT DISTINCT FROM NEW.is_draft
	UNION
	-- Changed is_sealed
	SELECT NEW.game_id, 'is_sealed', OLD.is_sealed::TEXT, NEW.is_sealed::TEXT, NEW.change_set_id
		WHERE OLD.is_sealed IS NOT DISTINCT FROM NEW.is_sealed
	UNION
	-- Changed location_name
	SELECT NEW.game_id, 'location_name', OLD.location_name, NEW.location_name, NEW.change_set_id
		WHERE OLD.location_name IS NOT DISTINCT FROM NEW.location_name
	UNION
	-- Changed start_on
	SELECT NEW.game_id, 'start_on', OLD.start_on::TEXT, NEW.start_on::TEXT, NEW.change_set_id
		WHERE OLD.start_on IS NOT DISTINCT FROM NEW.start_on
	UNION
	-- Changed end_on
	SELECT NEW.game_id, 'end_on', OLD.end_on::TEXT, NEW.end_on::TEXT, NEW.change_set_id
		WHERE OLD.end_on IS NOT DISTINCT FROM NEW.end_on
	UNION
	-- Changed starting_life
	SELECT NEW.game_id, 'starting_life', OLD.starting_life::TEXT, NEW.starting_life::TEXT, NEW.change_set_id
		WHERE OLD.starting_life IS NOT DISTINCT FROM NEW.starting_life
	UNION
	-- Changed active
	SELECT NEW.game_id, 'active', OLD.active::TEXT, NEW.active::TEXT, NEW.change_set_id
		WHERE OLD.active IS NOT DISTINCT FROM NEW.active
    ;
    
    RETURN NEW;
END;
$$ LANGUAGE plpgsql
;

CREATE TRIGGER TRI_before_insert_TCG_MTG_Game
    BEFORE INSERT ON tcg.public.TCG_MTG_Game
    FOR EACH ROW
    EXECUTE FUNCTION tcg.public.FN_before_insert_TCG_MTG_Game()
;
CREATE TRIGGER TRI_before_update_TCG_MTG_Game
    BEFORE UPDATE ON tcg.public.TCG_MTG_Game
    FOR EACH ROW
    EXECUTE FUNCTION tcg.public.FN_before_update_TCG_MTG_Game()
;