"""Part-local sampling grid mapped back to the unchanged L01 input timeline."""
import math
from sampling import make_plan

def make_part_plan(pts, bounds, part_id):
    lo, hi = bounds
    if not pts or not 1 <= lo <= hi <= len(pts):
        raise ValueError('Invalid part bounds')
    if any(not math.isfinite(x) for x in pts) or any(b <= a for a,b in zip(pts,pts[1:])):
        raise ValueError('Invalid full-input PTS')
    subset = pts[lo-1:hi]
    plan = make_plan(subset, None)
    origin = pts[0]
    offset = subset[0] - origin
    plan.update(part_id=part_id, source_frames=len(pts), part_source_frames=len(subset),
                source_pts_origin=origin, part_origin_seconds=offset,
                display_bounds=[lo,hi], tracking_start='part start; new independent ByteTracker',
                input_context='this part only; no cross-gap observations')
    plan['source_frame_indices'] = [n + lo - 1 for n in plan['source_frame_indices']]
    plan['target_seconds'] = [t + offset for t in plan['target_seconds']]
    plan['relative_seconds'] = [t + offset for t in plan['relative_seconds']]
    assert all(lo <= n <= hi for n in plan['source_frame_indices'])
    assert len(set(plan['source_frame_indices'])) == len(plan['source_frame_indices'])
    return plan
