"""Independent implementation of a known Delft Bell-data benchmark.

No downloaded code is executed. This is a retrospective reproduction.
"""
from __future__ import annotations
import argparse
import csv
from datetime import datetime, timezone
from decimal import Decimal, localcontext
import hashlib
import io
import itertools
import json
import math
from pathlib import Path
import sys
import urllib.request
import zipfile

import numpy as np
from scipy.stats import binom

ROOT = Path(__file__).resolve().parent.parent
URL = "https://data.4tu.nl/file/e8cf2991-3153-48ad-b67d-dfd7d7d97fd3/289c4850-6ed5-45a3-8b19-de8671f873a8"
MD5 = "342f29f8288c46575818acd2acebd535"


def save(path, obj):
    path.parent.mkdir(parents=True, exist_ok=True)
    path.write_text(json.dumps(obj, indent=2, allow_nan=False)+"\n",encoding="utf8")


def local_proof():
    rows = []
    for aa0, aa1, bb0, bb1 in itertools.product((-1,1),repeat=4):
        score = aa0*bb0 + aa0*bb1 + aa1*bb0 - aa1*bb1
        wins = sum((aa0 if a==0 else aa1)*(bb0 if b==0 else bb1) == (-1)**(a*b)
                   for a,b in itertools.product((0,1),repeat=2))
        assert abs(score)==2 and wins<=3
        rows.append([aa0,aa1,bb0,bb1,score,wins])
    return rows


def quantum_calibration():
    x=np.array([[0.,1.],[1.,0.]])
    z=np.diag([1.,-1.]); eye=np.eye(2)
    psi=np.array([1.,0.,0.,1.])/np.sqrt(2)
    rho=np.outer(psi,psi)
    b0,b1=(z+x)/np.sqrt(2),(z-x)/np.sqrt(2)
    bell=np.kron(z,b0)+np.kron(z,b1)+np.kron(x,b0)-np.kron(x,b1)
    score=float(psi@bell@psi)
    rho_a=np.trace(rho.reshape(2,2,2,2),axis1=1,axis2=3)
    rho_b=np.trace(rho.reshape(2,2,2,2),axis1=0,axis2=2)
    assert abs(score-2*np.sqrt(2))<1e-12
    assert np.allclose(rho_a,eye/2) and np.allclose(rho_b,eye/2)
    return {"theoretical_only":True,"CHSH":score,"local_bound":2,
            "global_purity":float(np.trace(rho@rho)),
            "local_A_purity":float(np.trace(rho_a@rho_a)),
            "local_B_purity":float(np.trace(rho_b@rho_b))}


def decimal_tail(n,k,q):
    with localcontext() as context:
        context.prec=80
        p=sum(Decimal(math.comb(n,j))*q**j*(1-q)**(n-j) for j in range(k,n+1))
        return str(p)


def correlations(a,b,x,y):
    table=[]; es=[]; ns=[]
    for a0,b0 in ((0,0),(0,1),(1,0),(1,1)):
        mask=(a==a0)&(b==b0)
        n=int(mask.sum()); assert n>0
        counts=[int(np.sum(mask&(x==x0)&(y==y0))) for x0,y0 in ((1,1),(1,-1),(-1,1),(-1,-1))]
        es.append(float(np.mean((x*y)[mask]))); ns.append(n); table.append(counts)
    score=es[0]+es[1]+es[2]-es[3]
    se=math.sqrt(sum((1-e*e)/n for e,n in zip(es,ns)))
    return table,ns,es,float(score),se


