
CREATE OR REPLACE FUNCTION tcg.public.FN_before_insert_TCG_MTG_Game_Round_Player_Damage()
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_Round_Player_Damage()
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_Round_Player_Damage_Audit (
		  damage_id
        , name_field
        , value_prev
        , value_new
        , change_set_id
    )
	-- Changed round_id
	SELECT NEW.damage_id, 'round_id', OLD.round_id::TEXT, NEW.round_id::TEXT, NEW.change_set_id
		WHERE OLD.round_id IS NOT DISTINCT FROM NEW.round_id
	UNION
	-- Changed player_id
	SELECT NEW.damage_id, 'player_id', OLD.player_id::TEXT, NEW.player_id::TEXT, NEW.change_set_id
		WHERE OLD.player_id IS NOT DISTINCT FROM NEW.player_id
	UNION
	-- Changed received_from_commander_player_id
	SELECT NEW.damage_id, 'received_from_commander_player_id', OLD.received_from_commander_player_id::TEXT, NEW.received_from_commander_player_id::TEXT, NEW.change_set_id
		WHERE OLD.received_from_commander_player_id IS NOT DISTINCT FROM NEW.received_from_commander_player_id
	UNION
	-- Changed life_gain
	SELECT NEW.damage_id, 'life_gain', OLD.life_gain::TEXT, NEW.life_gain::TEXT, NEW.change_set_id
		WHERE OLD.life_gain IS NOT DISTINCT FROM NEW.life_gain
	UNION
	-- Changed life_loss
	SELECT NEW.damage_id, 'life_loss', OLD.life_loss::TEXT, NEW.life_loss::TEXT, NEW.change_set_id
		WHERE OLD.life_loss IS NOT DISTINCT FROM NEW.life_loss
	UNION
	-- Changed commander_deaths
	SELECT NEW.damage_id, 'commander_deaths', OLD.commander_deaths::TEXT, NEW.commander_deaths::TEXT, NEW.change_set_id
		WHERE OLD.commander_deaths IS NOT DISTINCT FROM NEW.commander_deaths
	UNION
	-- Changed is_eliminated
	SELECT NEW.damage_id, 'is_eliminated', OLD.is_eliminated::TEXT, NEW.is_eliminated::TEXT, NEW.change_set_id
		WHERE OLD.is_eliminated IS NOT DISTINCT FROM NEW.is_eliminated
	UNION
	-- Changed active
	SELECT NEW.damage_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_Round_Player_Damage
    BEFORE INSERT ON tcg.public.TCG_MTG_Game_Round_Player_Damage
    FOR EACH ROW
    EXECUTE FUNCTION tcg.public.FN_before_insert_TCG_MTG_Game_Round_Player_Damage()
;
CREATE TRIGGER TRI_before_update_TCG_MTG_Game_Round_Player_Damage
    BEFORE UPDATE ON tcg.public.TCG_MTG_Game_Round_Player_Damage
    FOR EACH ROW
    EXECUTE FUNCTION tcg.public.FN_before_update_TCG_MTG_Game_Round_Player_Damage()
;