"""Build a no-op v2 interface fixture. No API calls, payments or machine motion."""
import argparse
from pathlib import Path
import tempfile
import zipfile

import numpy as np
import onnx
from onnx import TensorProto, helper, numpy_helper
from contract import package


def build(output):
    tensor = lambda name, shape: helper.make_tensor_value_info(name, TensorProto.FLOAT, shape)
    inputs = [tensor('image', [1, 3, 120, 160]), tensor('previous_command', [1, 7]), tensor('memory', [1, 1])]
    outputs = [tensor('action_logits', [1, 6]), tensor('hold_ms', [1, 1]), tensor('next_memory', [1, 1])]
    nodes = [
        helper.make_node('Constant', [], ['action_logits'], value=numpy_helper.from_array(np.array([[1, 0, 0, 0, 0, 0]], np.float32))),
        helper.make_node('Constant', [], ['hold_ms'], value=numpy_helper.from_array(np.zeros((1, 1), np.float32))),
        helper.make_node('Identity', ['memory'], ['next_memory']),
    ]
    model = helper.make_model(helper.make_graph(nodes, 'noop-v2', inputs, outputs),
                              opset_imports=[helper.make_opsetid('', 17)], ir_version=9)
    onnx.checker.check_model(model, full_check=True)
    with tempfile.TemporaryDirectory() as directory:
        path = Path(directory) / 'policy.onnx'
        onnx.save(model, path)
        archive = Path(directory) / 'model.zip'
        package(path, archive, version=2, memory_elements=1)
        # Fixed ZIP metadata makes repeated builds byte-identical.
        with zipfile.ZipFile(archive) as source, zipfile.ZipFile(output, 'w') as destination:
            for name in ('manifest.json', 'policy.onnx'):
                info = zipfile.ZipInfo(name, date_time=(2026, 10, 4, 0, 0, 0))
                info.compress_type = zipfile.ZIP_DEFLATED
                destination.writestr(info, source.read(name))


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument('--out', default='noop-v2.zip')
    args = parser.parse_args()
    build(args.out)
    print(f'Created {args.out}. This fixture never moves or drops. Do not buy a run expecting a win.')
