
CREATE OR REPLACE FUNCTION tcg.public.FN_before_insert_TCG_Statistic()
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_Statistic()
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_Statistic_Audit (
		  statistic_id
        , name_field
        , value_prev
        , value_new
        , change_set_id
    )
	-- Changed entity_type_code
	SELECT NEW.statistic_id, 'entity_type_code', OLD.entity_type_code, NEW.entity_type_code, NEW.change_set_id
		WHERE OLD.entity_type_code IS NOT DISTINCT FROM NEW.entity_type_code
	UNION
	-- Changed entity_record_id
	SELECT NEW.statistic_id, 'entity_record_id', OLD.entity_record_id::TEXT, NEW.entity_record_id::TEXT, NEW.change_set_id
		WHERE OLD.entity_record_id IS NOT DISTINCT FROM NEW.entity_record_id
	UNION
	-- Changed name
	SELECT NEW.statistic_id, 'name', OLD.name, NEW.name, NEW.change_set_id
		WHERE OLD.name IS NOT DISTINCT FROM NEW.name
	UNION
	-- Changed value_bool
	SELECT NEW.statistic_id, 'value_bool', OLD.value_bool::TEXT, NEW.value_bool::TEXT, NEW.change_set_id
		WHERE OLD.value_bool IS NOT DISTINCT FROM NEW.value_bool
	UNION
	-- Changed value_float
	SELECT NEW.statistic_id, 'value_float', OLD.value_float::TEXT, NEW.value_float::TEXT, NEW.change_set_id
		WHERE OLD.value_float IS NOT DISTINCT FROM NEW.value_float
	UNION
	-- Changed value_text
	SELECT NEW.statistic_id, 'value_text', OLD.value_text, NEW.value_text, NEW.change_set_id
		WHERE OLD.value_text IS NOT DISTINCT FROM NEW.value_text
	UNION
	-- Changed value_timestamp
	SELECT NEW.statistic_id, 'value_timestamp', OLD.value_timestamp::TEXT, NEW.value_timestamp::TEXT, NEW.change_set_id
		WHERE OLD.value_timestamp IS NOT DISTINCT FROM NEW.value_timestamp
	UNION
	-- Changed active
	SELECT NEW.statistic_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_Statistic
    BEFORE INSERT ON tcg.public.TCG_Statistic
    FOR EACH ROW
    EXECUTE FUNCTION tcg.public.FN_before_insert_TCG_Statistic()
;
CREATE TRIGGER TRI_before_update_TCG_Statistic
    BEFORE UPDATE ON tcg.public.TCG_Statistic
    FOR EACH ROW
    EXECUTE FUNCTION tcg.public.FN_before_update_TCG_Statistic()
;