"""Candidate calibration/validation spots for the void plan step 1a (PROVISIONAL guesses for the user to confirm).
Samples points around outer-winding cells of the I5 flattened grid (q >= 45, interior z), measures local CT and m7
surface statistics at L2 (chunked reads), and picks spots per category; writes spots.tsv (L0 coords) + contact sheets.
"""
import numpy as np, zarr, tifffile, json, os
from PIL import Image, ImageDraw
OUT = os.path.dirname(os.path.abspath(__file__))
V = '/media/djosey/nvme4/spiral_0332/view/sheet_I5_v2union25k'
ct = zarr.open('/media/djosey/nvme4/20251211183505-2.399um-0.2m-78keV-masked.zarr', mode='r')['2']
sp = zarr.open('/media/djosey/nvme4/20251211183505-surface-20260413222639-surface-m7-L2-th0.2.zarr', mode='r')
sp = sp['0'] if hasattr(sp, 'keys') and '0' in sp else sp
d = np.load(f'{V}/I5_v2union25k_fiber_value_1102.npz'); q = d['q_median']; z0, z1 = d['z_range']
X, Y, Z = (tifffile.imread(f'{V}/I5_v2union25k_flat.tifxyz/{c}.tif') for c in 'xyz')
rng = np.random.default_rng(1102)
ok = (Z > 0) & np.isfinite(q) & (q >= 45) & (Z >= z0 + 800) & (Z < z1 - 800)
r, c = np.nonzero(ok); k = rng.choice(len(r), 2500, replace=False)
H = 13; g = np.mgrid[-H:H + 1, -H:H + 1, -H:H + 1]; dist = np.sqrt((g ** 2).sum(0))
rows = json.load(open(f'{OUT}/samples.json')) if os.path.exists(f'{OUT}/samples.json') else []
for i in ([] if rows else k):
    j = rng.normal(0, 12, 3)   # jitter so spots are not all on the fitted sheet
    x, y, z = int(X[r[i], c[i]] + j[0]), int(Y[r[i], c[i]] + j[1]), int(Z[r[i], c[i]] + j[2])
    a = np.asarray(ct[z - H:z + H + 1, y - H:y + H + 1, x - H:x + H + 1]).astype(float)
    s = np.asarray(sp[z - H:z + H + 1, y - H:y + H + 1, x - H:x + H + 1]) > 0
    if a.shape != dist.shape: continue
    b12, b3 = dist <= 12, dist <= 3
    rows.append(dict(x=x, y=y, z=z, q=float(q[r[i], c[i]]), m7_r3=bool(s[b3].any()), m7_r12=bool(s[b12].any()),
                     m7_frac12=float(s[b12].mean()), ct_c=float(a[b3].mean()), ct_p95=float(np.percentile(a[b12], 95)),
                     zero12=float((a[b12] == 0).mean())))
print('sampled', len(rows)); json.dump(rows, open(f'{OUT}/samples.json', 'w'))
import sys
if os.environ.get('DUMP_ONLY'): sys.exit(0)
R = rows
# thresholds from the sampled distributions (samples.json): papyrus at m7 points has ct_c median 95, p95 ~160;
# air (no m7 within 12) ct_c ~39, p95 ~44
cats = {
  'papyrus_thin':   [t for t in R if t['m7_r3'] and t['ct_c'] < 70 and t['zero12'] < 0.05],
  'papyrus_m7miss': [t for t in R if not t['m7_r3'] and t['ct_c'] > 100 and t['zero12'] < 0.05],
  'air':            [t for t in R if not t['m7_r12'] and t['ct_p95'] < 60 and t['zero12'] < 0.05],
  'gap_between':    [t for t in R if t['m7_r12'] and not t['m7_r3'] and t['ct_c'] < 50 and t['zero12'] < 0.05],
  'mask_edge':      [t for t in R if 0.05 < t['zero12'] < 0.95 and not t['m7_r3']],
}
want = {'papyrus_thin': (14, 10), 'papyrus_m7miss': (6, 6), 'air': (20, 12), 'gap_between': (0, 6), 'mask_edge': (0, 6)}
guess = {'papyrus_thin': 'papyrus', 'papyrus_m7miss': 'papyrus?', 'air': 'air', 'gap_between': 'air', 'mask_edge': 'air?'}
picked = []
for cat, lst in cats.items():
    print(cat, len(lst))
    nc, nv = want[cat]; idx = rng.permutation(len(lst))[:nc + nv]
    for n, ii in enumerate(idx):
        t = dict(lst[ii]); t.update(cat=cat, set='cal' if n < nc else 'val', guess=guess[cat]); picked.append(t)
# blind ids: shuffle so category is not readable from the id
order = rng.permutation(len(picked)); picked = [picked[i] for i in order]
for n, t in enumerate(picked): t['id'] = n + 1
with open(f'{OUT}/spots.tsv', 'w') as fh:
    keys = ['id', 'set', 'guess', 'cat', 'x_L0', 'y_L0', 'z_L0', 'q', 'm7_r3', 'm7_frac12', 'ct_c', 'ct_p95', 'zero12']
    fh.write('\t'.join(keys) + '\n')
    for t in picked:
        t.update(x_L0=t['x'] * 4, y_L0=t['y'] * 4, z_L0=t['z'] * 4)
        fh.write('\t'.join(str(round(t[k], 3) if isinstance(t[k], float) else t[k]) for k in keys) + '\n')
# contact sheets: per spot XY / XZ / YZ crops, 64 L2 vx half-size, shown 3x
W = 64; S = 3
def crop(t):
    x, y, z = t['x'], t['y'], t['z']
    xy = np.asarray(ct[z, y - W:y + W, x - W:x + W]); xz = np.asarray(ct[z - W:z + W, y, x - W:x + W]); yz = np.asarray(ct[z - W:z + W, y - W:y + W, x])
    tiles = []
    for im in (xy, xz, yz):
        im = np.pad(im, ((0, 2 * W - im.shape[0]), (0, 2 * W - im.shape[1])))
        p = Image.fromarray(im).resize((2 * W * S, 2 * W * S), Image.NEAREST).convert('RGB'); dr = ImageDraw.Draw(p)
        m = W * S; dr.ellipse([m - 3 * S * 3, m - 3 * S * 3, m + 3 * S * 3, m + 3 * S * 3], outline=(255, 60, 60), width=2)
        tiles.append(p)
    row = Image.new('RGB', (3 * 2 * W * S + 40, 2 * W * S + 26), (20, 20, 20))
    for n, p in enumerate(tiles): row.paste(p, (n * (2 * W * S + 20), 26))
    ImageDraw.Draw(row).text((4, 4), f"#{t['id']}  ({t['x_L0']}, {t['y_L0']}, {t['z_L0']})  {('my guess: ' + t['guess']) if t['set'] == 'cal' else 'label: papyrus / air / unsure'}   [XY | XZ | YZ, red ring = r 3 L2 vx, crops 128 L2 vx]", fill=(255, 255, 255))
    return row
per = 8
for pg in range(0, len(picked), per):
    rs = [crop(t) for t in picked[pg:pg + per]]
    sheet = Image.new('RGB', (rs[0].width, sum(x.height for x in rs)), (0, 0, 0)); yy = 0
    for x in rs: sheet.paste(x, (0, yy)); yy += x.height
    sheet.save(f'{OUT}/spots_{pg // per + 1:02d}.png')
print('picked', len(picked), 'sheets', (len(picked) + per - 1) // per)
