"""Run a synthetic native-schema study and validate replay/resume, never real AQ.

Usage: python verification/run_native_smoke.py --directory /tmp/suite35-proof
The directory must be new. All generated inputs/results are confined beneath it.
"""
import argparse
import json
import os
from pathlib import Path
import subprocess
import sys
import time

import numpy as np
import pandas as pd
from sklearn.metrics import average_precision_score

PACKAGE=Path(__file__).resolve().parents[1]
sys.path.insert(0,str(PACKAGE));sys.path.insert(0,str(PACKAGE/'tests'))
from aq_fixture import write_project
from pace.data import sha256
from pace.runner import source_digest


def main():
    parser=argparse.ArgumentParser(description=__doc__)
    parser.add_argument('--directory',required=True)
    args=parser.parse_args()
    work=Path(args.directory).expanduser().resolve()
    if work.exists():raise ValueError('Use a new directory for isolated synthetic verification')
    work.mkdir(parents=True)
    root=write_project(work/'inputs/project')
    output=work/'results';master=work/'master.log'
    env={**os.environ,'AQ_ROOT':str(root),'AQ_PYTHON':sys.executable,'SUITE35_LOG_FILE':str(master)}
    command=[str(PACKAGE/'run_suite35.sh'),'--output-dir',str(output),'--seeds','29',
             '--ssl-epochs','1','--neural-epochs','1','--dim','8','--hidden-dim','8',
             '--layers','1','--heads','2','--batch-size','64','--ssl-batch-size','16',
             '--threads','1','--bootstrap','10','--hgb-iterations','2','--hgb-min-samples-leaf','2']
    files=sorted(list(root.rglob('*.joblib'))+list((work/'inputs/database').glob('*.csv')))
    hashes={str(p):sha256(p) for p in files}
    (work/'source_hashes_before.json').write_text(json.dumps(hashes,indent=2))
    started=time.time()
    run=subprocess.run(command,cwd=PACKAGE,env=env,text=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
    (work/'execution.log').write_text(run.stdout)
    if run.returncode:raise RuntimeError(f'Native run failed ({run.returncode}); see {work}/execution.log')
    assert 'SUITE35 COMPLETE' in run.stdout and '[RUN STATUS] SUCCESS' in run.stdout
    assert master.read_text()==run.stdout,'Consolidated log did not capture the complete execution'
    results=pd.read_csv(output/'all_results.csv')
    predictions=pd.read_csv(output/'reports/predictions_all.csv',dtype={'row_id':str,'faculty':str})
    keys=['faculty','fold','seed','arm','arch','task']
    assert len(results)==504 and not results.duplicated(keys).any()
    assert len(results.groupby(['arm','arch','task']))==36
    assert set(results.fold)==set(range(1,8)) and set(results.faculty)=={'A','B'}
    assert results.checkpoint_replay.all()
    ap_checks=0
    for key,group in predictions.groupby(keys):
        match=results
        for name,value in zip(keys,key):match=match[match[name]==value]
        assert len(match)==1 and not group.row_id.duplicated().any()
        assert np.isclose(average_precision_score(group.y,group.p),match.ap.iloc[0],atol=1e-12)
        ap_checks+=1
    assert ap_checks==504
    freeze=json.loads((output/'protocol_freeze.json').read_text())
    assert freeze['development_folds']==[1,2,3,4,5]
    identity=json.loads((output/'run_identity.json').read_text())
    assert identity['source_digest']==source_digest()
    checkpoints=sorted(list((output/'cells').glob('*/checkpoint.*')))
    checkpoint_state={str(p):(p.stat().st_mtime_ns,sha256(p)) for p in checkpoints}
    resume=subprocess.run(command,cwd=PACKAGE,env=env,text=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
    (work/'resume.log').write_text(resume.stdout)
    assert resume.returncode==0 and '[RUN STATUS] SUCCESS' in resume.stdout
    assert '\nARM START ' not in resume.stdout
    assert sum(line.startswith('RESUME ') for line in resume.stdout.splitlines())==504
    assert checkpoint_state=={str(p):(p.stat().st_mtime_ns,sha256(p)) for p in checkpoints}
    assert master.read_text()==run.stdout+resume.stdout
    assert hashes=={str(p):sha256(p) for p in files},'An upstream synthetic source changed'
    summary=dict(synthetic=True,cells=504,variants=36,faculties=2,folds=7,seeds=[29],
                 encoders=['mlp','gru','transformer'],tasks=['grade','course','joint'],
                 ap_checks=ap_checks,checkpoint_replay_cells=504,resumed_cells=504,
                 source_immutable=True,checkpoint_immutable_on_resume=True,
                 full_log_verified=True,development_freeze_verified=True,
                 source_digest=identity['source_digest'],seconds=time.time()-started,
                 real_AQ_scores_verified=False,CUDA_verified=False,command=command)
    (work/'summary.json').write_text(json.dumps(summary,indent=2)+'\n')
    print(json.dumps(summary,indent=2),flush=True)


if __name__=='__main__':main()
