"""Pixel-only new-taxonomy inference, abstention, supported exactK and quotas."""
from collections import Counter, defaultdict
import json
import time

import cv2
import numpy as np
import torch

from .cluster_targets import digest
from .mask_annotation_full_data import context
from .mask_annotation_full_train import normalize
from .mask_class_keypoints import convert
from .mask_model_improvement import predicted_rle
from .mask_model_training_data import PROJECT, FALSE_FLAGS, require, read, write, jsonl, letterbox, unletterbox, gpu_receipt
from .mask_model_worklist import polygonize
from .mask_quota_saved_extension import load_formal_evidence
from .mask_taxonomy_data import ROOT, FORMAL_SOURCE, sources_current, verify
from .mask_taxonomy_train import CONFIG, RUN, TaxonomyMaskNet, class_tables


def classify(fine, parent, embedding, prototypes, classes, attributes, parent_table):
    """Never replace a missing fine decision with its parent label."""
    fine=np.asarray(fine,float);parent=np.asarray(parent,float)
    order=np.argsort(-fine); top=int(order[0]); second=int(order[1]); cls=classes[top]
    similarities=np.asarray(prototypes)@np.asarray(embedding); prototype=int(similarities.argmax())
    threshold=CONFIG['small_fine_probability_threshold'] if cls['small_sample'] else CONFIG['fine_probability_threshold']
    expected=parent_table[cls['parent_index']]; material=['塑料','木质','金属']
    expected_attrs=[expected['legs']-2,expected['beams']-1,material.index(expected['material'])]
    reasons=[]
    if fine[top]<threshold: reasons.append('fine_confidence_below_uncalibrated_heuristic')
    if fine[top]-fine[second]<CONFIG['fine_margin_threshold']: reasons.append('fine_top2_ambiguous')
    if int(parent.argmax())!=cls['parent_index'] or parent[cls['parent_index']]<CONFIG['parent_probability_threshold']:
        reasons.append('parent_head_disagrees_or_uncertain')
    if attributes!=expected_attrs: reasons.append('attribute_heads_disagree_with_fine_parent')
    if prototype!=top or similarities[top]<CONFIG['prototype_cosine_minimum']:
        reasons.append('training_prototype_disagrees_or_distant')
    return dict(candidate_class_id=cls['class_id'],classified_class_id=None if reasons else cls['class_id'],
        classification_gate_passed=not reasons,classification_withheld_reasons=reasons,
        fine_confidence=float(fine[top]),fine_top2_margin=float(fine[top]-fine[second]),
        fine_candidates=[dict(class_id=classes[int(i)]['class_id'],score=float(fine[i]),K=classes[int(i)]['K'],
            parent=classes[int(i)]['parent']) for i in order[:5]],
        parent_candidate_index=int(parent.argmax()),parent_scores=parent.tolist(),
        parent_confidence=float(parent.max()),fine_scores=fine.tolist(),
        prototype_class_id=classes[prototype]['class_id'],prototype_cosine=float(similarities[top]),
        predicted_attribute_indices=attributes,score_is_calibrated_accuracy_probability=False)


def select_quotas(predictions,classes,quota=100):
    available=defaultdict(list)
    for p in predictions:
        if p['classification_gate_passed'] and p['geometry_gate_passed']: available[p['classified_class_id']].append(p)
    selected=[]; global_images=set(); capacity={c['class_id']:len({p['image_sha256'] for p in available[c['class_id']]}) for c in classes}
    for cls in sorted(classes,key=lambda c:(capacity[c['class_id']],c['class_id'])):
        pending=available[cls['class_id']][:];counts=Counter();class_images=set()
        while len(class_images)<quota:
            options=[p for p in pending if p['image_sha256'] not in global_images]
            if not options:break
            best=min(options,key=lambda p:(counts[p['component']],-p['fine_confidence'],p['target_key']))
            pending.remove(best);selected.append(best);global_images.add(best['image_sha256']);class_images.add(best['image_sha256']);counts[best['component']]+=1
    reports={}
    for cls in classes:
        c=cls['class_id']; chosen=[p for p in selected if p['classified_class_id']==c]
        reports[str(c)]=dict(class_id=c,K=cls['K'],parent=cls['parent'],small_sample=cls['small_sample'],
            requested_new_images=quota,predicted_top1_targets=sum(p['candidate_class_id']==c for p in predictions),
            classification_pass_targets=sum(p['classified_class_id']==c for p in predictions),
            exactK_geometry_pass_distinct_images=capacity[c],selected_new_images=len(chosen),
            gap=quota-len(chosen),selected_components=len({p['component'] for p in chosen}),
            not_filled_with_repeated_or_wrong_class_images=True)
    return selected,reports


