"""Fresh V4 lossless merge; all V3 code/data and canonical site remain read-only."""
import hashlib, json, re, sqlite3, sys
from pathlib import Path
from freeze_inputs import ROOT, WORK, SOURCE, BASE_RELEASE, read, rows, sha, require
from source_common import baseline_records, encoded
sys.path.insert(0, str(ROOT/'scripts_build'))
import build_public_name_directory as base
from public_name_v3_catalog import Catalog as OriginalCatalog

HASH=re.compile(r'[0-9a-f]{64}')
def project(row, scope, kind, normalizer):
    role,key,form=row['role'],row['key'],row['form'];proof=row['provenance']
    require(role in ('given','family') and normalizer.name_key(form)==key and normalizer.hygiene_reason(normalizer.normalize_form(form)) is None and row['language'] is None, 'Invalid admitted literal spelling/role')
    if kind=='datacite_native':
        declared=proof['source_scope_id'];require(declared==scope and row['source']=='bibliographic:datacite2025' and row['tier']=='bibliographic' and proof['license']=='CC0-1.0', 'DataCite source/scope differs')
        support=proof['distinct_doi_support'];require(type(support) is int and support>=2,'Invalid native distinct DOI support')
        source_witnesses=proof['support_witnesses'];require(len(source_witnesses)==2,'Missing native exact witnesses')
        witnesses=[{k:w[k] for k in ('doi','field','form','role','member','line')} for w in source_witnesses]
        chosen=proof['chosen_field'];require(chosen['form']==form and chosen['role']==role,'Native display form is not exactly witnessed')
        chosen={k:chosen[k] for k in ('doi','field','form','role','member','line')}
        matches=[n for n,w in enumerate(witnesses) if w['doi']==chosen['doi']]
        if matches:witnesses[matches[0]]=chosen
        else:witnesses.append(chosen)
        require(len({w['doi'] for w in witnesses})==len(witnesses) and all(w['role']==role and normalizer.name_key(w['form'])==key and
            w['field'].endswith('.givenName' if role=='given' else '.familyName') and isinstance(w['member'],str) and type(w['line']) is int and w['line']>0 for w in witnesses),'Native exact witness role/form differs')
        native_sha=proof['native_receipt_sha256'];require(HASH.fullmatch(native_sha),'Missing source receipt pin')
        return {'source_id':'datacite-public-2025','cohort':scope,'tier':'bibliographic','role':role,'form':form,'language':None,
            'distinct_doi_support':support,'witnesses':witnesses,'native_receipt_sha256':native_sha}
    if kind=='orcid_minimized':
        require(row['source']=='bibliographic:orcid2026' and row['tier']=='bibliographic' and proof['license']=='CC0-1.0' and proof['self_asserted_public_name_field'] is True and
            proof['primary_field']==('given-names' if role=='given' else 'family-name') and type(row['distinct_support_records']) is int and row['distinct_support_records']>=2 and 'support_examples' not in proof,'Invalid minimized ORCID evidence')
        return {'source_id':'orcid-public-2026','tier':'bibliographic','evidence_kind':'public_profile_summary','role':role,'form':form,'language':None,
            'cohort':scope,'primary_field':proof['primary_field'],'distinct_public_record_support':row['distinct_support_records']}
    crossref_sources = {
        'crossref-rest-2026-10-01-03-01':'bibliographic:crossref_rest_created_20261001_03',
        'crossref-rest-2026-10-04-06-01':'bibliographic:crossref_rest_created_20261004_06',
        'crossref-rest-2026-10-07-09-01':'bibliographic:crossref_rest_created_20261007_09',
        'crossref-rest-2026-10-07-09-frontier01':'bibliographic:crossref_rest_created_20261007_09_frontier01',
        'crossref-rest-2026-10-10-12-01':'bibliographic:crossref_rest_created_20261010_12',
    }
    require(kind=='crossref_bibliographic_facts' and row['source']==crossref_sources.get(scope) and row['tier']=='bibliographic_crossref' and proof['scope']==scope and
        'license' not in proof and proof['terms_label'].startswith('Reusable bibliographic facts'),'Crossref terms/source/scope differ')
    support=row['distinct_support_dois'];require(type(support) is int and support>=2,'Crossref DOI support differs')
    witnesses=proof['support_witnesses'];require(len(witnesses)==2 and len({w['doi'] for w in witnesses})==2 and
        all(set(w)=={'doi','field','form','role','response_row_locator'} and w['role']==role and normalizer.name_key(w['form'])==key and
            re.fullmatch(r'author\[[0-9]+\]\.'+('given' if role=='given' else 'family'),w['field']) and
            re.fullmatch(r'response-[0-9]+\.json#message\.items\[[0-9]+\]',w['response_row_locator']) for w in witnesses),'Crossref exact public witness differs')
    forms=row['forms'];require(form in forms and all(normalizer.name_key(f)==key and normalizer.hygiene_reason(normalizer.normalize_form(f)) is None for f in forms),'Crossref literal forms differ')
    return {'source_id':'crossref-rest-bibliographic','cohort':scope,'tier':'bibliographic_crossref','evidence_kind':'declared_author_name','role':role,'form':form,'forms':forms,'language':None,
        'distinct_doi_support':support,'created_window':proof['created_window'],'witnesses':witnesses}

