"""Data-only join of hash-bound admitted names to exact retained DOI fields.

Historical complete native raw-source/support/minority/quarantine proofs are
reused. This is neither an archive replay nor a new admission calculation.
"""
import argparse, gzip, hashlib, json, os, re, sys
from datetime import datetime, timezone
from pathlib import Path
from freeze_inputs import ROOT, WORK, read, rows, sha, write, require
from source_common import encoded

def export(spec, policy):
    root = ROOT / spec['receipt_path']; receipt = read(root); directory = root.parent
    wanted = spec['id']; output = WORK / 'native-exports' / (wanted.replace(':','--') + '.jsonl.gz')
    rp = output.with_name(output.name + '.receipt.json'); output.parent.mkdir(exist_ok=True)
    if rp.exists():
        old = read(rp); require(old['ok'] and old['source_scope_id'] == wanted and old['export_sha256'] == sha(output) and old['input_manifest_sha256'] == sha(WORK/'INPUT_MANIFEST.json'), 'Committed witness export changed'); return old
    require(not output.exists(), 'Unclosed native export exists; preserve it and use a fresh attempt')
    require(sha(root) == spec['receipt_sha256'] and sha(ROOT/spec['path']) == spec['sha256'], 'Frozen native admitted input changed')
    require(sha(ROOT/spec['attestations_path']) == spec['attestations_sha256'] and sha(directory/'SOURCE_REPLAY_VERIFICATION.json') == spec['support_proof_sha256'], 'Frozen retained field/support proof changed')
    targets = {}
    for number,row in enumerate(rows(ROOT/spec['path']),1):
        pair = row['role'],row['key']; require(pair not in targets and row['evidence_tier']=='bibliographic' and row['distinct_support_dois']>=2 and policy.name_key(row['form'])==row['key'], 'Invalid committed native name')
        targets[pair] = {'native':row,'native_row':number,'chosen':None,'witnesses':{}}
    require(len(targets)==sum(spec['expected_roles'].values()), 'Native row count differs')
    remaining=len(targets); scanned=0; malformed_quarantine_markers=0
    with gzip.open(ROOT/spec['attestations_path'],'rb') as stream:
        for line_number,line in enumerate(stream,1):
            require(len(line)<=16*1024*1024,'Retained field line exceeds frozen native bound')
            row=json.loads(line); scanned=line_number
            if row.get('quarantined_record') is True:
                require(row['reason']=='missing_or_malformed_doi' and set(row)=={'quarantined_record','member','line','raw_sha256','raw_bytes','reason'}, 'Typed DOI quarantine marker differs')
                malformed_quarantine_markers+=1;continue
            if 'record' in row or row.get('reason')!='':continue
            target=targets.get((row['role'],row['key']))
            if target is None:continue
            need_chosen=target['chosen'] is None and row['form']==target['native']['form']
            need_support=len(target['witnesses'])<2 and row['doi'] not in target['witnesses']
            if not (need_chosen or need_support):continue
            require(re.fullmatch(r'10\.[0-9]{4,9}/\S+',row['doi']) and row['doi']==row['doi'].lower() and policy.name_key(row['form'])==row['key'] and row['field'].endswith('.givenName' if row['role']=='given' else '.familyName'), 'Selected exact DOI field differs')
            witness={k:row[k] for k in ['doi','member','line','field','form','role']};witness['attestation_line']=line_number
            was_done=target['chosen'] is not None and len(target['witnesses'])==2
            if need_chosen:target['chosen']=witness
            if need_support:target['witnesses'][row['doi']]=witness
            if not was_done and target['chosen'] is not None and len(target['witnesses'])==2:remaining-=1
            if remaining==0:break
    require(remaining==0,'A committed admitted role/form lacks two exact distinct-DOI witnesses')
    temp=output.with_name(output.name+'.temporary')
    with temp.open('xb') as raw,gzip.GzipFile(filename='',mode='wb',fileobj=raw,mtime=0,compresslevel=6) as out:
        for (role,key),target in sorted(targets.items(),key=lambda item:(item[0][1],item[0][0])):
            native=target['native']; witnesses=list(target['witnesses'].values())
            event={'role':role,'key':key,'form':native['form'],'language':None,'source':'bibliographic:datacite2025','tier':'bibliographic',
                'locator':spec['attestations_path']+'#jsonl-line='+str(target['chosen']['attestation_line']),
                'provenance':{'publisher':'DataCite','source_version':'public-2025','source_scope_id':wanted,
                    'distinct_doi_support':native['distinct_support_dois'],'minimum_distinct_dois':2,'role_minority_ratio':10,'role_minority_min_other':20,
                    'support_scope':'Only this complete native selected100-member batch; no cohort pooling, people count or independent-source claim.',
                    'native_receipt_sha256':spec['receipt_sha256'],'native_admitted_row':target['native_row'],
                    'historical_complete_raw_source_support_quarantine_proof_sha256':spec['support_proof_sha256'],
                    'chosen_field':target['chosen'],'support_witnesses':witnesses,'license':'CC0-1.0',
                    'license_url':'https://support.datacite.org/docs/datacite-data-file-use-policy','language_inferred':False,
                    'fresh_validation_scope':'Exact witness join from hash-bound retained fields; complete native raw/source/support/minority/quarantine proofs reused, no archive replay.'}}
            out.write(encoded(event))
    require(sha(root)==spec['receipt_sha256'] and sha(ROOT/spec['attestations_path'])==spec['attestations_sha256'] and sha(ROOT/spec['path'])==spec['sha256'], 'Native inputs changed during witness join')
    os.replace(temp,output)
    result={'schema':'ndr-public-v4-closed-native-witness-export/1','ok':True,'closed_at':datetime.now(timezone.utc).isoformat(),
        'source_scope_id':wanted,'input_manifest_sha256':sha(WORK/'INPUT_MANIFEST.json'),'admitted_rows':len(targets),
        'export_path':output.relative_to(ROOT).as_posix(),'export_sha256':sha(output),'export_bytes':output.stat().st_size,
        'native_names_sha256':spec['sha256'],'native_receipt_sha256':spec['receipt_sha256'],
        'native_attestations_sha256_before_and_after':spec['attestations_sha256'],'historical_complete_source_support_quarantine_proof_sha256':spec['support_proof_sha256'],
        'retained_attestation_prefix_lines_joined':scanned,'typed_DOI_quarantine_markers_seen_in_join_prefix':malformed_quarantine_markers,
        'all_chosen_forms_exactly_witnessed':True,'two_distinct_DOIs_per_role_key':True,'native_admission_and_counts_unchanged':True,
        'no_new_full_archive_replay_or_support_recount_claim':True,'exporter_sha256':sha(__file__)}
    write(rp,result);print(json.dumps(result),flush=True);return result

def main():
    parser=argparse.ArgumentParser();parser.add_argument('--cohorts',nargs='+',type=int,required=True);args=parser.parse_args()
    require(set(args.cohorts)<=set(range(4,10)),'Only frozen DataCite04–09 scopes are allowed')
    manifest=read(WORK/'INPUT_MANIFEST.json');sys.path.insert(0,str(ROOT/'scripts_build'));import build_public_name_directory as base
    policy=base.policy()
    for spec in manifest['inputs']:
        if spec['kind']=='datacite_native' and int(spec['id'].split(':')[0][-4:]) in args.cohorts:export(spec,policy)

if __name__=='__main__':main()
