#!/usr/bin/env python3
"""Strict B/C catalog contract comparison; output contains no response values."""
import argparse
import importlib.util
import json
import re
from pathlib import Path

parser = argparse.ArgumentParser()
parser.add_argument('b_responses')
parser.add_argument('c_responses')
args = parser.parse_args()
module_path = Path(__file__).resolve().parent.parent / 'http-harness/strict_response_compare.py'
spec = importlib.util.spec_from_file_location('strict_response_compare', module_path)
strict = importlib.util.module_from_spec(spec)
spec.loader.exec_module(strict)
b, c = strict.load_json(args.b_responses), strict.load_json(args.c_responses)
if set(b) != set(c):
    raise SystemExit('Scenario sets differ')
expected = {f'PILOT_AI_ENUM_{i}' for i in range(1, 18)} | {
    'PILOT_AI_SPORT_ALL_ALL', 'PILOT_AI_SPORT_ALL_1', 'PILOT_AI_SPORT_ALL_2',
    'PILOT_AI_SPORT_CATE', 'PILOT_AI_SPORT_GROUPED'}
if set(b) != expected:
    raise SystemExit('Required 22 scenario set differs')
rule = {'$["trace_id"]': {'type': 'string', 'pattern': '[0-9a-f]{13}',
    'reason': 'think.App::initialize uses uniqid() per request; validated exact 13-hex shape',
    'require_distinct': True}}
cases = []
for key in sorted(b):
    for side in (b[key], c[key]):
        if side['http_status'] != 200 or side['body'].get('status') != 200:
            raise SystemExit('Expected positive HTTP/business status: ' + key)
        trace = side['body'].get('trace_id')
        if not isinstance(trace, str) or re.fullmatch('[0-9a-f]{13}', trace) is None:
            raise SystemExit('Missing or invalid trace_id: ' + key)
    differences = []
    if b[key]['http_status'] != c[key]['http_status']:
        differences.append(('HTTP_STATUS', 'number', 'number'))
    differences.extend((path, left, right) for path, left, right, _, _ in strict.compare(
        b[key]['body'], c[key]['body'], rules=rule))
    cases.append({'scenario': key, 'strict_differences': len(differences),
        'differences': [{'path': p, 'mysql_type': l, 'dm_type': r} for p, l, r in differences]})
print(json.dumps({'cases': cases, 'case_count': len(cases),
    'strict_differences': sum(case['strict_differences'] for case in cases),
    'normalization': 'validated distinct top-level trace_id only; no array sorting or numeric/string coercion',
    'acceptance': 'these read scenarios only; not full-interface or whole-system acceptance'}, indent=2))
raise SystemExit(any(case['strict_differences'] for case in cases))
