"""New namespace review bundles; existing human labels are baseline, not overwritten."""
from collections import Counter,defaultdict
from copy import deepcopy
import json
import shutil
import types

import numpy as np
from PIL import Image

from .cluster_targets import digest
from .mask_model_training_data import PROJECT,FALSE_FLAGS,read,write,jsonl,require,sha
from .pallet_description_round4_data import ROOT,PREVIOUS,verify,sources_current
from .pallet_description_round4_mask import RUN as MASK_RUN,CONFIG,learning,class_tables
from .pallet_description_round4_vlm import RUN as VLM_RUN,parse
from .pallet_description_build import description_draft
from .pallet_description_review import validate_description
from .pallet_description_simple import ROOT as SIMPLE_ROOT,POLICY
from .pallet_description_single import REVIEWER_POLICY
from .pallet_description_ui import assets
from .mask_taxonomy_review import draw_case
from .pose_review_geometry import polygon_value

RELEASE=SIMPLE_ROOT/'round4_v1'
REVIEW_SETS={mode:dict(root=PROJECT/f'outputs/pallet_description_round4_{mode}_v1',reviews=PROJECT/f'outputs/pallet_description_round4_{mode}_reviews_v1',
    managed=PROJECT/f'outputs/pallet_description_round4_{mode}_managed_v1',namespace=f'pallet-description-round4-{mode}-v1',port=port)
    for mode,port in (('new',3416),('existing',3417))}


def bind_review_sets():
    from . import pallet_description_review as review
    # Rebind the serving module only. The freeze module holds the original SETS
    # object for round3 source discovery and must not observe new review DBs.
    review.SETS=deepcopy(REVIEW_SETS)


def infer_masks():
    """Adapt only class count/report constants, freezing exact inference source bytes."""
    require(not (ROOT/'inference_implementation.json').exists(),'Preserve inference attempt')
    folder=ROOT/'inference_code_snapshot';folder.mkdir(mode=0o700);modules=[]
    for name in ('mask_taxonomy_infer','mask_taxonomy_release'):
        path=PROJECT/'pallet'/f'{name}.py';source=path.read_text().replace('4100','4000')
        frozen=folder/f'{name}_round4.py';frozen.write_text(source)
        m=types.ModuleType('pallet._round4_'+name);m.__file__=str(path);exec(compile(source,str(path),'exec'),m.__dict__)
        m.ROOT=ROOT;m.RUN=MASK_RUN;m.CONFIG=CONFIG;m.verify=verify;m.sources_current=sources_current;m.class_tables=class_tables
        modules.append(m)
    module,release=modules;module.TaxonomyMaskNet=learning.TaxonomyMaskNet;release.select_quotas=module.select_quotas
    write(ROOT/'inference_implementation.json',dict(original_source_sha256={n:digest(PROJECT/'pallet'/f'{n}.py') for n in ('mask_taxonomy_infer','mask_taxonomy_release')},
        frozen_source_sha256={p.name:digest(p) for p in folder.iterdir()},adaptation='dynamic40_class_model_and4100_to4000_quota_constants_only',
        checkpoint_sha256=digest(MASK_RUN/'full_pool/final_ema.pt')))
    module.infer();release.select_release()


def semantic_selection():
    require(not (ROOT/'private/semantic_review_selected.jsonl').exists(),'Preserve selection')
    records=jsonl(ROOT/'private/training.jsonl');semantic={r['target_key']:r for r in jsonl(ROOT/'description_data/records.jsonl')};reasons=defaultdict(set)
    for key,r in semantic.items():
        if r['semantic_conflicts']:reasons[key].add('结构选项／文字／类别定义冲突，需明确完整与可见结构口径')
    for p in jsonl(VLM_RUN/'component_branch/holdout_predictions.jsonl'):
        r=semantic[p['target_key']]
        if not p['valid']:reasons[p['target_key']].add('独立分支VLM完整JSON格式失败')
        elif any(r['supervised_fields'][k] and p['fields'][k]!=r['target'][k] for k in ('支腿','横梁','材质')):
            reasons[p['target_key']].add('独立分支VLM与现有结构字段不一致，仅为复核线索')
        if r['observation_supervised'] and (not p['valid'] or not p['fields']['观察']):reasons[p['target_key']].add('已有观察文字但VLM未给出有效观察，不自动判错')
    for p in jsonl(MASK_RUN/'independent_branch/holdout_predictions.jsonl'):
        if p['predicted_fine_index']!=p['fine_index']:reasons[p['target_key']].add('独立分支分类与当前人工类别不一致')
        if p['iou']<.85:reasons[p['target_key']].add('独立分支mask与当前轮廓IoU低于0.85，仅为复核线索')
    # Small per-class inspection panel is explicitly existing data, never counted as new images.
    for cls in sorted({r['class_id'] for r in records}):
        options=sorted([r for r in records if r['class_id']==cls],key=lambda r:sha(['round4-semantic-audit',r['target_key']]))
        for r in options[:10]:reasons[r['target_key']].add('逐细类语义抽检（已有图，不计新增配额）')
    selected=[dict(**r,review_reasons=sorted(reasons[r['target_key']])) for r in records if reasons[r['target_key']]]
    write(ROOT/'private/semantic_review_selected.jsonl',selected,lines=True)
    write(ROOT/'semantic_review_selection.json',dict(targets=len(selected),reason_counts=dict(Counter(reason for r in selected for reason in r['review_reasons'])),
        existing_human_baselines_preserved=True,model_disagreement_is_not_error_GT=True,no_existing_images_counted_as_new=True))


