#!/usr/bin/env python3
"""Local syntax and byte-integrity inventory, not a Wezy manifest/restore validator.
Usage: python jsm-jsonl-inventory.py batch-a.jsonl batch-b.jsonl
Reads only the specified files; prints filenames/counts/hashes, never record contents.
No network, modifications, upload, restore or assertion that all batches were received.
"""
import argparse
import hashlib
import json
import sys
from pathlib import Path


def reject_non_json_constant(value):
    raise ValueError('Non-standard JSON numeric constant')


def inventory(path):
    path = Path(path)
    digest, records, size = hashlib.sha256(), 0, 0
    with path.open('rb') as source:
        for number, raw in enumerate(source, 1):
            digest.update(raw)
            size += len(raw)
            if not raw.strip():
                raise ValueError(f'{path.name}: blank JSONL line {number}')
            try:
                json.loads(raw.decode('utf-8'), parse_constant=reject_non_json_constant)
            except (ValueError, UnicodeError):
                raise ValueError(f'{path.name}: invalid UTF-8/JSON on line {number}') from None
            records += 1
    if records == 0:
        raise ValueError(f'{path.name}: empty batch file')
    return {'filename': path.name, 'bytes': size, 'jsonl_records': records, 'sha256': digest.hexdigest()}


def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument('files', nargs='+', type=Path)
    args = parser.parse_args()
    try:
        files = [inventory(path) for path in args.files]
    except (OSError, ValueError) as exc:
        print(f'Inventory failed: {exc}', file=sys.stderr)
        return 2
    repeated = sorted({row['sha256'] for row in files if sum(x['sha256'] == row['sha256'] for x in files) > 1})
    print(json.dumps({'files': files, 'repeated_digests_to_review': repeated,
                     'complete_recovery': None,
                     'limits': 'JSON syntax and hashes only. Compare native batch sequence, terminal state, manifest and omissions separately. No schema validation, export authenticity or restore guarantee.'}, indent=2))
    return 0

if __name__ == '__main__':
    raise SystemExit(main())
