from __future__ import annotations

from app.agents.schemas.learning import LearningDecision, LearningSignal


def route_learning_signal(signal: LearningSignal) -> str:
    if signal.signal_type == "preference":
        return "memory"
    if signal.signal_type in {"workflow", "decomposition", "tool_success"}:
        return "skill"
    if signal.signal_type == "correction":
        return "audit"
    return "memory"


def build_learning_bridge_summary(signals: list[LearningSignal]) -> dict[str, object]:
    memory_count = 0
    skill_count = 0
    audit_count = 0

    for signal in signals:
        route = route_learning_signal(signal)
        if route == "memory":
            memory_count += 1
        elif route == "skill":
            skill_count += 1
        else:
            audit_count += 1

    return {
        "memory_signal_count": memory_count,
        "skill_signal_count": skill_count,
        "audit_signal_count": audit_count,
    }


def update_learning_decision_with_bridge(
    decision: LearningDecision,
    signals: list[LearningSignal],
) -> LearningDecision:
    bridge_summary = build_learning_bridge_summary(signals)
    metadata = dict(decision.metadata or {})
    metadata["bridge"] = bridge_summary
    decision.metadata = metadata
    return decision
