"""Register the reference ZIP release through the public API."""
import argparse
import json

import httpx

REFERENCE_VARIANTS = {
    "locate_end_of_central_directory": (
        "scan_backward", "reverse_windows", "forward_latest", "trailer_length",
        "signature_state", "faulty_short_comment",
    ),
    "parse_central_directory_entry": (
        "indexed", "bounds_first", "fixed_slice", "lengths_first", "signature_guard",
    ),
    "parse_local_file_header": (
        "indexed", "sliced", "fixed_array", "lengths_first", "signature_guard",
    ),
    "decode_filename": (
        "utf8_checked", "owned_utf8", "push_chars", "ascii_fast_path", "codepoints",
    ),
    "normalize_extraction_path": (
        "component_stack", "segment_fold", "segments", "byte_scanner", "collect_join",
        "faulty_depth_limit",
    ),
    "decompress_entry": (
        "stream_reader", "buffered_reader", "preallocated", "chunk_loop", "io_copy",
    ),
    "validate_crc": ("one_shot", "chunked", "bytewise", "fixed_blocks", "halves"),
    "extract_entry": ("direct", "staged", "file_handle", "buffered", "chunked"),
    "list_archive": (
        "central_order", "preallocated", "shrink_to_fit", "boxed_roundtrip",
        "deque_roundtrip",
    ),
}

REFERENCE_RELEASE = {
    "application": "zipdiv",
    "version": "0.1.0",
    "spec_functions": [
        {
            "name": function,
            "implementations": [
                {"id": f"{function}.{variant}", "name": variant}
                for variant in variants
            ],
        }
        for function, variants in REFERENCE_VARIANTS.items()
    ],
}

def main():
    parser=argparse.ArgumentParser()
    parser.add_argument("--url",default="http://127.0.0.1:8000")
    parser.add_argument("--version",default="0.1.0")
    args=parser.parse_args(); body={**REFERENCE_RELEASE,"version":args.version}
    with httpx.Client(base_url=args.url,timeout=30) as client:
        response=client.post("/api/v1/releases",json=body)
        if response.status_code==409:
            releases=client.get("/api/v1/releases").raise_for_status().json()
            release=next(x for x in releases if x["application"]==body["application"] and x["version"]==body["version"])
        else:
            release=response.raise_for_status().json()
    print(json.dumps({"release_id":release["id"],"application":release["application"],"version":release["version"]}))

if __name__ == "__main__":
    main()
