"""Pretty-printing for LQP IR Transactions."""
from __future__ import annotations

import re

from lqp.gen.pretty import pretty, pretty_debug

from relationalai.semantics.lqp.lqp_bundle.ir import Transaction
from relationalai.semantics.lqp.lqp_bundle.proto_conversion import _transaction_to_proto


def to_safe_string(
    txn: Transaction,
    *,
    debug: bool = False,
) -> str:
    """Pretty-print an IR Transaction to a string, with CSV filenames hidden."""
    try:
        proto = _transaction_to_proto(txn)
        result = pretty_debug(proto) if debug else pretty(proto)
        result = re.sub(
            r'"snowflake://[^"]+/export/export_[^"/]+/data"',
            '"<hidden filename>"',
            result,
        )
        return result
    except Exception as exc:
        return f"<lqp_print error: {repr(exc)}>"