def main():
    parser=argparse.ArgumentParser()
    parser.add_argument("--archive",type=Path,help="Previously downloaded, checksum-matched official archive")
    args=parser.parse_args()
    if args.archive:
        archive=args.archive.read_bytes()
    else:
        request=urllib.request.Request(URL,headers={"User-Agent":"OzoneArchiveScientificReanalysis/1.0"})
        with urllib.request.urlopen(request,timeout=60) as r: archive=r.read()
    assert len(archive)==152913 and hashlib.md5(archive).hexdigest()==MD5
    with zipfile.ZipFile(io.BytesIO(archive)) as z:
        raw=z.read("bell_open_data.txt")
    entries=list(csv.reader(io.StringIO(raw.decode("ascii"))))
    assert all(len(row)==17 for row in entries)
    data=np.array([[int(v) for v in row[1:]] for row in entries],dtype=np.int64)
    # Named column access follows the deposited schema, not downloaded executable code.
    first_time,first_channel=data[:,2],data[:,3]
    second_time,second_channel=data[:,4],data[:,5]
    starts=np.array([5426350,5425700])
    assert np.isin(first_channel,[0,1]).all() and np.isin(second_channel,[0,1]).all()
    window1=(first_time>=starts[first_channel])&(first_time<starts[first_channel]+52450)
    window2=(second_time>=starts[second_channel]+250000)&(second_time<starts[second_channel]+302450)
    herald=window1&window2&(first_channel!=second_channel)
    markers=np.all((data[:,14:16]==0)|(data[:,14:16]>250),axis=1)
    no_excite=np.all(data[:,12:14]==0,axis=1)
    keep=herald&markers&no_excite
    selected=data[keep]
    a,b=selected[:,6],selected[:,7]
    assert np.isin(a,[0,1]).all() and np.isin(b,[0,1]).all()
    x=np.where((selected[:,10]>10620)&(selected[:,10]<=14320),1,-1)
    y=np.where((selected[:,11]>10620)&(selected[:,11]<=14320),1,-1)
    table,ns,es,score,se=correlations(a,b,x,y)
    n=len(a); wins=int(np.sum(x*y==np.where(a*b==0,1,-1)))
    tau=Decimal("0.0000108")
    q=Decimal("0.75")+3*(tau+tau*tau)
    p=binom.sf(wins-1,n,float(q))
    exact_decimal=decimal_tail(n,wins,q)
    assert abs(p-float(exact_decimal))<1e-13
    assert n==245 and wins==196 and ns==[53,79,62,51]
    assert round(score,2)==2.42 and round(se,2)==.20 and round(p,3)==.039
    local=local_proof(); quantum=quantum_calibration()
    rng=np.random.default_rng(11115000)
    null_scores=[]
    for _ in range(2000):
        shuffled=y.copy()
        for setting in (0,1):
            mask=b==setting; shuffled[mask]=rng.permutation(y[mask])
        null_scores.append(correlations(a,b,x,shuffled)[3])
    out=ROOT/"results";out.mkdir(exist_ok=True)
    with (out/"counts.csv").open("w",newline="",encoding="utf8") as f:
        w=csv.writer(f);w.writerow(["a","b","x","y","count"])
        for (a0,b0),counts in zip(((0,0),(0,1),(1,0),(1,1)),table):
            for (x0,y0),count in zip(((1,1),(1,-1),(-1,1),(-1,-1)),counts):w.writerow([a0,b0,x0,y0,count])
    with (out/"local_deterministic_strategies.csv").open("w",newline="",encoding="utf8") as f:
        w=csv.writer(f);w.writerow(["A0","A1","B0","B1","S","uniform_setting_wins_of_4"]);w.writerows(local)
    save(out/"selected_raw_row_indices.json",np.flatnonzero(keep).tolist())
    save(out/"permutation_scores.json",null_scores)
    result={"record_id":"O0-PHYS-001","analysis":"retrospective_reproduction_known_result",
        "verdict":"REPRODUCED_BELL_VIOLATION","executed_utc":datetime.now(timezone.utc).isoformat(),
        "source":{"doi":"10.4121/uuid:6e19e9b2-4a2d-40b5-8dd3-a660bf3c0a31",
                  "download_url":URL,"archive_bytes":len(archive),"archive_md5":MD5,
                  "archive_sha256":hashlib.sha256(archive).hexdigest(),
                  "raw_log_sha256":hashlib.sha256(raw).hexdigest(),"original_archive_redistributed":False},
        "raw_rows":len(entries),"herald_window_pass":int(herald.sum()),
        "all_quality_pass":int(keep.sum()),"trials":n,"wins":wins,
        "setting_counts_00_01_10_11":ns,"count_matrix":table,
        "correlations_00_01_10_11":es,"CHSH_S":score,"descriptive_standard_error":se,
        "original_rng_bias_tau":str(tau),"original_conservative_win_bound":str(q),
        "original_bias_adjusted_tail":float(p),"high_precision_tail":exact_decimal,
        "uniform_setting_tail":float(binom.sf(wins-1,n,.75)),
        "local_strategy_count":len(local),"all_local_strategies_obey_bound":True,
        "quantum_calibration":quantum,
        "permutation_control":{"seed":11115000,"resamples":2000,"mean_S":float(np.mean(null_scores)),
           "central95_S":np.quantile(null_scores,[.025,.975]).tolist(),
           "observed_or_greater":sum(s>=score for s in null_scores),
           "role":"descriptive_negative_control_not_primary_Bell_p_value"},
        "independent_physical_experiment":False,"proof_of_full_O0":False,
        "source_code_sha256":hashlib.sha256(Path(__file__).read_bytes()).hexdigest(),
        "python":sys.version,"numpy":np.__version__}
    save(out/"summary.json",result)
    print(json.dumps({k:result[k] for k in ["verdict","raw_rows","trials","wins","CHSH_S","descriptive_standard_error","original_bias_adjusted_tail"]}))


if __name__=="__main__":main()