def build(mode):
    settings=REVIEW_SETS[mode];out=settings['root'];require(not out.exists(),'Preserve existing review bundle');require(sources_current(),'Sources changed before build')
    require((MASK_RUN/'training_complete.json').exists() and (VLM_RUN/'training_complete.json').exists(),'Both actual trainings required')
    out.mkdir(mode=0o700);(out/'cases').mkdir();(out/'examples').mkdir();classes=read(ROOT/'classes.json');training=jsonl(ROOT/'private/training.jsonl')
    selected=jsonl(ROOT/('private/release_selected.jsonl' if mode=='new' else 'private/semantic_review_selected.jsonl'))
    descriptions={p['target_key']:p for p in jsonl(ROOT/f'descriptions_{"new" if mode=="new" else "semantic"}.jsonl')}
    formal=PROJECT/'outputs/pallet_dataset_formal_v1/dataset';mask_sha=digest(MASK_RUN/'full_pool/final_ema.pt');adapter_sha=digest(VLM_RUN/'full_pool/adapter/adapter_model.safetensors')
    examples={}
    for c in classes.values():
        if not c['train_enabled']:continue
        members=sorted([r for r in training if r['class_id']==c['class_id']],key=lambda r:r['target_key']);chosen=[]
        for r in members:
            if not chosen or r['component']!=chosen[0]['component']:chosen.append(r)
            if len(chosen)==2:break
        if len(chosen)<2:chosen=members[:2]
        examples[str(c['class_id'])]=[]
        for i,r in enumerate(chosen):
            path=f'examples/class{c["class_id"]}_{i}.png';draw_case(Image.open(formal/r['source']['image']).convert('RGB'),r['polygon'],r['roi']).save(out/path)
            examples[str(c['class_id'])].append(dict(path=path,target_key=r['target_key'],K=c['K'],source_polygon_sha256=sha(r['polygon']),source_human_revision=r['source_revision']))
    proposals=[];images={}
    for r in selected:
        key=r['target_key'];cls=r['classified_class_id'] if mode=='new' else r['class_id'];info=classes[str(cls)];pred=descriptions[key]
        require(pred['adapter_sha256']==adapter_sha and pred['image_sha256']==r['image_sha256'],'VLM image/model binding mismatch')
        d=description_draft(pred) if mode=='new' else deepcopy(r['description']);validate_description(d)
        classification={k:r[k] for k in ('fine_candidates','fine_confidence','fine_top2_margin','parent_confidence','prototype_class_id','prototype_cosine')} if mode=='new' else dict(fine_candidates=[],fine_confidence=0.,fine_top2_margin=0.,parent_confidence=0.,prototype_class_id=None,prototype_cosine=0.)
        p=dict(schema=settings['namespace'],target_key=key,class_id=cls,K=info['K'],parent=info['parent'],polygon=r['polygon'],source=r['source'],roi=r['roi'],
            image_sha256=r['image_sha256'],pose_payload_raw_sha256=r.get('pose_payload_raw_sha256'),classification=classification,class_graph_revision=5,
            geometry_provenance=r.get('conversion') if mode=='new' else dict(kind='latest_prior_human_geometry_preserved',source_revision=r['source_revision'],source_payload_sha256=r['source_payload_sha256']),
            checkpoint_sha256=mask_sha,original_formal_class_id=r['source']['final_class_id'],geometry_previously_completed=mode!='new',
            initial_geometry_status='machine_draft' if mode=='new' else 'human_completed',description=d,description_raw=pred['raw'],description_parse_valid=pred['valid'],
            description_structure_recovered=pred['structure_recovered'],description_observation_withheld=pred['observation_withheld'],description_model_sha256=adapter_sha,
            description_initial_status='machine_draft' if mode=='new' else 'prior_human_baseline_preserved_model_suggestion_read_only',human_note=r.get('original_human_note','') if mode!='new' else '',
            prior_description=deepcopy(r['description']) if mode!='new' else None,review_reasons=r.get('review_reasons',[]),
            prior_review_source=dict(mode=r['mode'],action=r['original_review_action'],revision=r['source_revision'],payload_sha256=r['source_payload_sha256']) if mode!='new' else None,
            template_used=False,quality_override_used=False,propagation_seed_eligible=False,**FALSE_FLAGS)
        proposals.append(p);images[f'images/{key}.png']=dict(formal_relative_image=r['source']['image'],sha256=r['image_sha256'])
        draw_case(Image.open(formal/r['source']['image']).convert('RGB'),r['polygon'],r['roi']).save(out/'cases'/f'{key}.png')
    write(out/'proposals.jsonl',proposals,lines=True);write(out/'classes.json',classes);write(out/'examples.json',examples)
    quotas=read(ROOT/'release_quota_report.json') if mode=='new' else {str(c['class_id']):dict(selected_new_images=sum(p['class_id']==c['class_id'] for p in proposals),gap=0,existing_semantic_targets_not_new_quota=True) for c in classes.values() if c['train_enabled']}
    write(out/'quota_report.json',quotas)
    inference=read(ROOT/'inference_complete.json')
    write(out/'summary.json',dict(mode=mode,targets=len(proposals),requested_images=4000 if mode=='new' else len(proposals),gap=sum(q['gap'] for q in quotas.values()),
        candidate_targets=inference['candidate_targets'],unresolved_classification_targets=inference['candidate_targets']-inference['classification_pass_targets'],
        independent_metrics=read(MASK_RUN/'independent_branch/holdout_metrics.json'),vlm_metrics=read(VLM_RUN/'component_branch/holdout_metrics.json'),
        all_model_descriptions_require_human_review=True,prior_human_description_not_overwritten=mode!='new'))
    rawpred=jsonl(ROOT/'private/predictions.jsonl');write(out/'unresolved_examples.json',[{k:p[k] for k in ('target_key','candidate_class_id','fine_candidates','classification_withheld_reasons')} for p in rawpred if not p['classification_gate_passed']][:100])
    write(out/'description_drafts.jsonl',[dict(target_key=p['target_key'],description=p['description'],model_raw=p['description_raw'],prior_description=p['prior_description'],review_reasons=p['review_reasons'],machine_suggestion_not_human_complete=True) for p in proposals],lines=True)
    ui=assets(mode);oldprefix=f'pallet-description-round3-{mode}-v1:'
    require(ui['review.js'].count(oldprefix)==1,'Unexpected local draft namespace anchor');ui['review.js']=ui['review.js'].replace(oldprefix,settings['namespace']+':')
    ui['review.js']=ui['review.js'].replace("+'\\n当前规则：异常描述为空按正常处理。';", "+'\\n上轮描述基线：'+JSON.stringify(value.proposal.prior_description||null)+'\\n定向复核原因：'+(value.proposal.review_reasons||[]).join('；')+'\\n当前规则：异常描述为空按正常处理。';")
    require('定向复核原因：' in ui['review.js'],'Review reasons missing from evidence panel')
    ui['review.js']=ui['review.js'].replace("$('modelInfo').textContent=`", "$('modelInfo').textContent=(value.proposal.review_reasons?.length?'本图复核原因：'+value.proposal.review_reasons.join('；')+'。 ':'')+`",1)
    ui['page.html']=ui['page.html'].replace('第三轮 ·','第四轮 ·').replace('已有数据 · 描述补标与联合','第四轮 · 定向语义与联合')
    if mode!='new':ui['page.html']=ui['page.html'].replace('恢复本图模型描述（不覆盖历史）','恢复本轮初始描述（保留人工基线）').replace('<p id="modelInfo"></p>','<p id="modelInfo"></p><p>本轮保留上轮描述、类别和轮廓作为初始值。新版模型建议只读显示，不自动覆盖人工内容；仅对需要修正的内容编辑保存。</p>')
    for name,text in ui.items():(out/name).write_text(text)
    from scripts.build_mask_remaining_direct_requests import build_font
    build_font(out,ui['page.html']+ui['review.js']+json.dumps(classes,ensure_ascii=False)+json.dumps(proposals,ensure_ascii=False))
    manifest=dict(schema=settings['namespace'],mode=mode,request_count=len(proposals),proposals_sha256=digest(out/'proposals.jsonl'),original_images=images,
        public_assets={str(p.relative_to(out)):digest(p) for p in out.rglob('*') if p.is_file() and p.name!='proposals.jsonl'},
        dataset_manifest_sha256=digest(ROOT/'dataset_manifest.json'),source_binding_sha256=digest(ROOT/'source_binding.json'),mask_checkpoint_sha256=mask_sha,
        vlm_adapter_sha256=adapter_sha,description_predictions_sha256=digest(ROOT/f'descriptions_{"new" if mode=="new" else "semantic"}.jsonl'),code_sha256=digest(__file__),**FALSE_FLAGS)
    write(out/'manifest.json',manifest)
    overlay=RELEASE/mode;overlay.mkdir(mode=0o700,parents=True)
    names=('page.html','review.js','review.css','report-font.otf','description_drafts.jsonl')
    for name in names:shutil.copyfile(out/name,overlay/name)
    write(overlay/'manifest.json',dict(task_manifest_sha256=digest(out/'manifest.json'),policy=POLICY,batch_structure_enabled=True,batch_attributes_enabled=True,reviewer_policy=REVIEWER_POLICY,
        assets={n:digest(overlay/n) for n in names},round4_draft_namespace=settings['namespace']))
    verify_bundle(mode)


