"""Apply source adjudication pass 2 after apply_audit.py.
Usage: python continue_audit.py INPUT_DIRECTORY AUDIT_OUTPUT_DIRECTORY
Standard library only. No final analysis or automatic biological approval.
"""
import csv, json, sys
from pathlib import Path
src,out=map(Path,sys.argv[1:3])
def read(p,sep=','):
    with p.open(encoding='utf-8-sig',newline='') as f:return list(csv.DictReader(f,delimiter=sep))
def write(p,rr):
    with p.open('w',newline='') as f:
        w=csv.DictWriter(f,fieldnames=list(rr[0]));w.writeheader();w.writerows(rr)
rr=read(out/'measurement_audit.csv');aa=read(src/'anage_data.txt','\t');an={a['Genus']+' '+a['Species']:a for a in aa}
dec=[]
def decision(r,method,endpoint,status,note,url,identity='not_fully_verified',target=None,numeric='not_verified'):
    r.update(method_audited=method,endpoint_audited=endpoint,screening_status=status,source_check_note=note,source_check_url=url,identity_status=identity,numeric_source_status=numeric)
    if target:
        a=an[target];r.update(anage_target_audited=target,anage_HAGRID_audited=a['HAGRID'],longevity_years_audited=a['Maximum longevity (yrs)'],anage_quality_audited=a['Data quality'])
    dec.append({k:r[k] for k in ['record_id','Species_phylo','CFF_corrected','method_audited','endpoint_audited','screening_status','identity_status','anage_target_audited','longevity_years_audited','numeric_source_status','source_check_note','source_check_url']})
