"""Frozen2200 human polygons; hard K, candidate face groups and strict-majority overlay."""
from collections import Counter,defaultdict
from contextlib import closing
import hashlib
import json
import math
from pathlib import Path
import shutil

import cv2
import numpy as np
from PIL import Image,ImageDraw
from scipy.cluster.hierarchy import linkage,fcluster
from scipy.spatial.distance import squareform

from .cluster_targets import digest,protected_snapshot
from .mask_model_training_data import PROJECT,read,jsonl,write,require,sha
from .mask_quota_saved_extension import load_formal_evidence,file_inventory
from .mask_template_refined import readonly,snapshot_database
from .mask_quality_recovery import fingerprint,POSE,GALLERY
from .pose_review_geometry import polygon_value,canonical

ROOT=PROJECT/'outputs/pallet_mask_class_correction_v1'
STATE=PROJECT/'outputs/pallet_mask_class_correction_reviews_v1'
NAMESPACE='human2200-face-class-correction-v1'
BATCHES=[('337','pallet_mask_model_requests_v3','pallet_mask_model_reviews_v3'),
    ('145','pallet_mask_annotation_update_requests_v1','pallet_mask_model_reviews_v4'),
    ('9','pallet_mask_annotation_update_requests_v2','pallet_mask_model_reviews_v5'),
    ('150','pallet_mask_quality_override_requests_v1','pallet_mask_model_reviews_v6')]
SETTINGS=dict(algorithm='complete_linkage_same_K_outline_and_image_evidence_candidates_v1',
    max_candidate_distance=1.,outline_rms_scale=.105,edge_turn_sequence_scale=.45,log_aspect_scale=.48,
    appearance_gradient_scale=.68,appearance_weight=1.,duplicate_same_face_bbox_iou=.90,
    physical_type_confirmed=False,reflection_alignment_allowed=False)


def raw_sha(raw):return hashlib.sha256(raw.encode('utf-8')).hexdigest()


def box_iou(a,b):
    area=max(0,min(a['x']+a['width'],b['x']+b['width'])-max(a['x'],b['x']))*max(0,min(a['y']+a['height'],b['y']+b['height'])-max(a['y'],b['y']))
    return area/(a['width']*a['height']+b['width']*b['height']-area) if area else 0.


def vote_units(records):
    # Duplicate bytes + same visible box are one face observation. Different
    # objects in the same image are not collapsed. K never changes in dedup.
    byimage=defaultdict(list);units={}
    for r in sorted(records,key=lambda r:r['target_key']):
        candidates=byimage[r['image_sha256']];unit=None
        for prior in candidates:
            if box_iou(r['row']['bbox'],prior['row']['bbox'])>=SETTINGS['duplicate_same_face_bbox_iou']:
                unit=units[prior['target_key']];break
        units[r['target_key']]=unit or r['target_key'];candidates.append(r)
    return units


def outline(points):
    p=np.asarray(points,float)
    if cv2.contourArea(p.astype(np.float32),oriented=True)<0:p=p[::-1]
    # Small image-plane rotation only, not reflection or arbitrary homography.
    edges=np.roll(p,-1,axis=0)-p;eligible=np.abs(edges[:,0])>2*np.abs(edges[:,1])
    if eligible.any():
        e=edges[np.argmax(np.where(eligible,np.linalg.norm(edges,axis=1),-1.))];angle=math.atan2(e[1],e[0]);angle=(angle+math.pi/2)%math.pi-math.pi/2
        angle=float(np.clip(angle,-math.pi/9,math.pi/9));c,s=math.cos(angle),math.sin(angle);p=p@np.array([[c,-s],[s,c]])
    span=np.maximum(np.ptp(p,axis=0),1e-6);q=(p-p.min(axis=0))/span
    before=q-np.roll(q,1,axis=0);after=np.roll(q,-1,axis=0)-q
    turns=np.arctan2(before[:,0]*after[:,1]-before[:,1]*after[:,0],(before*after).sum(axis=1))/math.pi
    return q,turns,math.log(span[0]/span[1])


