"""Generate a synthetic CSV with a single matching record at the end."""
import argparse
from pathlib import Path

parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("output", type=Path)
parser.add_argument("--mib", type=int, choices=(512, 4096), default=512)
parser.add_argument("--line-ending", choices=("crlf", "lf"), default="crlf")
args = parser.parse_args()
ending = b"\r\n" if args.line_ending == "crlf" else b"\n"
header = b"id,created_at,status,amount,contact" + ending


def record(identifier):
    fields = f"{identifier},2026-08-01T12:00:00Z,ready,19.95,operator@example.test"
    return fields.ljust(128 - len(ending), ".").encode("ascii") + ending


ordinary = record("000001")
marker = record("LAST42")
block = ordinary * 8192
# Leave room for the header at the viewer's 4 GiB input ceiling.
rows_per_block = len(block) // len(ordinary)
last_block_rows = rows_per_block - int(args.mib == 4096)
data_rows = (args.mib - 1) * rows_per_block + last_block_rows
with args.output.open("xb") as output:
    output.write(header)
    for _ in range(args.mib - 1):
        output.write(block)
    output.write(ordinary * (last_block_rows - 1))
    output.write(marker)
print(f"{data_rows:,} data rows, {args.output.stat().st_size:,} bytes")
