
from debug import trace as grownet_trace


class Synapse:
    def __init__(self, source, target, feedback=False):
        self.source = source
        self.target = target
        self.feedback = bool(feedback)
        self.strength = 0.0
        self.last_seen_tick = 0

    # snake_case accessors only (no camelCase)
    def get_strength_value(self):
        return self.strength

    def set_strength_value(self, v):
        self.strength = float(v)

    def get_last_seen_tick(self):
        return self.last_seen_tick

    def set_last_seen_tick(self, t):
        self.last_seen_tick = int(t)

    def deliver(self, value):
        """Deliver a value to the target once and report whether it fired."""
        current_step = 0
        try:
            source_bus = self.source.get_bus() if hasattr(self.source, "get_bus") else None
            if source_bus is not None and hasattr(source_bus, "get_current_step"):
                current_step = int(source_bus.get_current_step())
            elif source_bus is not None and hasattr(source_bus, "get_step"):
                current_step = int(source_bus.get_step())
        except Exception:
            current_step = 0

        self.strength = float(self.strength) + 0.02
        self.last_seen_tick = current_step

        try:
            source_layer = getattr(self.source, "owner", None)
            region_obj = getattr(source_layer, "region", None)
            if region_obj is not None and hasattr(region_obj, "_record_delivery_event"):
                region_obj._record_delivery_event()
        except Exception:
            pass

        with grownet_trace.run_with_hop_increment():
            return bool(self.target.on_input(value))
