"""Rebuild the worked synthetic packet from retained source-bound inputs."""

import hashlib
import json
from pathlib import Path

PROJECT = Path(__file__).resolve().parents[2]
HERE = Path(__file__).resolve().parent


def binding(path):
    return {'path': str(path.relative_to(PROJECT)), 'sha256': hashlib.sha256(path.read_bytes()).hexdigest()}


def write(name, value):
    path = HERE / name
    path.write_text(json.dumps(value, indent=2) + '\n')
    return path


def main():
    recording = binding(HERE / 'input/recording.mp4')
    worksheet = write('timing-private.json', {'schema_version': 1, 'origin': 'illustrative-not-measured', 'units': 'seconds',
        'values': {'C': 2, 'H': 35, 'T': 30, 'E': 33, 'L': 28.2, 'P': 33, 'B': 2, 'R1': .55, 'R2': .65, 'R3': .75},
        'meeting_verified': False})
    tail = write('tail-observations.json', {'source_sha256': recording['sha256'], 'evidence_kind': 'visual-inspection',
        'start_seconds': 30, 'end_seconds': 33, 'wave_seconds': [28.7, 30],
        'observations': ['Synthetic hand motion stops at 30 s; calm pose remains through 32.967 s.',
                         'The generated figure leaves the pose from 33 s; file continues until 35 s.',
                         'Low-level generated room noise continues through the calm hold; this is not claimed to be silent.'],
        'scope': 'Synthetic construction and decoded frame inspection, not a human performance or real rehearsal.'})
    proof = write('capture-observations.json', {'scope': 'Actual runtime of original synthetic fixture, not Poe',
        'object': 'Ridge loop', 'actual': '9 stops and 2 closures; same route retained',
        'capture_clock': 'Encoded native capture PTS, relative to first captured frame',
        'observations': [{'source_seconds': .6, 'state': 'Loaded app, no summary'},
                         {'source_seconds': 2, 'state': 'Ridge loop selected'},
                         {'source_seconds': 2.8, 'state': 'Build accepted; working state'},
                         {'source_seconds': 10.4, 'state': 'Still working'},
                         {'source_seconds': 12, 'state': 'Settled result: 9 stops, 2 closures'}],
        'useful_pixels': [1920, 1080], 'maximum_camera_zoom': 1.4,
        'wall_clock_policy': 'Actions evidence is approximate wall time, not a substitute for inspected encoded PTS.'})
    motion = {'kind': 'hold', 'actionFrame': 15, 'changeFrame': 45, 'zoom': 1, 'focusX': .5, 'focusY': .5, 'evidenceKind': 'proposed-edit'}
    first = {'id': 'build-and-check', 'title': 'Nine stops. Two closures.', 'recordingStartSeconds': 7,
        'recordingEndSeconds': 18, 'recordingProofSeconds': 14, 'proofLocalFrame': 180,
        'durationInFrames': 270, 'minimumReadingFrames': 90, 'captureId': 'moss', 'objectId': 'ridge-loop',
        'reference': {'studyUrl': 'https://openai-film-fieldnotes-100.pages.dev/videos/films/codex-first-look/',
            'sourceIntervalSeconds': [121, 130], 'evidenceKind': 'source-observation',
            'rationale': 'Show actual action, completion and an unchanging reading interval. Native app motion is enough here.',
            'alternative': 'A camera push during result arrival would compete with the result; keep the camera still.'},
        'motion': motion, 'segments': [{'fromFrame': 0, 'endFrame': 90, 'sourceStartSeconds': .6, 'mode': 'play'},
            {'fromFrame': 90, 'endFrame': 180, 'sourceStartSeconds': 9, 'mode': 'play'},
            {'fromFrame': 180, 'endFrame': 270, 'sourceStartSeconds': 12, 'mode': 'hold'}],
        'omissions': [{'afterLocalFrame': 90, 'sourceStartSeconds': 3.6, 'sourceEndSeconds': 9,
            'reason': 'The actual wait cannot fit the fixed narration. Preserve initiation and actual arrival; omit only the middle wait.',
            'disclosure': '5.4 s wait omitted'}]}
    second = {'id': 'same-route-detail', 'title': 'Keep the same route in view.', 'recordingStartSeconds': 21,
        'recordingEndSeconds': 27, 'recordingProofSeconds': 23, 'proofLocalFrame': 60,
        'durationInFrames': 240, 'minimumReadingFrames': 150, 'captureId': 'moss', 'objectId': 'ridge-loop',
        'reference': {'studyUrl': 'https://openai-film-fieldnotes-100.pages.dev/videos/motion/#codex-review-reframe',
            'sourceIntervalSeconds': [92.5, 98], 'evidenceKind': 'source-observation',
            'rationale': 'The wide proof is already established. One push brings the same route and counts closer, then stops.',
            'alternative': 'Atlas 72–76 s supplies a contrasting hard reframe. A hard cut is valid, but the gentle push makes retained identity easier to follow here.'},
        'motion': {**motion, 'kind': 'push', 'zoom': 1.4, 'focusX': .6, 'focusY': .51},
        'segments': [{'fromFrame': 0, 'endFrame': 240, 'sourceStartSeconds': 12, 'mode': 'hold'}], 'omissions': []}
    packet = {'schemaVersion': 1, 'mode': 'synthetic-study', 'recording': recording,
        'intake': binding(HERE / 'intake/intake.json'), 'transcript': binding(HERE / 'transcript.json'),
        'timing': {'worksheet': binding(worksheet), 'nominalReactionSeconds': .65, 'evidenceKind': 'synthetic-fixture',
            'tailObservation': binding(tail), 'cueDescription': 'At 2.000 s, the original title cuts to the loaded Moss Ledger application.'},
        'identity': {'title': 'Make the result easy to check.', 'label': 'MOSS LEDGER / SYNTHETIC STUDY', 'accent': '#3d6646'},
        'exportProfile': {'width': 1280, 'height': 720, 'fps': 30, 'container': 'mp4', 'codec': 'h264', 'pixelFormat': 'yuv420p', 'color': 'bt709', 'audio': False},
        'approval': {'state': 'synthetic-only'},
        'captures': [{'id': 'moss', 'manifest': binding(PROJECT / 'public/transfer/moss/capture.json'),
            'objectId': 'ridge-loop', 'settledSourceSeconds': 12,
            'verification': {'status': 'verified', 'kind': 'synthetic-fixture', 'runtime': 'Local examples/transfer/demo.html',
                'version': hashlib.sha256((HERE / 'demo.html').read_bytes()).hexdigest(),
                'permission': 'Original synthetic local test only; no external account or service',
                'instructionsRead': 'PRODUCTION_PACKET.md and the self-contained fixture source',
                'expected': 'Ridge loop shows 9 stops and 2 closures after a genuine wait',
                'actual': 'Ridge loop shows 9 stops and 2 closures after a genuine wait', 'evidence': binding(proof)}}],
        'beats': [first, second]}
    write('packet.json', packet)


if __name__ == '__main__':
    main()
