"""Exploratory image-only translation checks; no registration is automatically applied."""
import numpy as np
try:
 from .phase_alignment import phase_displacement,accepted_shift
 from .phase_alignment_checked import checked_alignment
except ImportError:
 from phase_alignment import phase_displacement,accepted_shift
 from phase_alignment_checked import checked_alignment

def _centered(reference,target):
 if np.ptp(reference)==0 or np.ptp(target)==0:return None
 a=reference.astype('float32');b=target.astype('float32');a-=a.mean();b-=b.mean();return phase_displacement(a,b)

def compare_registration(reference,target,local_limit=3.,cycle_limit=1.):
 """Return raw, centered, and local/cycle guarded tentative estimates.

 Local and cycle thresholds are predeclared heuristics, not calibrated error
 bounds. Periodic patterns may satisfy every check despite ambiguous motion.
 """
 raw=checked_alignment(reference,target);forward=_centered(reference,target)
 if forward is None:return {'raw':raw,'centered':{'status':'unidentifiable_constant','estimate':None},'strict':{'status':'unidentifiable_constant','estimate':None,'local':[],'cycle_error':None,'max_local_disagreement':None}}
 centered_ok=accepted_shift(forward) is not None;back=_centered(target,reference);cycle=float(np.hypot(forward['dx']+back['dx'],forward['dy']+back['dy'])) if back else None;h,w=reference.shape;local=[]
 for y0,y1,x0,x1 in [(0,h//2,0,w//2),(0,h//2,w//2,w),(h//2,h,0,w//2),(h//2,h,w//2,w)]:
  e=_centered(reference[y0:y1,x0:x1],target[y0:y1,x0:x1]);local.append(e)
 usable=all(e is not None and accepted_shift(e) is not None for e in local)
 maxdiff=max((float(np.hypot(e['dx']-forward['dx'],e['dy']-forward['dy'])) for e in local if e is not None),default=None)
 strict_ok=centered_ok and usable and cycle is not None and cycle<=cycle_limit and maxdiff is not None and maxdiff<=local_limit
 return {'raw':raw,'centered':{'status':'tentative' if centered_ok else 'heuristic_reject','estimate':forward},'strict':{'status':'tentative' if strict_ok else 'heuristic_reject','estimate':forward,'local':local,'cycle_error':cycle,'max_local_disagreement':maxdiff}}