def verify_bundle(mode):
    s=REVIEW_SETS[mode];out=s['root'];m=read(out/'manifest.json');props=jsonl(out/'proposals.jsonl');classes=read(ROOT/'classes.json')
    baseline={r['target_key']:r for r in jsonl(ROOT/'private/training.jsonl')};blocked=set(read(ROOT/'private/protection.json')['protected_image_hashes'])
    predictions={p['target_key']:p for p in jsonl(ROOT/'private/predictions.jsonl')};descriptions={p['target_key']:p for p in jsonl(ROOT/f'descriptions_{"new" if mode=="new" else "semantic"}.jsonl')};seen=set()
    require(len(props)==len({p['target_key'] for p in props})==m['request_count'],'Repeated/missing targets')
    for p in props:
        info=classes[str(p['class_id'])];polygon_value(p['polygon'],*p['source']['size']);require(info['train_enabled'] and len(p['polygon'])==info['K']==p['K'],'Wrong canonical classK')
        pred=descriptions[p['target_key']];require(parse(pred['raw'])['valid']==pred['valid'] and p['description_raw']==pred['raw'],'VLM raw parse does not replay')
        if mode=='new':
            q=predictions[p['target_key']];require(p['image_sha256'] not in blocked|seen and q['classification_gate_passed'] and q['geometry_gate_passed'],'Old image or rejected prediction')
            require(q['polygon']==p['polygon'] and q['classified_class_id']==p['class_id'] and q['conversion']['inserted_or_collinear_padding_points']==0,'Wrong predicted class/contour or K padding')
            from pycocotools import mask as mu
            from .mask_class_keypoints import mask_fidelity,corner_geometry
            rle=q['model_mask_rle'];mask=mu.decode(dict(size=rle['size'],counts=rle['counts'].encode())).astype(bool)
            f=mask_fidelity(p['polygon'],mask,origin=tuple(q['roi'][:2]),native_size=p['source']['size']);g=corner_geometry(p['polygon'])
            require(f['iou']>=.9 and f['hd95']<=max(2.,.1*q['source']['bbox']['height']) and g['minimum_abs_turn_degrees']>=18-1e-5 and g['minimum_edge_length']>=1.5-1e-5 and g['minimum_corner_deviation']>=.55-1e-5,'Unsupported exactK geometry')
        else:
            r=baseline[p['target_key']];require(p['class_id']==r['class_id'] and p['polygon']==r['polygon'] and p['description']==r['description'],'Prior human baseline changed')
        seen.add(p['image_sha256'])
    require(sources_current(),'Live sources changed during bundle build')
    write(out/'verification.json',dict(state='passed_joint_description_source_K_and_no_old_image_reuse',mode=mode,manifest_sha256=digest(out/'manifest.json'),
        targets=len(props),unique_images=len(seen),counts=dict(Counter(p['class_id'] for p in props)),old_annotations_unchanged=True,
        new_images_strictK_no_padding=True if mode=='new' else None,prior_human_baselines_preserved=mode!='new',
        new_machine_suggestions_not_GT=True,code_sha256=digest(__file__)))


if __name__=='__main__':
    import argparse
    p=argparse.ArgumentParser();p.add_argument('phase',choices=('infer-masks','select-semantic','build-new','build-existing'));a=p.parse_args()
    if a.phase=='infer-masks':infer_masks()
    elif a.phase=='select-semantic':semantic_selection()
    else:build(a.phase.split('-')[1])
