"""Read-only, stratified native-VLM experiment, separate from production labeling."""
from collections import Counter
import json
from pathlib import Path

import numpy as np
from PIL import Image,ImageDraw

from .cluster_targets import digest
from .mask_model_training_data import PROJECT,read,write,jsonl,sha,require

ROOT=PROJECT/'outputs/pallet_vlm_native_intensity_v1'
SOURCE=PROJECT/'outputs/pallet_description_round3_training_v1'
SEED='native-intensity-understanding-v1-20261105'
PROMPTS={
    'scene': '请用简短中文描述图中可见的主要物体、它们的大致位置和空间关系，以及图像的视觉特征。不确定的物体请明确说不确定；不要猜测不可见内容。不需要JSON。',
    'crop': '请用简短中文描述图中中心主要物体的外形、组成部分和可见状态。请区分直接看见的证据与不确定的猜测，不要补全不可见部位。不需要JSON。',
    'structure': '这是一幅传感器强度图的目标区域。请依据图像判断是否有托盘、可见的竖向支腿和主横梁数、材质及可见状态。看不清的项目用null，不能由亮暗直接确定材质，不要把遮挡或出视野当损坏。仅输出JSON，字段为：托盘（true/false/null）、支腿（整数/null）、横梁（整数/null）、材质（塑料/木质/金属/null）、遮挡（true/false/null）、出视野（true/false/null）、质量差（true/false/null）、依据（简短中文字符串）。',
}


def select(records,per_class=2):
    """Freeze before seeing outputs; unique original images, separate note enrichment."""
    selected=[];used={r['image_sha256'] for r in records if r['observation_supervised']}
    classes={r['class_id'] for r in records}
    # Reserve rare-class images first; a scene can contain targets from several classes.
    order=sorted(classes,key=lambda cls:(len({r['image_sha256'] for r in records if r['class_id']==cls and not r['observation_supervised']}),cls))
    for cls in order:
        options=[r for r in records if r['class_id']==cls and not r['observation_supervised']]
        components=set()
        for _ in range(per_class):
            pending=[r for r in options if r['image_sha256'] not in used]
            require(pending,'Not enough independent images for fine class '+str(cls))
            best=min(pending,key=lambda r:(r['component'] in components,sha([SEED,r['target_key']])))
            selected.append((best,'fine_class_stratum'));used.add(best['image_sha256']);components.add(best['component'])
    selected.sort(key=lambda pair:(pair[0]['class_id'],sha([SEED,pair[0]['target_key']])))
    used={r['image_sha256'] for r,_ in selected}
    for r in sorted(records,key=lambda r:r['target_key']):
        if not r['observation_supervised']:continue
        require(r['image_sha256'] not in used,'Repeated original in observation enrichment')
        selected.append((r,'all_known_human_observations'));used.add(r['image_sha256'])
    return selected


def parse_structure(raw):
    """Strict field scoring, never infer values from class templates or free text."""
    try:
        value=json.loads(raw[raw.index('{'):raw.rindex('}')+1])
    except (ValueError,TypeError):return dict(valid=False,fields=None,reason='invalid_JSON')
    if not isinstance(value,dict):return dict(valid=False,fields=None,reason='not_object')
    boolkeys=('托盘','遮挡','出视野','质量差');intkeys=('支腿','横梁')
    valid=(all(k in value and (value[k] is None or type(value[k]) is bool) for k in boolkeys)
        and all(k in value and (value[k] is None or type(value[k]) is int and 0<=value[k]<=20) for k in intkeys)
        and '材质' in value and value['材质'] in (None,'塑料','木质','金属') and isinstance(value.get('依据'),str))
    return dict(valid=valid,fields=value if valid else None,reason=None if valid else 'schema_mismatch')


