"""Read-only evidence for model-route research; no label export or model training."""
from collections import Counter
from importlib.metadata import version
import json
from pathlib import Path
import sys

import cv2
import numpy as np

PROJECT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(PROJECT))
from pallet.cluster_targets import digest
from pallet.mask_quota_saved_extension import file_inventory
from pallet.pose_review_geometry import canonical, raster_mask

SOURCE = PROJECT/'outputs/pallet_mask_quality_recovery_v2'
OLD_SEG = PROJECT/'outputs/pallet_instance_segmentation_seed_v1'
OUTPUT = PROJECT/'outputs/pallet_mask_model_research_v1'


def read(path):
    return json.loads(Path(path).read_text())


def read_jsonl(path):
    return [json.loads(line) for line in Path(path).read_text().splitlines()]


def quantiles(values):
    return {name: float(np.quantile(values, q)) if len(values) else None
        for name, q in (('min', 0), ('p10', .1), ('median', .5), ('p90', .9), ('max', 1))}


def train_summary(catalog, components):
    """Only formal train annotations are read by the mask/shape analysis."""
    train = [r for r in catalog if r['row']['split'] == 'train']
    heights, widths, edges, areas, thickness, parts = [], [], [], [], [], []
    records = []
    for record in train:
        row = record['row']
        width, height = row['size']
        polygon = np.asarray(record['polygon'], float)
        mask = raster_mask(polygon, width, height).astype(np.uint8)
        edge_lengths = np.linalg.norm(np.roll(polygon, -1, axis=0)-polygon, axis=1)
        distance = cv2.distanceTransform(mask, cv2.DIST_L2, cv2.DIST_MASK_PRECISE)
        # Not a beam-width estimate: twice the in-mask distance is only a
        # resolution diagnostic and is labelled accordingly in the report.
        values = distance[mask > 0]*2
        count = cv2.connectedComponents(mask, connectivity=8)[0]-1
        heights.append(row['bbox']['height']); widths.append(row['bbox']['width'])
        edges.extend(edge_lengths.tolist()); areas.append(int(mask.sum()))
        thickness.extend(values.tolist()); parts.append(count)
        records.append(dict(target_key=record['target_key'], class_id=row['final_class_id'],
            formal_split='train', component=components[record['target_key']],
            development_fold=int(components[record['target_key']][:16], 16) % 3,
            actual_saved_vertex_count=len(polygon), native_size=row['size'],
            bbox_height=row['bbox']['height'], bbox_width=row['bbox']['width'],
            mask_area_native_pixels=int(mask.sum()), shortest_edge_native_pixels=float(edge_lengths.min()),
            raster_connected_parts=count, source_payload_sha256=record['payload_sha256'],
            training_authorized=False, semantic_audit_required=True,
            automatic_ground_truth_eligible=False))
    counts = Counter(len(r['polygon']) for r in train)
    by_class = {}
    for cls in range(1, 23):
        selected = [r for r in train if r['row']['final_class_id'] == cls]
        by_class[str(cls)] = dict(samples=len(selected),
            independent_components=len({components[r['target_key']] for r in selected}),
            groups=len({r['row']['group_id'] for r in selected}),
            vertices=dict(Counter(len(r['polygon']) for r in selected)))
    return dict(samples=len(train), distinct_images=len({r['row']['image'] for r in train}),
        groups=len({r['row']['group_id'] for r in train}),
        independent_components=len({components[r['target_key']] for r in train}),
        vertex_counts=dict(counts), by_class=by_class,
        bbox_height_native_pixels=quantiles(heights), bbox_width_native_pixels=quantiles(widths),
        polygon_edge_length_native_pixels=quantiles(edges), mask_area_native_pixels=quantiles(areas),
        twice_inside_distance_not_beam_width_native_pixels=quantiles(thickness),
        raster_connected_part_counts=dict(Counter(parts)),
        development_folds=dict(Counter(r['development_fold'] for r in records)),
        fixed_topology_keypoints_ready=False, automatic_training_eligibility=False), records


def old_checkpoint_audit(catalog, native_index, dataset_manifest, selection):
    old_train = [row for row in native_index if row['split'] == 'train']
    groups = {r['group_id'] for r in old_train}
    frames = {r['frame_id'] for r in old_train}
    overlap = {}
    for split in ('train', 'val', 'test'):
        selected = [r for r in catalog if r['row']['split'] == split]
        group_matches = [r for r in selected if r['row']['group_id'] in groups]
        frame_matches = [r for r in selected if r['row']['frame_id'] in frames]
        overlap[split] = dict(current_saved_targets_in_old_train_groups=len(group_matches),
            current_saved_targets_in_old_train_frames=len(frame_matches),
            group_matches=[r['target_key'] for r in group_matches],
            frame_matches=[r['target_key'] for r in frame_matches])
    comparisons = [{k: v for k, v in comparison.items() if k != 'calibration'}
        for comparison in selection['comparisons']]
    return dict(actual_supervision=dataset_manifest['split_counts'],
        output_440_is_not_training_sample_count=True, model_selected=selection['model'],
        comparisons=comparisons, existing_validation_was_used_for_selection=True,
        current_formal_overlap=overlap,
        measured_overlap_lower_bound_only=True,
        overlap_scope='exact frame IDs and collection groups; not exhaustive image/PCD/near-duplicate scan',
        old_finetuned_checkpoint_allowed_for_new_independent_evaluation=False,
        reuse_recommendation='reuse audited code path only; use generic pretraining, never this task-finetuned checkpoint')