def infer():
    require((RUN/'training_complete.json').exists(),'Actual new training must finish before inference')
    require(not (ROOT/'private/predictions.jsonl').exists(),'Preserve immutable inference evidence')
    verify();require(sources_current(),'Source graph/protection changed; do not infer stale tasks')
    gpu=gpu_receipt('m2');torch.set_num_threads(4);cv2.setNumThreads(1)
    checkpoint=RUN/'full_pool/final_ema.pt';checkpoint_sha=digest(checkpoint)
    require(checkpoint_sha==read(RUN/'training_complete.json')['full_pool'],'Full-pool model checkpoint changed')
    model=TaxonomyMaskNet().cuda().eval();weights=torch.load(checkpoint,map_location='cpu',weights_only=True)
    model.load_state_dict(weights['model'],strict=True);del weights
    classes,parents=class_tables();prototypes=read(RUN/'full_pool/prototypes.json')['centers']
    formal,rows,evidence=load_formal_evidence(read(FORMAL_SOURCE)); candidates=jsonl(ROOT/'private/candidates.jsonl')
    started=time.monotonic();predictions=[]
    write(ROOT/'inference_start.json',dict(checkpoint_sha256=checkpoint_sha,gpu=gpu,targets=len(candidates),
        no_formal_old_class_used_for_prediction=True,no_manual_polygon_template_or_ground_truth_as_input=True,
        exactK='boundary_supported_vertices_with_fidelity_checks_no_inserted_padding',config=CONFIG))
    with torch.inference_mode():
        for offset in range(0,len(candidates),CONFIG['batch_size']):
            batch=candidates[offset:offset+CONFIG['batch_size']];images=[];native=[]
            for r in batch:
                row=r['source'];require(digest(formal/row['image'])==r['image_sha256'],'Candidate original image changed')
                roi=context(row['bbox'],row['size']);x1,y1,x2,y2=roi
                grey=evidence.image(row['frame_id'])[y1:y2,x1:x2]
                canvas,_,_,transform=letterbox(grey,None,tuple(CONFIG['input_size']))
                images.append(normalize(canvas));native.append((roi,grey,transform))
            with torch.autocast('cuda',dtype=torch.bfloat16): output=model(torch.stack(images).cuda())
            fine=output['fine'].float().softmax(1).cpu().numpy();parent=output['parent'].float().softmax(1).cpu().numpy()
            embeddings=output['embedding'].float().cpu().numpy()
            attrs=np.stack([output[name].argmax(1).cpu().numpy() for name in ('legs','beams','material')],axis=1)
            masks=output['mask'].float().sigmoid().cpu().numpy()
            for j,r in enumerate(batch):
                roi,grey,transform=native[j];row=r['source'];x1,y1,x2,y2=roi
                classification=classify(fine[j],parent[j],embeddings[j],prototypes,classes,attrs[j].tolist(),parents)
                cls=next(c for c in classes if c['class_id']==classification['candidate_class_id'])
                probability=unletterbox(masks[j],transform); mask=probability>=.5
                quality_reasons=[];polygon=None;conversion=None
                contrast=float(np.quantile(grey,.99)-np.quantile(grey,.01))
                if contrast<8:quality_reasons.append('insufficient_native_image_contrast')
                if mask.sum()<16:quality_reasons.append('empty_or_tiny_model_mask')
                if mask[0].any() or mask[-1].any() or mask[:,0].any() or mask[:,-1].any():quality_reasons.append('model_mask_touches_fixed_ROI_boundary')
                if classification['classification_gate_passed'] and not quality_reasons:
                    dense,details=polygonize(mask)
                    if dense: polygon,conversion=convert(mask,dense,cls['K'],row['bbox']['height'],origin=(x1,y1),native_size=row['size'])
                    else:conversion=dict(reason=details['reason'])
                    if polygon is None:quality_reasons.append(conversion['reason'])
                elif not classification['classification_gate_passed']:quality_reasons.append('fine_class_unresolved_no_classK_claim')
                predictions.append(dict(target_key=r['target_key'],source=row,image_sha256=r['image_sha256'],component=r['component'],
                    pose_payload_raw_sha256=r['pose_payload_raw_sha256'],roi=roi,**classification,
                    requested_K=cls['K'],polygon=polygon,actual_K=len(polygon) if polygon else None,
                    geometry_gate_passed=polygon is not None and not quality_reasons,geometry_withheld_reasons=quality_reasons,
                    conversion=conversion,native_contrast_1_99=contrast,model_mask_rle=predicted_rle(mask),
                    mask_foreground_mean_probability=float(probability[mask].mean()) if mask.any() else None,
                    checkpoint_sha256=checkpoint_sha,template_used=False,quality_override_used=False,**FALSE_FLAGS))
            if offset%512==0:print(dict(inferred=min(offset+len(batch),len(candidates)),total=len(candidates),elapsed_seconds=round(time.monotonic()-started)),flush=True)
    require(sources_current(),'Source/protection changed during inference; no publication')
    write(ROOT/'private/predictions.jsonl',predictions,lines=True)
    selected,reports=select_quotas(predictions,classes)
    write(ROOT/'private/selected.jsonl',selected,lines=True);write(ROOT/'quota_report.json',reports)
    report=dict(state='new_model_remaining_pool_classification_and_strictK_selection_complete',candidate_targets=len(predictions),
        classification_pass_targets=sum(p['classification_gate_passed'] for p in predictions),
        strictK_geometry_pass_targets=sum(p['geometry_gate_passed'] for p in predictions),
        selected_new_distinct_images=len(selected),requested_images=4100,total_gap=4100-len(selected),
        per_class=reports,classification_withheld_reasons=dict(Counter(reason for p in predictions for reason in p['classification_withheld_reasons'])),
        geometry_withheld_reasons=dict(Counter(reason for p in predictions for reason in p['geometry_withheld_reasons'])),
        unresolved_candidates_retained=True,no_old22class_filter=True,no_template_or_quality_override=True,
        checkpoint_sha256=checkpoint_sha,predictions_sha256=digest(ROOT/'private/predictions.jsonl'),
        gpu=gpu,elapsed_seconds=time.monotonic()-started,**FALSE_FLAGS)
    write(ROOT/'inference_complete.json',report)
    print(json.dumps({k:report[k] for k in ('state','candidate_targets','classification_pass_targets','strictK_geometry_pass_targets','selected_new_distinct_images','total_gap')}),flush=True)