def appearance(image,row):
    b=row['bbox'];h,w=image.shape;l=max(0,math.floor(b['x']));t=max(0,math.floor(b['y']));r=min(w,math.ceil(b['x']+b['width']));bt=min(h,math.ceil(b['y']+b['height']))
    crop=cv2.resize(image[t:bt,l:r],(64,24),interpolation=cv2.INTER_AREA);crop=cv2.createCLAHE(clipLimit=2.,tileGridSize=(4,3)).apply(crop)
    gx=cv2.Sobel(crop,cv2.CV_32F,1,0);gy=cv2.Sobel(crop,cv2.CV_32F,0,1);mag=cv2.magnitude(gx,gy)
    descriptor=cv2.resize(mag,(16,6),interpolation=cv2.INTER_AREA).ravel();norm=np.linalg.norm(descriptor)
    return descriptor/max(norm,1e-6)


def distance(a,b):
    qa,ta,aa,va=a;qb,tb,ab,vb=b
    if len(qa)!=len(qb):return float('inf')
    pairs=[]
    for shift in range(len(qb)):
        rms=float(np.sqrt(np.mean((qa-np.roll(qb,shift,axis=0))**2)))
        turn=float(np.mean(np.abs(ta-np.roll(tb,shift))))
        pairs.append(max(rms/SETTINGS['outline_rms_scale'],turn/SETTINGS['edge_turn_sequence_scale']))
    geometry=max(min(pairs),abs(aa-ab)/SETTINGS['log_aspect_scale'])
    visual=float(np.linalg.norm(va-vb))/SETTINGS['appearance_gradient_scale']
    return max(geometry,SETTINGS['appearance_weight']*visual)


def candidate_groups(records,features):
    byk=defaultdict(list);result={}
    for r in records:byk[r['actual_K']].append(r['target_key'])
    for k,keys in sorted(byk.items()):
        keys.sort()
        if len(keys)==1:labels=[1]
        else:
            ds=np.zeros((len(keys),len(keys)))
            for i in range(len(keys)):
                for j in range(i):ds[i,j]=ds[j,i]=distance(features[keys[i]],features[keys[j]])
            labels=fcluster(linkage(squareform(ds,checks=False),method='complete'),t=SETTINGS['max_candidate_distance'],criterion='distance')
        members=defaultdict(list)
        for key,label in zip(keys,labels):members[int(label)].append(key)
        for groupkeys in sorted(members.values(),key=lambda v:min(v)):
            group='F'+str(k)+'-'+sha(sorted(groupkeys))[:10]
            for key in groupkeys:result[key]=group
    return result


def majority_mapping(records,assignments,new_ids=None,anchor_choices=None):
    bykey={r['target_key']:r for r in records}
    require(set(assignments)==set(bykey),'Every2200 target needs exactly one face assignment')
    groups=defaultdict(list);classunits=defaultdict(set);unitgroups=defaultdict(set)
    for key,face in assignments.items():
        r=bykey[key];groups[face].append(r);classunits[r['old_class_id']].add(r['vote_unit']);unitgroups[r['old_class_id'],r['vote_unit']].add(face)
    for face,rs in groups.items():require(len({r['actual_K'] for r in rs})==1,'Different actual K cannot belong to the same face class')
    denom={c:len(units) for c,units in classunits.items()};supports=defaultdict(Counter)
    for (cls,unit),faces in unitgroups.items():
        # Conflicting duplicates share one vote, never inflate either group.
        for face in faces:supports[face][cls]+=1/len(faces)
    descriptions={};new_ids=dict(new_ids or {});anchor_choices=anchor_choices or {}
    next_id=max([22,*new_ids.values()])+1
    for face,rs in sorted(groups.items()):
        votes=supports[face];anchors=[c for c,n in votes.items() if n>denom[c]/2]
        ranked=sorted(anchors,key=lambda c:(-votes[c],-votes[c]/denom[c],c));ties=[];dest=None
        if ranked:
            best=ranked[0];ties=[c for c in ranked if abs(votes[c]-votes[best])<1e-9 and abs(votes[c]/denom[c]-votes[best]/denom[best])<1e-9]
            if len(ties)>1:dest=anchor_choices.get(face);require(dest is None or dest in ties,'Anchor choice must be one of the tied majority classes')
            else:dest=best
        elif face not in new_ids:new_ids[face]=next_id;next_id+=1
        if not ranked:dest=new_ids[face]
        descriptions[face]=dict(face_id=face,K=rs[0]['actual_K'],targets=len(rs),vote_units=len({r['vote_unit'] for r in rs}),
            old_class_counts=dict(Counter(r['old_class_id'] for r in rs)),majority_support={str(c):dict(support=float(n),denominator=denom[c],fraction=float(n/denom[c])) for c,n in sorted(votes.items())},
            majority_anchors=ranked,tied_majority_anchors=ties if len(ties)>1 else [],proposed_class_id=dest,
            decision='majority_tie_pending_human' if ranked and dest is None else 'retain_majority_class' if ranked else 'new_class_candidate',
            physical_face_identity_confirmed=False,representatives=[r['target_key'] for r in sorted(rs,key=lambda r:r['target_key'])[:6]])
    return descriptions,new_ids