def prepare():
    ROOT.mkdir(mode=0o700,exist_ok=True);out=ROOT/'sample';require(not out.exists(),'Preserve frozen sample')
    out.mkdir(mode=0o700)
    for name in ('images','crops','controls','contact_sheets'):(out/name).mkdir(mode=0o700)
    records=jsonl(SOURCE/'description_data/records.jsonl');training={r['target_key']:r for r in jsonl(SOURCE/'private/training.jsonl')}
    formal=Path(read(PROJECT/'configs/active_formal_dataset.json')['dataset']);classes=read(SOURCE/'classes.json')
    selected=select(records);result=[];tasks=[]
    for i,(r,stratum) in enumerate(selected,1):
        key=r['target_key'];t=training[key];source=formal/r['source']['image'];require(digest(source)==r['image_sha256'],'Original intensity image changed')
        image=Image.open(source).convert('RGB');crop=image.crop(t['roi']);original_crop=Image.open(SOURCE/r['image']).convert('RGB')
        require(np.array_equal(np.asarray(crop),np.asarray(original_crop)),'Frozen ROI differs from original pixels')
        sample_id=f'S{i:03d}';paths={}
        for kind,im in (('images',image),('crops',crop)):
            p=out/kind/(sample_id+'.png');im.save(p);p.chmod(0o600);paths[kind]=str(p.relative_to(ROOT))
        result.append(dict(sample_id=sample_id,target_key=key,stratum=stratum,class_id=r['class_id'],parent=classes[str(r['class_id'])]['parent'],
            attributes_text=classes[str(r['class_id'])]['attributes_text'],component=r['component'],image_sha256=r['image_sha256'],
            source_image=str(source.relative_to(PROJECT)),roi=t['roi'],reference=r['target'],observation_supervised=r['observation_supervised'],
            observation_tags=r['observation_tags'],paths=paths,reference_is_class_definition_not_per_image_visibility=True,
            missing_note_is_normal=False))
        for kind in PROMPTS:
            path=paths['images' if kind=='scene' else 'crops']
            tasks.append(dict(task_id=sample_id+'_'+kind,sample_id=sample_id,kind=kind,image=path,image_sha256=digest(ROOT/path),
                prompt=PROMPTS[kind],control=None,max_new_tokens=192 if kind=='structure' else 160))
    # One image from each parent: controls quantify prompting bias, not real negative accuracy.
    parents=set()
    for r in result:
        if r['parent'] in parents:continue
        parents.add(r['parent']);im=Image.open(ROOT/r['paths']['crops']);a=np.asarray(im)
        variants={'blank':np.zeros_like(a),'shuffled':np.random.default_rng(int(sha([SEED,r['sample_id']])[:8],16)).permutation(a.reshape(-1,3)).reshape(a.shape)}
        for control,a in variants.items():
            p=out/'controls'/(r['sample_id']+'_'+control+'.png');Image.fromarray(a).save(p);p.chmod(0o600)
            for kind in ('crop','structure'):
                tasks.append(dict(task_id=r['sample_id']+'_'+control+'_'+kind,sample_id=r['sample_id'],kind=kind,image=str(p.relative_to(ROOT)),
                    image_sha256=digest(p),prompt=PROMPTS[kind],control=control,max_new_tokens=192 if kind=='structure' else 160))
    write(out/'references.jsonl',result,lines=True);write(out/'tasks.jsonl',tasks,lines=True);write(out/'prompts.json',PROMPTS)
    # Contact-sheet labels live outside model input files.
    for offset in range(0,len(result),16):
        sheet=Image.new('RGB',(1200,4*230),'#eeeeee');draw=ImageDraw.Draw(sheet)
        for j,r in enumerate(result[offset:offset+16]):
            x=j%4*300;y=j//4*230;im=Image.open(ROOT/r['paths']['images']);im.thumbnail((290,145));sheet.paste(im,(x+(300-im.width)//2,y+20))
            im=Image.open(ROOT/r['paths']['crops']);im.thumbnail((290,60));sheet.paste(im,(x+(300-im.width)//2,y+165));draw.text((x+5,y+2),r['sample_id']+' / Class'+str(r['class_id']),fill='black')
        sheet.save(out/'contact_sheets'/f'{offset//16+1:02d}.png')
    counts=Counter(r['class_id'] for r in result)
    report=dict(state='native_sample_and_prompts_frozen_before_inference',sample_targets=len(result),unique_original_images=len({r['image_sha256'] for r in result}),
        fine_classes=len(counts),parent_groups=len(parents),per_fine_class=dict(sorted(counts.items())),known_observation_targets=sum(r['observation_supervised'] for r in result),
        positive_observation_tag_counts=dict(Counter(t for r in result for t in r['observation_tags'])),inference_tasks=len(tasks),
        real_tasks=sum(t['control'] is None for t in tasks),control_tasks=sum(t['control'] is not None for t in tasks),
        selection_seed=SEED,selection_independent_of_model_outputs=True,no_class_or_reference_in_model_input=True,
        native_grey_pixels_repeated_to_RGB=True,no_contrast_enhancement=True,no_adapter=True,no_finetuning=True,
        full_scene_and_crop_independent_conversations=True,unknown_not_default_normal_for_evaluation=True,
        unresolved_classes_excluded=[37,47],reference_caveat='class definitions are weak visible-structure references; absent observation is unlabeled, not negative',
        files={str(p.relative_to(out)):digest(p) for p in out.rglob('*') if p.is_file()},
        source_files={str(p.relative_to(PROJECT)):digest(p) for p in (SOURCE/'description_data/records.jsonl',SOURCE/'classes.json',SOURCE/'private/training.jsonl')},code_sha256=digest(__file__))
    write(out/'manifest.json',report);print({k:v for k,v in report.items() if k not in ('files','source_files')},flush=True)
