"""Finite original offline request/row review. Not a GSC client or country registry."""
import json,pathlib,sys,re
def review(packet):
    def stop(s):return {'status':s,'country':None,'otherKeyValuesReturned':False,'captureAuthenticityVerified':False}
    if not isinstance(packet,dict):return stop('INVALID_PACKET')
    dims=packet.get('dimensions');row=packet.get('row')
    if not isinstance(dims,list) or not dims or any(not isinstance(x,str) or not x for x in dims):return stop('DIMENSIONS_UNBOUND')
    if len(set(dims))!=len(dims):return stop('DUPLICATE_DIMENSION')
    if 'country' not in dims:return stop('COUNTRY_NOT_GROUPED')
    if not isinstance(row,dict) or not isinstance(row.get('keys'),list):return stop('ROW_KEYS_MISSING')
    keys=row['keys']
    if len(keys)!=len(dims):return stop('KEY_COUNT_MISMATCH')
    country=keys[dims.index('country')]
    if not isinstance(country,str) or not re.fullmatch('[A-Za-z]{3}',country):return stop('COUNTRY_CODE_SHAPE_UNRESOLVED')
    out=stop('BOUND_COUNTRY_SHAPE_ONLY');out['country']=country.lower();out['countryDimensionIndex']=dims.index('country');out['countryRegistryMembershipVerified']=False
    return out
if __name__=='__main__':
    p=pathlib.Path(sys.argv[1])
    if p.stat().st_size>1048576:raise SystemExit('Input exceeds 1 MiB')
    try:
        d=json.loads(p.read_text(encoding='utf-8'));cases=d.get('cases')
        out=[{'ordinal':i+1,**review(x)} for i,x in enumerate(cases)] if isinstance(cases,list) else {'status':'INVALID_DOCUMENT'}
    except (ValueError,UnicodeError,AttributeError):out={'status':'INVALID_DOCUMENT'}
    print(json.dumps(out,ensure_ascii=False,indent=2))
