"""Partial image-description labels: class structure != visible state; blank note != normal."""
from collections import Counter,defaultdict
import json

from .mask_model_training_data import read,write,jsonl,sha,require
from .pallet_description_data import ROOT,verify
from .cluster_targets import digest

PROMPT='请只描述图中指定托盘，输出JSON：{"支腿":整数或null,"横梁":整数或null,"材质":"塑料/木质/金属/无法判断","结构":"简短结构描述","观察":"可见的遮挡、超出视野或图像质量；无法判断时写无法判断"}。不要把遮挡或出视野说成损坏，不推测不可见部位。'


def structure(info):
    return info['attributes_text'].split('注：')[0].split('注:')[0].strip(' ，,。')


def note_fields(note):
    # Preserve exact human text. These tags are indexes, not invented new captions.
    result=[]
    if '遮挡' in note:result.append('occlusion')
    if '视野' in note or '视角' in note:result.append('out_of_view_or_viewpoint')
    if '质量' in note:result.append('low_quality')
    if '损坏' in note or '断裂' in note:result.append('damage_explicitly_mentioned')
    return result


def prepare():
    out=ROOT/'description_data';require(not out.exists(),'Preserve previous description dataset');_,records=verify();out.mkdir(mode=0o700)
    classes=read(ROOT/'classes.json');parents=read(ROOT/'parents.json');notes={r['target_key']:r['note'].strip() for r in jsonl(ROOT/'private/accepted_cycle2.jsonl') if r['note'].strip()}
    result=[]
    for r in records:
        info=classes[str(r['class_id'])];parent=parents[info['parent']];note=notes.get(r['target_key'])
        target={'支腿':parent['legs'],'横梁':parent['beams'],'材质':parent['material'],'结构':structure(info),'观察':note if note else None}
        result.append(dict(target_key=r['target_key'],class_id=r['class_id'],image=r['image'],source=r['source'],image_sha256=r['image_sha256'],component=r['component'],
            split=r['evaluation_branch_split'],prompt=PROMPT,target=target,observation_supervised=bool(note),
            observation_tags=note_fields(note or ''),label_source='confirmed_fine_class_definition_plus_exact_human_note',
            missing_note_is_normal=False,class_structure_not_per_image_visibility=True,original_source_payload_sha256=r['source_payload_sha256']))
    write(out/'records.jsonl',result,lines=True)
    # Deterministic component/class-balanced bounded LoRA pilot; never invent additional images.
    selected=[]
    for split in ('train','holdout'):
        for cls in sorted({r['class_id'] for r in result}):
            options=[r for r in result if r['split']==split and r['class_id']==cls];counts=Counter()
            cap=32 if split=='train' else 8
            chosen=sorted([r for r in options if r['observation_supervised']],key=lambda r:r['target_key'])
            for r in chosen:counts[r['component']]+=1
            pending=[r for r in options if r not in chosen]
            while pending and len(chosen)<cap:
                best=min(pending,key=lambda r:(counts[r['component']],sha(['vlm-pilot-v1',r['target_key']])));pending.remove(best);chosen.append(best);counts[best['component']]+=1
            selected.extend(chosen)
    write(out/'pilot.jsonl',selected,lines=True)
    report=dict(state='partial_description_supervision_frozen',targets=len(result),human_observation_targets=len(notes),
        observation_split_counts=dict(Counter(r['split'] for r in result if r['observation_supervised'])),
        pilot_targets=len(selected),pilot_splits=dict(Counter(r['split'] for r in selected)),
        blank_observation_loss_masked=True,no_observation_negative_labels_inferred=True,no_paraphrase_count_inflation=True,
        class_structure_labels_are_weak_for_visual_visibility=True,prompt_sha256=sha(PROMPT),source_manifest_sha256=digest(ROOT/'dataset_manifest.json'))
    write(out/'manifest.json',dict(**report,records_sha256=digest(out/'records.jsonl'),pilot_sha256=digest(out/'pilot.jsonl'),code_sha256=digest(__file__)))
    print(report,flush=True)


def parse_prediction(raw):
    try:
        value=json.loads(raw[raw.index('{'):raw.rindex('}')+1])
    except (ValueError,TypeError):return dict(valid=False,fields=None,reason='invalid_JSON',raw=raw)
    if not isinstance(value,dict):return dict(valid=False,fields=None,reason='not_object',raw=raw)
    valid=(value.get('支腿') in (None,2,3,4) and value.get('横梁') in (None,1,2)
        and value.get('材质') in ('塑料','木质','金属','无法判断') and isinstance(value.get('结构'),str) and isinstance(value.get('观察'),(str,type(None))))
    return dict(valid=valid,fields=value if valid else None,reason=None if valid else 'schema_mismatch',raw=raw)


if __name__=='__main__':prepare()