def geometry_failures(predictions):
    alignment, fit, first_edge, failed_index = Counter(), Counter(), Counter(), Counter()
    angles, residuals = [], []
    for prediction in predictions:
        for attempt in prediction['diagnostics']['attempts']:
            alignment[attempt['alignment']['rejection'] or 'passed'] += 1
            if not attempt.get('native_fit'):
                continue
            diagnostic = attempt['native_fit']
            fit[diagnostic['rejection'] or 'passed'] += 1
            if 'failed_edge' in diagnostic:
                failed_index[diagnostic['failed_edge']] += 1
            for edge in diagnostic.get('edges', []):
                if edge['rejection']:
                    first_edge[edge['rejection']] += 1
                if edge['rejection'] == 'ambiguous_or_nonstraight_boundary':
                    angles.append(edge['angle_degrees']); residuals.append(edge['residual_p90'])
    return dict(targets=len(predictions), alignment=dict(alignment), native_polygon_fit=dict(fit),
        first_failed_edge_reason=dict(first_edge), first_failed_edge_index=dict(failed_index),
        ambiguous_edge_angle_degrees=quantiles(angles), ambiguous_edge_residual_native_pixels=quantiles(residuals),
        counts_are_first_failure_only_not_all_edges=True,
        conclusion='Independent strongest-edge samples lack global line coherence; no case passes all edges and independent-reference consensus.',
        next_development='Score complete line/path hypotheses with multi-edge competition and native image support; keep thresholds and frozen v1 evidence.',
        repeated_use_of_train_oof_is_development_not_fresh_blind_test=True)


def build(source=SOURCE, out=OUTPUT):
    source, out = Path(source).resolve(), Path(out).resolve()
    if source.parent != PROJECT/'outputs' or out.parent != PROJECT/'outputs' or out.exists():
        raise ValueError('Read immutable source and write a fresh research-only directory')
    before = file_inventory(source)
    old_inputs = {str(p.relative_to(PROJECT)): digest(p) for p in (
        OLD_SEG/'dataset_manifest.json', OLD_SEG/'dataset/native_index.json', OLD_SEG/'model_selection.json')}
    result = read(source/'result.json')
    if result['state'] != 'completed_offline_not_published':
        raise ValueError('Completed offline evidence required')
    catalog = read_jsonl(source/'private/source_catalog.jsonl')
    components = read(source/'private/leakage_components.json')['by_target']
    summary, records = train_summary(catalog, components)
    old = old_checkpoint_audit(catalog, read(OLD_SEG/'dataset/native_index.json'),
        read(OLD_SEG/'dataset_manifest.json'), read(OLD_SEG/'model_selection.json'))
    diagnosis = geometry_failures(read_jsonl(source/'offline_predictions.jsonl'))
    out.mkdir(mode=0o700)
    output = dict(state='research_only_no_model_training_or_label_export',
        source_result_sha256=digest(source/'result.json'), source_inputs_sha256=before,
        old_experiment_input_sha256=old_inputs,
        corpus_split_counts=dict(Counter(r['row']['split'] for r in catalog)),
        train_only_feasibility=summary, old_experiment=old, geometry_failure_diagnosis=diagnosis,
        recommended_first_experiment='class_agnostic_ROI_segmentation_with_high_resolution_decoder',
        recommended_second_experiment='fixed_semantic_keypoints_only_after_surface_and_topology_mapping',
        recommended_combination='mask_probability_plus_boundary_and_keypoint_supported_native_polygon_fit',
        environment={name: version(name) for name in ('torch', 'torchvision', 'ultralytics', 'rfdetr')},
        pose_or_ruler_used_as_input=False, new_split_or_GT_created=False,
        frozen_OOF_reuse_scope='development only; not new blind evaluation',
        pending_semantic_audit=['visible_fork_face_scope', 'long_short_side', 'open_apertures',
            'closed_holes_if_actually_annotated', 'occlusion', 'stacked_rear_structures', 'reflection'],
        production_annotation_writes=0, training_processes_launched=0, checkpoints_loaded_or_downloaded=0,
        permission_to_start_training=False)
    if before != file_inventory(source) or any(digest(PROJECT/p) != h for p, h in old_inputs.items()):
        raise ValueError('Read-only research input changed')
    output['read_only_inputs_unchanged'] = True
    (out/'feasibility.json').write_bytes(canonical(output)+b'\n')
    (out/'train_reference_inventory.jsonl').write_bytes(b''.join(canonical(r)+b'\n' for r in records))
    receipt = dict(state='passed_research_generation_only_not_model_quality',
        code_sha256=digest(__file__), files=file_inventory(out),
        corpus_split_counts=output['corpus_split_counts'], production_writes=0, training_started=False)
    (out/'manifest.json').write_bytes(canonical(receipt)+b'\n')
    print(json.dumps(dict(state=output['state'], splits=output['corpus_split_counts'],
        train_independent_components=summary['independent_components'],
        old_checkpoint_blocked_for_independent_evaluation=True)), flush=True)
    return output


if __name__ == '__main__':
    import argparse
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument('--source', type=Path, default=SOURCE)
    parser.add_argument('--output', type=Path, default=OUTPUT)
    args = parser.parse_args()
    build(args.source, args.output)
