from pathlib import Path

from neurotron.logbus import logbus
import neurotron
from neurotron.trm.agentes.programador import ProgramadorV01


# neurotron/trm/agentes/programador/agent.py

from pathlib import Path
from neurotron.logbus import logbus

class ProgramadorAgent:
    name = "trm.programador"

    def __init__(self, ctx):
        self.ctx = ctx

        # "ROM"/source tree dentro do initramfs/rootfs
        # ajusta se o teu layout mudar
        self.rom_examples_dir = Path("/opt/kernel/neurotron/src/neurotron/lang/examples")

    def step(self, state, tele):
        # destino certo: runtime/lang/examples
        target = self.ctx.lang_examples_dir / "fib.nl"

        if target.exists():
            return state

        source = self.rom_examples_dir / "fib.nl"
        if not source.exists():
            logbus.debug(f"[trm.programador] ROM fib.nl não existe em {source}")
            return state

        try:
            self.ctx.lang_examples_dir.mkdir(parents=True, exist_ok=True)
            target.write_text(source.read_text(encoding="utf-8"), encoding="utf-8")
            logbus.debug("[trm.programador] fib.nl copiado ROM → runtime/examples")

            if hasattr(self.ctx, "memory"):
                self.ctx.memory.remember(
                    "trm.programador.action",
                    {"action": "sync_example", "file": "fib.nl", "from": str(source), "to": str(target)},
                )
        except Exception as e:
            logbus.debug(f"[trm.programador] erro ao copiar fib.nl: {e}")

        return state
