#--------------------------------------------------
# Compiler
#--------------------------------------------------

from relationalai import debugging
from relationalai.early_access.lqp import ir as lqp, llqp
from relationalai.early_access.lqp.passes import lqp_passes
from relationalai.early_access.lqp.model2lqp import to_lqp as ir_to_lqp
from relationalai.early_access.metamodel import ir, compiler as c

class Compiler(c.Compiler):
    def __init__(self):
        super().__init__(lqp_passes())

    def do_compile(self, model: ir.Model, options:dict={}) -> str:
        lqp_ir = ir_to_lqp(model)
        self._lqp_result = lqp_ir
        llqp_config = options.get("llqp_config", {})
        lqp_str = llqp.program_to_llqp(lqp_ir, llqp_config)

        with debugging.span("passes") as span:
            with debugging.span("LQP Emitter") as span:
                span["metamodel"] = llqp.program_to_llqp(lqp_ir, llqp.ugly_config)

        return lqp_str

    def get_lqp_ir(self) -> lqp.LqpProgram:
        """ Returns the LQP IR generated by the compiler. """
        assert self._lqp_result is not None, "LQP IR not generated yet"
        return self._lqp_result