for r in rr:
    a=an.get(r['anage_candidate'],{})
    r.update(method_audited=r['Method'],endpoint_audited=r['endpoint_status'],screening_status='awaiting_source_check',source_check_note='',source_check_url=r['decision_source'],identity_status='not_fully_verified',numeric_source_status='not_verified',anage_target_audited=r['anage_candidate'],anage_HAGRID_audited=a.get('HAGRID',''),longevity_years_audited=r['longevity_years'],anage_quality_audited=r['anage_quality'])
    rid=r['record_id'];sp=r['Species_phylo']
    if sp.startswith('Ficedula_'):
        r.update(screening_status='measurement_verified_other_eligibility_checks_pending',numeric_source_status='verified_mean',identity_status='source_species_supported')
    elif sp=='Melanophila_acuminata':r['screening_status']='exclude_nonvisual_endpoint'
    elif r['endpoint_status']=='right_censored_CFF_greater_than_80_Hz':r['screening_status']='exclude_exact_threshold_model'
    if rid=='H2025_219':
        decision(r,'Behavioural','conditioned_suppression_flicker_threshold','hold_tree_identity_and_numeric_extraction','Four beagles are domestic dogs. Replace the wolf longevity pairing (20.6 yr) with the supplied AnAge domestic-dog record (27 yr, acceptable). The abstract verifies the animals and method, not the compiled 84 Hz statistic. A Canis_lupus tree tip is not automatically a validated domestic-dog tip.','https://pubmed.ncbi.nlm.nih.gov/2813532/','domestic_dog_verified',target='Canis familiaris')
    elif rid in ['H2025_222','H2025_223','H2025_224','H2025_225']:
        defs={
        'H2025_222':('Haemodynamic','optic_nerve_head_blood_flow','exclude_endpoint_from_behavioural_and_ERG_strata','Blood-flow response is neither behavioural CFF nor ERG.','https://pubmed.ncbi.nlm.nih.gov/7730982/'),
        'H2025_223':('Intracellular','retinal_horizontal_cell_frequency_transfer','exclude_endpoint_from_behavioural_and_ERG_strata','Intracellular horizontal-cell recording is distinct from whole-retina ERG.','https://pubmed.ncbi.nlm.nih.gov/1891807/'),
        'H2025_224':('Behavioural','behavioural_temporal_modulation','hold_domestic_identity_and_questionable_longevity','Study is behavioural; exact 47.5 Hz extraction and domestic population need full-text verification.','https://pubmed.ncbi.nlm.nih.gov/1136168/'),
        'H2025_225':('Cortical electrophysiology','visual_cortex_frequency_response','exclude_endpoint_from_behavioural_and_ERG_strata','Compilation incorrectly calls this Behavioural; the source records cortical responses.','https://pubmed.ncbi.nlm.nih.gov/9751156/')}
        m,e,s,n,u=defs[rid]
        decision(r,m,e,s,n+' All four compilation rows say House cat. Felis catus (30 yr, questionable) is retained as the appropriate candidate instead of silently using wildcat longevity. This is not final source-species approval.',';'.join([u,'user-provided anage_data.txt']),'domestic_cat_candidate_pending_full_text',target='Felis catus')
    elif rid=='H2025_273':
        decision(r,'Behavioural','behavioural_fusion_frequency','hold_numeric_extraction','Publisher identifies animal psychophysics and behavioural CFF in Chrysemys picta picta. Correct the compilation method from Electrophysiology to Behavioural. Species-level longevity match is supported; 31 Hz and experimental conditions still need full text. The HBS suffix is retained as a source-tree label, not asserted to identify an experimental animal.','https://doi.org/10.1159/000123745','source_species_supported')
    elif rid=='H2025_280':
        decision(r,'Behavioural','motor_response_to_visual_flicker','hold_numeric_extraction','Original paper names Pseudemys scripta; Reptile Database lists that name under Trachemys scripta. Correct sscripta in the biological identity field, retaining the raw tree label. The exact 52.6 Hz statistic remains unchecked.','https://pubmed.ncbi.nlm.nih.gov/19873106/;https://reptile-database.reptarium.cz/Trachemys/scripta','source_species_and_alias_supported',target='Trachemys scripta')
    elif rid in ['H2025_087','H2025_089']:
        decision(r,'Isolated-retina ERG','hatchling_retinal_frequency_following','hold_numeric_conflict_and_developmental_scope','Source abstract reports leatherback responses not following above 10 Hz and loggerhead responses extending to 15 Hz; the compiled values are 15 and 40 Hz respectively. Do not replace them with 10/15 as if these were comparable mean CFF estimates. Full-text threshold definition and extraction must be reconciled. Species binomial is supported, specimen suffix provenance is not.','https://pubmed.ncbi.nlm.nih.gov/19146881/','source_species_supported')
    elif rid=='H2025_088':
        decision(r,'Electrophysiology','unverified_retinal_endpoint','hold_original_report_required','Species suffix normalization alone does not validate 40 Hz. Obtain the Fritsches and Warrant 2006 NOAA report and identify the exact species, life stage and endpoint.','User-provided Haarlem CFF_reference: Fritsches and Warrant 2006','label_normalization_only')
    elif rid=='H2025_038':
        decision(r,'Corneal ERG','maximum_intensity_FFF_median','hold_species_assignment','Full article verifies 88 Hz as a median at Imax, not a mean or an individual maximum. Methods describe Florida Keys lionfish as Pterois spp.; species-specific P. volitans attribution is not established in the inspected methods. Retain genus-level study identity and withhold the species-level longevity pairing.','https://par.nsf.gov/servlets/purl/10159097','Pterois_spp_only',numeric='verified_median_88_at_Imax')
    elif rid=='H2025_117':
        decision(r,'Behavioural motion task','movement_fusion_rotating_pattern','exclude_from_uniform_flicker_primary_hold_caste_match','Primary summary reports approximately 200 Hz movement fusion using rotating sectored discs. Keep this as a motion-based endpoint, separate from uniform-field flicker. Worker/queen longevity compatibility also remains unverified. Do not assert the 8-year AnAge record describes the tested bees.','https://doi.org/10.1007/BF00610583','subspecies_and_caste_not_verified',numeric='verified_approximate_movement_fusion_200')
    elif rid=='H2025_252':
        decision(r,'Electrophysiology','neonatal_nutrition_intervention_retinal_response','hold_development_and_endpoint','Study enrolled piglets at 2–5 days and compared parenteral lipid treatments. Abstract does not establish the compiled 60 Hz as a measured fusion threshold. Do not infer a bound from another related paper; control-group provenance and exact frequency protocol remain unresolved.','https://pubmed.ncbi.nlm.nih.gov/29534288/','domestic_pig_population_supported')
    elif rid=='H2025_052':
        decision(r,'Photoreceptor electrophysiology','rod_photoreceptor_flicker','exclude_endpoint_from_behavioural_and_ERG_strata','Rod photoreceptor endpoint belongs in a separate mechanistic inventory. Bufo marinus is a historical name associated with Rhinella marina, but later species-complex changes mean original specimen locality should still be checked.','https://pubmed.ncbi.nlm.nih.gov/6415914/;https://amphibiansoftheworld.amnh.org/Amphibia/Anura/Bufonidae/Rhinella/Rhinella-marina','historical_alias_supported_locality_pending')
    elif rid=='H2025_053':
        decision(r,'Electrophysiology','primary_endpoint_unverified','hold_primary_source_required','Rana clamitans/Lithobates clamitans nomenclatural connection is supported. The compilation cites Inger 2014, a review, and links t001, which is a human-effects table. Trace its supporting species table to the original experiment before accepting 21 Hz.','https://amphibiansoftheworld.amnh.org/Amphibia/Anura/Ranidae/Lithobates/Lithobates-clamitans;https://doi.org/10.1371/journal.pone.0098631','alias_supported_experimental_identity_pending')
    elif rid=='H2025_070':
        decision(r,'Retinal ERG','adult_hen_retinal_flicker_threshold','hold_exact_numeric_extraction_secondary_ERG_only','Correct compilation Behavioural to retinal ERG. Primary abstract describes adult hens and approximately 105 Hz average at 2740 cd/m2. The compiled 104 Hz may use a specific extraction or estimator; retain it pending full-text verification instead of rounding it to 105.','https://pubmed.ncbi.nlm.nih.gov/22521657/','domestic_hen_source_verified_population_match_pending')
    elif rid=='H2025_256':
        decision(r,'Retinal ERG','retinal_flicker_response','hold_exact_numeric_extraction_secondary_ERG_only','Correct compilation Behavioural to retinal ERG, as identified by the original paper and bibliographic indexing. Exact 90 Hz and historical Tupaia species limits need full-text checking.','https://pubmed.ncbi.nlm.nih.gov/5608651/','source_label_supported_historical_limits_pending')
    elif rid=='H2025_064':
        decision(r,'Behavioural','LED_discrimination_75_percent_threshold','hold_table_numeric_extraction','Experiment I genuinely measured flicker thresholds in adult pigeons despite the symmetry-focused title. Threshold is 75 percent correct; wavelengths, training and duty cycle alter results. Do not reject from title alone. Confirm the 70 Hz table entry and choose a consistent protocol before aggregation.','https://doi.org/10.1371/journal.pone.0187541','source_species_supported')
    elif rid=='H2025_079':
        decision(r,'Behavioural','species_mean_peak_behavioural_CFF','measurement_verified_other_eligibility_checks_pending','Original Bostrom study reports 130.3 Hz in blue tits; Parus caeruleus maps to Cyanistes caeruleus. This validates the source value and alias, not every future analysis gate.','https://doi.org/10.1371/journal.pone.0151099;https://www.itis.gov/servlet/SingleRpt/SingleRpt?search_topic=TSN&search_value=1280033','source_species_and_alias_supported',numeric='verified_mean_130.3')
