"""Declared refinements scope, independently of the preceding transfer scope.

Bounds follow named assembly reservations and native installer sites. Never
create an allowlist from the candidate's differences. Native behavior, inputs,
screenshots and ABI checks remain separate mandatory release requirements.
"""
import hashlib
from menu_ui import HOOKS as MENU_HOOKS

BASE_SHA = '57bfd4e0826286e144643fe0ea12d35953d3b085c067039ad9a6eb23f781f7eb'
MODULES = {
    'Alt Goalies default and native menu return': (0x221000, 0x221800),
    'visible settings row mapper': (0x222000, 0x222500),
    'AI Shot Plan default and menu return': (0x223000, 0x223800),
    'permanent painted faceoff dots': (0x224000, 0x224200),
    'friendly native-interception buffer admission': (0x225800, 0x226000),
    'lower-left shot HUD and displayed units': (0x227000, 0x228000),
    'Allstar CPU versus human exhibition policy': (0x229000, 0x22A000),
    'native season menu and settings defaults': (0x22A000, 0x22B000),
    'Allstar team-overall full-D0 ABI repair': (0x22C300, 0x22C400),
    'Allstar CPU policy identification': (0x22CD6C, 0x22CD70),
    'transfer-screen native controls and footer': (0x250000, 0x250C00),
    'units/glass preferences, team mean, pass charge, line pool': (0x258000, 0x259000),
    'Momentum below the two native team names': (0x259000, 0x25A000),
    'menu plus-minus and modal-owned dollar glyph': (0x25A000, 0x25A040),
    'shared native menu controls and help': (0x25B000, 0x25C000),
    'season slots and per-context refinements preferences': (0x2AA000, 0x2AA800),
    'retire HUD before native Stars window': (0x2DB600, 0x2DB800),
    'native framed powerplay draw and clear': (0x2DCC00, 0x2DD000),
    'CPU frame buffer admission call sites': (0x2DD000, 0x2DD400),
    'owned friendly-catch opening fallback': (0x2DF000, 0x2DF400),
    'shootout/default settings row remap': (0x2DE000, 0x2DE200),
    'CPU line-screen native controls': (0x2DE900, 0x2DF000),
    'native replay option/menu defaults and text': (0x2E0000, 0x2E0800),
    'menu labels/value groups': (0x2E1200, 0x2E1700),
    'replay caller-owned 180-byte framed-PP snapshot': (0x2E5000, 0x2E5400),
    'finished-season browser controls': (0x2E6000, 0x2E6800),
    'pass settings default Low and permanent-rule normalization': (0x2E6800, 0x2E7000),
    'season end and restored native pregame presentation': (0x2E7000, 0x2E8200),
    'native replay title with frozen framed-PP HUD': (0x2EC000, 0x2EC800),
    'shot meter and pass-charge display': (0x2ED000, 0x2ED800),
    'standings native controls/footer': (0x2F9000, 0x2F9800),
    'leaders and abandon-season native controls/footer': (0x2FA000, 0x2FAC00),
    'team statistics native controls/footer': (0x2FB000, 0x2FB800),
    'matchup table native controls/footer': (0x2FC000, 0x2FC800),
    'optional fragile glass': (0x2FDB00, 0x2FDD00),
}
MAIN_FOOTER_NATIVE = [(0xF7728,0xF772A),(0xF774C,0xF774E),(0xF7758,0xF775A),(0xF7762,0xF7764),(0xF7C08,0xF7C0A),(0xF7B82,0xF7B84),(0xF7BFC,0xF7BFE),(0xF80A2,0xF80A4)]
NATIVE = [
    (0x120, 0x180), (0x18E, 0x190),  # version/checksum
    (0x1960, 0x19C0),               # exact five-pulse warning sequence
    (0x8398, 0x83A0),               # restored native line-selection pool
    (0x11C38, 0x11C42),             # guarded single font-token lookup
    (0x12ACC, 0x12AD2),             # native PP framed-box draw
    (0x12AF0, 0x12AF1), (0x12AFA, 0x12AFB), (0x12B1E, 0x12B1F),
    (0x12C7C, 0x12C7D), (0x12C80, 0x12C82),
    (0x12C8E, 0x12C8F), (0x12CA0, 0x12CA1),  # score frame/name rows
] + [(at, at + len(bytes.fromhex(old))) for at, old, _ in MENU_HOOKS] + MAIN_FOOTER_NATIVE
EXACT = {
    'ordinary club/player pointer tables': (0x30E, 0x180E),
    'ordinary physical rosters and uniforms': (0x260000, 0x28EE00),
    'Allstar rosters and black/red uniforms': (0x22D000, 0x22F000),
    'original startup image and routine': (0x2A0000, 0x2A9000),
    'supplied menu image': (0x240000, 0x2494E6),
    'physical star/goal palette': (0x59900, 0x59A00),
    'large native font': (0xAAC52, 0xAB970),
    'small native font': (0xBE26A, 0xBEFB8),
    'native packed replay decoder': (0xA68C, 0xA79C),
    'native code before the declared VCR-exit menu hook': (0xA000, 0xA39E),
    'native shot/puck physics and ordinary passing after VCR exit': (0xA3A8, 0xEE80),
    'original finisher positioning and native interception wrapper': (0x225000, 0x225800),
    'other Hard support and coverage': (0x226000, 0x227000),
    'original native interception and loose-puck lunge': (0x10588, 0x10750),
    'native Hard cross-ice helpers': (0x228000, 0x229000),
    'transfer engine/runtime and immutable market': (0x234000, 0x23C100),
    'transfer preferences and reset journal': (0x254000, 0x258000),
    'native season accounting/simulation': (0x2F8000, 0x2F9000),
}


def verify(before, after):
    assert hashlib.sha256(before).hexdigest() == BASE_SHA, 'exact runtime18 refinements parent required'
    actual_rom_sha256=hashlib.sha256(after).hexdigest()
    from main_menu_scope import prior_scope_image
    after = prior_scope_image(after)
    assert len(before) == len(after) == 3145728
    allowed = NATIVE + list(MODULES.values())
    changed = [i for i, (x, y) in enumerate(zip(before, after)) if x != y]
    bad = [i for i in changed if not any(lo <= i < hi for lo, hi in allowed)]
    assert changed and not bad, ('outside declared refinements scope', [hex(i) for i in bad[:32]])
    for name, (lo, hi) in EXACT.items():
        assert before[lo:hi] == after[lo:hi], ('preserved region changed', name, hex(lo), hex(hi))
    assert after[0x22CD6C:0x22CD70] == b'ASCP', 'Allstar CPU policy marker absent'
    assert after[0x2EC008:0x2EC010] == b'PPFR\x00\x12\x00\xb4', 'framed-PP replay snapshot protocol absent'
    return dict(parent_sha256=BASE_SHA, rom_sha256=actual_rom_sha256,
                changed_bytes=len(changed), native_sites=NATIVE, named_modules=MODULES,
                preserved_regions=EXACT,
                scope='Requested menus/defaults/presentation/HUD, Allstar CPU allocation, replay frame snapshot and local friendly-pass reception repair. '
                      'Native ABI and actual gameplay are separately mandatory; no universal trajectory or Hard-strength claim.')