class Catalog(OriginalCatalog):
    def event(self,key,form,data,rank=0):
        self.db.execute('INSERT INTO names(key,name,base_json,priority) VALUES(?,?,NULL,?) ON CONFLICT(key) DO UPDATE SET name=excluded.name,priority=excluded.priority WHERE names.base_json IS NULL AND excluded.priority<names.priority',(key,form,rank))
        for literal in data.get('forms',[form]):self.db.execute('INSERT OR IGNORE INTO forms VALUES(?,?)',(key,literal))
        self.db.execute('INSERT OR IGNORE INTO roles VALUES(?,?)',(key,data['role']))
        self.db.execute('INSERT OR IGNORE INTO source_roles VALUES(?,?,?)',(key,data['source_id'],data['role']))
        value=encoded(data).decode('utf-8').rstrip('\n');digest=hashlib.sha256(encoded({'key':key,'event':data})).hexdigest()
        self.db.execute('INSERT OR IGNORE INTO events(key,source_id,data,event_sha,unit_rank) VALUES(?,?,?,?,?)',(key,data['source_id'],value,digest,rank))
    def baseline(self):
        manifest=read(ROOT/'site/assets/public-name-data'/BASE_RELEASE/'manifest.json');fingerprint=sha(ROOT/'site/assets/public-name-data'/BASE_RELEASE/'manifest.json')
        if self.done('baseline911217',fingerprint) is not None:return manifest
        count=0
        for row in baseline_records():
            retained={k:v for k,v in row.items() if k not in ('attestations','source_role_difference')}
            self.db.execute('INSERT OR IGNORE INTO names(key,name,base_json) VALUES(?,?,?)',(row['key'],row['name'],encoded(retained).decode('utf-8')))
            for form in row['forms']:self.db.execute('INSERT OR IGNORE INTO forms VALUES(?,?)',(row['key'],form))
            for role in row['roles']:self.db.execute('INSERT OR IGNORE INTO roles VALUES(?,?)',(row['key'],role))
            for locator in row['primary_locators']:self.db.execute('INSERT OR IGNORE INTO source_roles VALUES(?,?,?)',(row['key'],row['source_id'],locator['role']))
            for item in row.get('attestations',[]):self.event(row['key'],item['form'],item)
            count+=1
            if count%25000==0:self.db.commit();print(json.dumps({'stage':'baseline_records','keys':count}),flush=True)
        require(count==911217,'Baseline count differs');self.db.execute('INSERT INTO units VALUES(?,?,?)',('baseline911217',fingerprint,count));self.db.commit()
        self.db.execute('PRAGMA wal_checkpoint(TRUNCATE)');return manifest
    def add(self,path,unit,fingerprint,specs,rank):
        path=Path(path);require(sha(path)==fingerprint,'Closed witness input changed')
        count=self.done(unit,fingerprint)
        if count is not None:return count
        normal=base.policy();counts={scope:0 for scope in specs};seen=set();count=0
        for row in rows(path):
            scope=row['provenance']['source_scope_id'] if len(specs)>1 or next(iter(specs.values()))['kind']=='datacite_native' else next(iter(specs))
            require(scope in specs,'Witness export contains a scope outside the frozen V4 cutoff')
            spec=specs[scope];pair=(scope,row['role'],row['key']);require(pair not in seen,'Duplicate source scope/role/key');seen.add(pair)
            data=project(row,scope,spec['kind'],normal);self.event(row['key'],row['form'],data,rank)
            counts[scope]+=1;count+=1
            if count%10000==0:self.db.commit()
        require(all(counts[s]==sum(specs[s]['expected_roles'].values()) for s in specs),'Witness scope count differs from native closure')
        self.db.execute('INSERT INTO units VALUES(?,?,?)',(unit,fingerprint,count));self.db.commit();self.db.execute('PRAGMA wal_checkpoint(TRUNCATE)')
        return count

if __name__=='__main__':
    c=Catalog(WORK/'private-catalog/catalog.sqlite3')
    try:c.baseline()
    finally:c.close()