write(out/'measurement_audit_pass2.csv',rr);write(out/'source_decisions_pass2.csv',dec)
# Disjoint statuses: no absence of a flag is treated as completed review.
cc=read(out/'crosswalk_with_corrections.csv')
for c in cc:
    group=[r for r in rr if r['Species_phylo']==c['cff_taxon']]
    first=group[0]
    c.update(anage_target_pass2=first['anage_target_audited'],longevity_years_pass2=first['longevity_years_audited'],anage_quality_pass2=first['anage_quality_audited'],methods_pass2=';'.join(sorted({r['method_audited'] for r in group})),screening_statuses_pass2=';'.join(sorted({r['screening_status'] for r in group})))
write(out/'crosswalk_pass2.csv',cc)
queue=[r for r in rr if r['longevity_years_audited'] and r['anage_quality_audited'] in ['acceptable','high'] and not r['screening_status'].startswith('exclude')]
write(out/'candidate_source_check_queue.csv',sorted(queue,key=lambda r:(r['method_audited']!='Behavioural',r['Species_phylo'],r['record_id'])))
summary=json.loads((out/'audit_summary.json').read_text())
quality=[r for r in cc if r['longevity_years_pass2'] and r['anage_quality_pass2'] in ['acceptable','high']]
summary.update(audit_pass=2,pass2_source_decisions=len(dec),quality_candidates_after_domestic_cat_reclassification=len(quality),numerical_candidate_taxon_labels=sum(bool(r['longevity_years_pass2']) for r in cc),candidate_queue_measurements=len(queue),candidate_queue_taxon_labels=len({r['Species_phylo'] for r in queue}),final_analysis_sample_frozen=False)
assert len(rr)==280 and len(cc)==237 and len(dec)==19
assert next(r for r in rr if r['record_id']=='H2025_219')['longevity_years_audited']=='27'
assert next(r for r in rr if r['record_id']=='H2025_225')['method_audited']=='Cortical electrophysiology'
assert next(r for r in rr if r['record_id']=='H2025_273')['method_audited']=='Behavioural'
assert len(quality)==94
(out/'audit_summary_pass2.json').write_text(json.dumps(summary,indent=2)+'\n')
print(json.dumps({k:summary[k] for k in ['pass2_source_decisions','quality_candidates_after_domestic_cat_reclassification','candidate_queue_measurements','candidate_queue_taxon_labels','final_analysis_sample_frozen']},indent=2))