def gather():
    from scripts.audit_mask_model_reviews import audit
    formal,rows,_=load_formal_evidence(read(PROJECT/'outputs/pallet_mask_remaining_round5_v1/manifest.json'))
    source=PROJECT/'outputs/pallet_mask_continuation_sources_v18';a=read(source/'audit.json')
    require(fingerprint(POSE)==a['pose_fingerprint'] and fingerprint(GALLERY)==a['gallery_fingerprint'],'Base human source changed; fresh provenance freeze required')
    catalog=jsonl(PROJECT/'outputs/pallet_mask_annotation_update_v2/private/source_catalog.jsonl')
    base=[r for r in catalog if r['batch'] not in ('model-class-keypoints-v3','model-annotation-update-remaining-v1')];require(len(base)==1559,'Original1559 manual sources required')
    records=[];source_paths={str(POSE.relative_to(PROJECT)):fingerprint(POSE),str(GALLERY.relative_to(PROJECT)):fingerprint(GALLERY)};audits={}
    isolated=PROJECT/'outputs/pallet_mask_nonconforming_reviews_v1/mask_reviews.sqlite3';source_paths[str(isolated.relative_to(PROJECT))]=fingerprint(isolated)
    for ref in base:
        key=ref['target_key'];row=rows[key]
        records.append(dict(target_key=key,old_class_id=row['final_class_id'],row=row,polygon=ref['polygon'],source_revision=ref['revision'],
            source_event_raw_sha256=ref['event_raw_sha256'],source_payload_sha256=ref['payload_sha256'],source_batch=ref['batch']))
    for name,task,store in BATCHES:
        taskroot=PROJECT/'outputs'/task;path=PROJECT/'outputs'/store/'mask_reviews.sqlite3';result=audit(taskroot,path);audits[name]=result
        require(result['new_distinct_saved_images']==int(name),'Every issued human target must be genuinely saved')
        props={p['target_key']:p for p in jsonl(taskroot/'proposals.jsonl')};snapshot_database(path,ROOT/'private'/f'reviews_{name}.sqlite3')
        source_paths[str(path.relative_to(PROJECT))]=fingerprint(path)
        with closing(readonly(ROOT/'private'/f'reviews_{name}.sqlite3',True)) as db:
            current=list(db.execute('SELECT target_key,revision,payload FROM reviews'))
        for key,revision,raw in current:
            value=json.loads(raw);row=rows[key];require(props[key]['source']==row,'Current target/class source binding differs')
            records.append(dict(target_key=key,old_class_id=row['final_class_id'],row=row,polygon=value['polygon'],source_revision=revision,
                source_event_raw_sha256=raw_sha(raw),source_payload_sha256=sha(value),source_batch=result['source_namespace']))
    require(len(records)==len({r['target_key'] for r in records})==2200,'Exactly2200 different saved targets')
    for r in records:
        polygon_value(r['polygon'],*r['row']['size']);r['actual_K']=len(r['polygon']);r['image_sha256']=digest(formal/r['row']['image'])
    units=vote_units(records)
    for r in records:r['vote_unit']=units[r['target_key']]
    return formal,records,source_paths,audits


def build():
    require(not ROOT.exists(),'Fresh independent correction version required');ROOT.mkdir(mode=0o700);(ROOT/'private').mkdir(mode=0o700)
    cv2.setNumThreads(1);formal,records,sources,audits=gather();records.sort(key=lambda r:r['target_key'])
    for directory in ('images','crops','overlays'):(ROOT/directory).mkdir()
    features={}
    for r in records:
        key=r['target_key'];image=cv2.imread(str(formal/r['row']['image']),0);require(image is not None,'Original image unavailable')
        features[key]=(*outline(r['polygon']),appearance(image,r['row']))
        shutil.copyfile(formal/r['row']['image'],ROOT/'images'/f'{key}.png')
        pts=np.asarray(r['polygon']);b=r['row']['bbox'];l=max(0,math.floor(min(pts[:,0].min(),b['x'])-8));t=max(0,math.floor(min(pts[:,1].min(),b['y'])-8))
        rr=min(image.shape[1],math.ceil(max(pts[:,0].max(),b['x']+b['width'])+8));bt=min(image.shape[0],math.ceil(max(pts[:,1].max(),b['y']+b['height'])+8))
        crop=Image.fromarray(image[t:bt,l:rr]).convert('RGB');crop.save(ROOT/'crops'/f'{key}.png');draw=ImageDraw.Draw(crop)
        p=[(x-l,y-t) for x,y in r['polygon']];draw.line(p+[p[0]],fill='#55d5ff',width=1)
        for i,(x,y) in enumerate(p,1):draw.ellipse((x-1,y-1,x+1,y+1),fill='#ffbe43');draw.text((x+1,y-8),str(i),fill='#ffbe43')
        crop.save(ROOT/'overlays'/f'{key}.png')
    assignments=candidate_groups(records,features);groups,newids=majority_mapping(records,assignments)
    write(ROOT/'private/records.jsonl',records,lines=True);write(ROOT/'private/source_audits.json',audits)
    write(ROOT/'assignments.json',assignments);write(ROOT/'face_groups.json',groups);write(ROOT/'new_class_registry.json',newids)
    mapping=[dict(target_key=r['target_key'],old_class_id=r['old_class_id'],actual_K=r['actual_K'],face_id=assignments[r['target_key']],
        proposed_class_id=groups[assignments[r['target_key']]]['proposed_class_id'],review_status='pending_human',source_polygon_sha256=sha(r['polygon'])) for r in records]
    write(ROOT/'proposed_mapping.jsonl',mapping,lines=True)
    write(ROOT/'input_manifest.json',dict(scope='2200_latest_saved_human_only',source_fingerprints=sources,formal_protected=protected_snapshot(formal),
        configs=file_inventory(PROJECT/'configs'),source_records_sha256=digest(ROOT/'private/records.jsonl'),settings=SETTINGS,
        old_static_inventories={str(path.relative_to(PROJECT)):file_inventory(path) for path in [PROJECT/'outputs/pallet_mask_annotation_update_v2',
            PROJECT/'outputs/pallet_mask_quality_override_requests_v1',PROJECT/'outputs/pallet_mask_annotation_update_requests_v2']},
        prior_class_standard_not_used_as_actual_K=True,majority_denominator='whole_old_class_frozen_deduplicated_face_observations',
        conflicting_duplicate_faces_share_one_vote=True,grouping_candidate_not_physical_truth=True,automatic_training_or_GT_promotion=False))
    public_records=[dict(target_key=r['target_key'],old_class_id=r['old_class_id'],actual_K=r['actual_K'],source_revision=r['source_revision'],
        image_sha256=r['image_sha256'],vote_unit=r['vote_unit']) for r in records]
    write(ROOT/'targets.json',public_records)
    from .mask_class_correction_review import write_assets
    write_assets(ROOT)
    assets={str(path.relative_to(ROOT)):digest(path) for folder in ('images','crops','overlays') for path in (ROOT/folder).glob('*.png')}
    for name in ('report_zh.html','review.js','report-font.otf','FONT_LICENSE.txt','targets.json','face_groups.json'):assets[name]=digest(ROOT/name)
    write(ROOT/'manifest.json',dict(schema=NAMESPACE,targets=2200,initial_candidate_groups=len(groups),new_class_candidates=len(newids),
        pending_tied_groups=sum(g['decision']=='majority_tie_pending_human' for g in groups.values()),
        proposed_changed_targets=sum(m['proposed_class_id'] is not None and m['old_class_id']!=m['proposed_class_id'] for m in mapping),
        actual_K_histogram=dict(Counter(r['actual_K'] for r in records)),settings=SETTINGS,public_assets=assets,
        source_records_sha256=digest(ROOT/'private/records.jsonl'),input_manifest_sha256=digest(ROOT/'input_manifest.json'),
        initial_assignments_sha256=digest(ROOT/'assignments.json'),proposed_mapping_sha256=digest(ROOT/'proposed_mapping.jsonl'),
        all_proposals_pending_human=True,old_class_ids_not_overwritten=True,remaining_formal_targets_not_reclassified=10144,
        no_model_training=True,no_GT_or_eligibility_promotion=True))
    print({k:v for k,v in read(ROOT/'manifest.json').items() if k not in ('public_assets','settings')},flush=True)
