"""M1 high-resolution U-Net and M2 RF-DETR-Seg Small research workers.

Only newly prepared, globally isolated formal-train data are accepted. No old
task checkpoint, production writer, official val/test prediction or pose input.
"""
from __future__ import annotations

from collections import Counter, defaultdict
import gc
import hashlib
import importlib.metadata
import json
import math
import os
from pathlib import Path
import random
import re
import time
import traceback
import urllib.request

import cv2
import numpy as np
from scipy import ndimage
import torch
from torch import nn
from torch.nn import functional as F
from torch.utils.data import Dataset, DataLoader

from .mask_model_training_data import (
    PROJECT, FALSE_FLAGS, atomic_json, digest, disk_budget, gpu_receipt, jsonl,
    letterbox, read, require, sha, unletterbox, verify_dataset, verify_sources, write,
)

SEED = 42
MAX_EPOCHS = 80
PATIENCE = 15


def seed_all():
    random.seed(SEED)
    np.random.seed(SEED)
    torch.manual_seed(SEED)
    torch.cuda.manual_seed_all(SEED)
    torch.backends.cudnn.benchmark = False


def generic_weights(output, route):
    """Download only the fixed official generic asset, checking published hash.

    No search through task outputs or ambiguous local checkpoint caches.
    """
    directory = output/'generic_pretraining'
    directory.mkdir(mode=0o700, exist_ok=True)
    if route == 'm1':
        from torchvision.models import ResNet18_Weights
        url = ResNet18_Weights.IMAGENET1K_V1.url
        name = 'resnet18-f37072fd.pth'
        expected_md5 = None
    else:
        from rfdetr.assets.model_weights import ModelWeights
        asset = ModelWeights.from_filename('rf-detr-seg-small.pt')
        url, name, expected_md5 = asset.url, 'rf-detr-seg-small.pt', asset.md5_hash
    path = directory/name
    receipt_path = directory/(route+'_provenance.json')
    if not path.exists():
        temporary = path.with_suffix(path.suffix+'.download')
        with urllib.request.urlopen(url, timeout=120) as response, temporary.open('xb') as stream:
            while block := response.read(4*1024*1024):
                stream.write(block)
        temporary.chmod(0o600)
        os.replace(temporary, path)
    actual = digest(path)
    if route == 'm1':
        require(actual.startswith('f37072fd'), 'Official torchvision SHA256 prefix mismatch')
    else:
        md5 = hashlib.md5(path.read_bytes()).hexdigest()
        require(md5 == expected_md5, 'Official RF-DETR generic published MD5 mismatch')
    receipt = dict(route=route, url=url, filename=name, sha256=actual,
        published_md5=expected_md5, official_sha256_prefix='f37072fd' if route == 'm1' else None,
        generic_pretraining_only=True, old_pallet_task_weights_used=False,
        task_finetuning_started_from_this_file=True,
        license_note='torchvision ResNet/ImageNet or RF-DETR generic segmentation asset; '
                     'upstream model/data terms require separate deployment review')
    if receipt_path.exists():
        require(read(receipt_path) == receipt, 'Generic provenance changed')
    else:
        write(receipt_path, receipt)
    return path, receipt


class ConvBlock(nn.Sequential):
    def __init__(self, cin, cout):
        super().__init__(nn.Conv2d(cin, cout, 3, padding=1, bias=False),
                         nn.GroupNorm(8, cout), nn.ReLU(inplace=True),
                         nn.Conv2d(cout, cout, 3, padding=1, bias=False),
                         nn.GroupNorm(8, cout), nn.ReLU(inplace=True))


class ROIMaskBoundaryUNet(nn.Module):
    """Generic ResNet18 encoder, /32..full-resolution skip decoder, two heads."""
    def __init__(self, encoder_weights=None):
        super().__init__()
        from torchvision.models import resnet18
        self.encoder = resnet18(weights=None)
        if encoder_weights is not None:
            self.encoder.load_state_dict(torch.load(encoder_weights, map_location='cpu', weights_only=True))
        self.encoder.fc = nn.Identity()
        self.raw = ConvBlock(3, 16)
        self.up4 = ConvBlock(512+256, 128)
        self.up3 = ConvBlock(128+128, 64)
        self.up2 = ConvBlock(64+64, 48)
        self.up1 = ConvBlock(48+64, 32)
        self.up0 = ConvBlock(32+16, 24)
        self.mask_head = nn.Conv2d(24, 1, 1)
        self.boundary_head = nn.Conv2d(24, 1, 1)

    @staticmethod
    def join(up, skip):
        return torch.cat((F.interpolate(up, size=skip.shape[-2:], mode='bilinear', align_corners=False), skip), 1)

    def forward(self, x):
        raw = self.raw(x)
        stem = self.encoder.relu(self.encoder.bn1(self.encoder.conv1(x)))
        a = self.encoder.layer1(self.encoder.maxpool(stem))
        b = self.encoder.layer2(a)
        c = self.encoder.layer3(b)
        d = self.encoder.layer4(c)
        y = self.up4(self.join(d, c))
        y = self.up3(self.join(y, b))
        y = self.up2(self.join(y, a))
        y = self.up1(self.join(y, stem))
        y = self.up0(self.join(y, raw))
        return self.mask_head(y), self.boundary_head(y)


class ROIDataset(Dataset):
    def __init__(self, output, records, split):
        self.items = []
        for record in records:
            if record['development_split'] != split:
                continue
            image = cv2.imread(str(output/record['image']), cv2.IMREAD_GRAYSCALE)
            mask = cv2.imread(str(output/record['native_mask']), cv2.IMREAD_GRAYSCALE) > 0
            grey, label, valid, transform = letterbox(image, mask)
            rgb = np.repeat(grey[None].astype(np.float32)/255, 3, axis=0)
            rgb = (rgb-np.array([.485, .456, .406], np.float32)[:, None, None]) / np.array([.229, .224, .225], np.float32)[:, None, None]
            boundary = label-cv2.erode(label, np.ones((3, 3), np.uint8), borderType=cv2.BORDER_CONSTANT, borderValue=0)
            self.items.append((torch.from_numpy(rgb), torch.from_numpy(label[None].astype(np.float32)),
                               torch.from_numpy(boundary[None].astype(np.float32)),
                               torch.from_numpy(valid[None].astype(np.float32)), record, transform, mask))

    def __len__(self):
        return len(self.items)

    def __getitem__(self, index):
        # No IDs/classes/surfaces/poses/groups become tensors, even accidentally.
        return self.items[index][:4]


def masked_segmentation_loss(mask_logits, boundary_logits, target, boundary, valid):
    count = valid.sum().clamp_min(1)
    bce = (F.binary_cross_entropy_with_logits(mask_logits, target, reduction='none')*valid).sum()/count
    probability = mask_logits.sigmoid()*valid
    dice = 1-(2*(probability*target).sum((1, 2, 3))+1)/((probability+target*valid).sum((1, 2, 3))+1)
    edge = (F.binary_cross_entropy_with_logits(boundary_logits, boundary, reduction='none',
                                              pos_weight=boundary_logits.new_tensor(4.))*valid).sum()/count
    return bce+dice.mean()+.25*edge


def native_metrics(prediction, target):
    """Native pixel-center masks, symmetric HD95 and 1px boundary F1.

    Empty predictions remain in the denominator with IoU/F1=0; distance is null
    (not an invented finite good value). Empty accepted-set quality is null.
    """
    a, b = np.asarray(prediction, bool), np.asarray(target, bool)
    require(a.shape == b.shape and b.any(), 'Invalid native evaluation target')
    intersection, union = np.count_nonzero(a & b), np.count_nonzero(a | b)
    if not a.any():
        return dict(iou=0., boundary_f1_1px=0., hd95_native_pixels=None,
                    assd_native_pixels=None, empty_prediction=True, predicted_to_reference_area_ratio=0.)
    edge_a = a & ~ndimage.binary_erosion(a, np.ones((3, 3), bool), border_value=0)
    edge_b = b & ~ndimage.binary_erosion(b, np.ones((3, 3), bool), border_value=0)
    left = ndimage.distance_transform_edt(~edge_b)[edge_a]
    right = ndimage.distance_transform_edt(~edge_a)[edge_b]
    precision, recall = float(np.mean(left <= 1)), float(np.mean(right <= 1))
    values = np.r_[left, right]
    return dict(iou=float(intersection/union), boundary_f1_1px=2*precision*recall/max(precision+recall, 1e-12),
                hd95_native_pixels=float(np.quantile(values, .95)), assd_native_pixels=float(values.mean()),
                empty_prediction=False, predicted_to_reference_area_ratio=float(a.sum()/b.sum()))


def summarize(records):
    records = list(records)
    result = dict(count=len(records), empty_predictions=sum(r['empty_prediction'] for r in records))
    for field in ('iou', 'boundary_f1_1px', 'hd95_native_pixels', 'assd_native_pixels'):
        values = [r[field] for r in records if r[field] is not None]
        result[field+'_median'] = float(np.median(values)) if values else None
        result[field+'_defined_count'] = len(values)
    result['iou_p10'] = float(np.quantile([r['iou'] for r in records], .1)) if records else None
    result['fraction_iou_at_least_090'] = sum(r['iou'] >= .9 for r in records)/len(records) if records else None
    return result


def grouped_bootstrap(records, repetitions=500):
    groups = defaultdict(list)
    for record in records:
        groups[record['component']].append(record['iou'])
    if not groups:
        return None
    keys, rng = sorted(groups), np.random.default_rng(SEED)
    values = [np.median([v for index in rng.integers(0, len(keys), len(keys)) for v in groups[keys[index]]])
              for _ in range(repetitions)]
    return dict(iou_median_95pct_ci=[float(np.quantile(values, q)) for q in (.025, .975)],
                independent_components=len(keys), repetitions=repetitions)


def result_record(record, metrics, confidence, seconds):
    return dict(target_key=record['target_key'], component=record['component'],
        class_id=record['class_id'], policy_id=record['policy_id'], surface=record['surface'],
        structure=record['structure'], formal_split='train', development_split='dev',
        bbox_height=record['original_bbox']['height'], bbox_height_band=(
            'under32' if record['original_bbox']['height'] < 32 else '32to48' if record['original_bbox']['height'] < 48 else 'over48'),
        confidence=float(confidence), inference_seconds=float(seconds), **metrics, **FALSE_FLAGS)


def pilot_goal(records):
    records = list(records)
    summary = summarize(records)
    return (len({r['component'] for r in records}) >= 10 and summary['iou_median'] >= .95
            and summary['fraction_iou_at_least_090'] >= .9
            and all(r['hd95_native_pixels'] is not None and r['hd95_native_pixels'] <= max(2, .1*r['bbox_height'])
                    for r in records))


def quality_report(out, records, threshold_search=None):
    """Calibration uses only the internal development labels, never formal val/test."""
    confidence_options = [0., .5, .7, .8, .9, .95]
    choices = []
    for cutoff in confidence_options:
        accepted = [r for r in records if not r['empty_prediction'] and r['confidence'] >= cutoff]
        choices.append(dict(cutoff=cutoff, accepted=len(accepted),
                            internal_pilot_goal_met=pilot_goal(accepted), quality=summarize(accepted)))
    passing = [r for r in choices if r['internal_pilot_goal_met']]
    cutoff = min((r['cutoff'] for r in passing), default=None)
    accepted = [] if cutoff is None else [r for r in records if not r['empty_prediction'] and r['confidence'] >= cutoff]
    report = dict(scope='formal-train internal grouped development, not independent blind test',
        all=summarize(records), accepted=summarize(accepted), all_target_denominator=len(records),
        accepted_count=len(accepted), delivery_rate=len(accepted)/len(records) if records else 0.,
        rejection_rate=1-len(accepted)/len(records) if records else 0.,
        quality_gate='blocked' if cutoff is None else 'internal_numeric_goal_only_semantic_trial_still_required',
        quality_filter_confidence_cutoff=cutoff, internal_only_calibration=choices,
        mask_threshold_search_internal_only=threshold_search,
        grouped_bootstrap=grouped_bootstrap(records),
        by_class={str(c): summarize(r for r in records if r['class_id'] == c) for c in range(1, 23)},
        strata={field: {str(v): summarize(r for r in records if r[field] == v)
                         for v in sorted({r[field] for r in records})}
                for field in ('policy_id', 'surface', 'structure', 'bbox_height_band')},
        inference_seconds_median=float(np.median([r['inference_seconds'] for r in records])) if records else None,
        formal_val_test_prediction_or_score_count=0, deployment_authorized=False, **FALSE_FLAGS,
        limitations=['Internal dev is used for early stopping/calibration; not an unbiased generalization estimate.',
            'No automatic claim of correct surface/layer/topology from pixel metrics.',
            'Unknown unboxed adjacent faces may remain despite conservative bbox and visual exclusion.',
            'Single-ring supervision does not establish unannotated closed-hole ground truth.',
            'Rare classes/groups may have zero development coverage, explicitly reported.',
            'Upper-beam/foot/inner-corner semantic errors and human correction time need a later authorized trial.',
            'No official val/test evaluation before model/postprocessing/threshold freeze.'])
    write(out/'internal_development_predictions.jsonl', records, lines=True)
    write(out/'internal_quality_report.json', report)
    return report


@torch.no_grad()
def m1_predictions(model, dataset, threshold=.5):
    model.eval()
    records, probabilities = [], []
    for image, _, _, _, record, transform, target in dataset.items:
        torch.cuda.synchronize()
        start = time.monotonic()
        logits, _ = model(image[None].to('cuda:0'))
        probability = unletterbox(logits.sigmoid()[0, 0].cpu().numpy(), transform)
        torch.cuda.synchronize()
        duration = time.monotonic()-start
        mask = probability >= threshold
        confidence = float(probability[mask].mean()) if mask.any() else 0.
        records.append(result_record(record, native_metrics(mask, target), confidence, duration))
        probabilities.append((record, probability, target, duration))
    return records, probabilities


def atomic_torch(path, payload):
    path = Path(path)
    temporary = path.with_name(path.name+'.atomic.tmp')
    require(not temporary.exists(), 'Stale checkpoint temporary file: '+str(temporary))
    torch.save(payload, temporary)
    temporary.chmod(0o600)
    os.replace(temporary, path)


def profile_m1(out, weight_path, dataset):
    attempts = []
    for batch in (8, 4, 2, 1):
        seed_all()
        model = ROIMaskBoundaryUNet(weight_path).to('cuda:0').train()
        optimizer = torch.optim.AdamW(model.parameters(), lr=3e-4, weight_decay=1e-4)
        torch.cuda.reset_peak_memory_stats()
        started = time.monotonic()
        try:
            samples = [dataset[i % len(dataset)] for i in range(batch)]
            inputs = [torch.stack([sample[j] for sample in samples]).to('cuda:0') for j in range(4)]
            mask, boundary = model(inputs[0])
            loss = masked_segmentation_loss(mask, boundary, *inputs[1:])
            loss.backward()
            optimizer.step()
            torch.cuda.synchronize()
            attempts.append(dict(batch_size=batch, state='actual_forward_backward_optimizer_succeeded',
                seconds=time.monotonic()-started, peak_bytes=torch.cuda.max_memory_allocated()))
            del model, optimizer, inputs, samples, loss, mask, boundary
            gc.collect(); torch.cuda.empty_cache()
            write(out/'memory_profile.json', dict(attempts=attempts, selected_batch=batch,
                profile_updates_discarded=True, fresh_generic_initialization_for_real_training=True))
            return batch
        except torch.cuda.OutOfMemoryError:
            attempts.append(dict(batch_size=batch, state='actual_cuda_oom', peak_bytes=torch.cuda.max_memory_allocated()))
            del model, optimizer
            gc.collect(); torch.cuda.empty_cache()
    write(out/'memory_profile.json', dict(attempts=attempts, selected_batch=None))
    raise RuntimeError('M1 OOM even at physical batch 1; training blocked')


def train_m1(output, out, records):
    weights, provenance = generic_weights(output, 'm1')
    train, dev = ROIDataset(output, records, 'train'), ROIDataset(output, records, 'dev')
    batch = profile_m1(out, weights, train)
    seed_all()
    model = ROIMaskBoundaryUNet(weights).to('cuda:0')
    # Freeze pretrained BN running statistics on the small research corpus, not encoder weights.
    optimizer = torch.optim.AdamW([
        dict(params=model.encoder.parameters(), lr=3e-5),
        dict(params=[p for name, p in model.named_parameters() if not name.startswith('encoder.')], lr=3e-4)],
        weight_decay=1e-4)
    generator = torch.Generator().manual_seed(SEED)
    loader = DataLoader(train, batch_size=batch, shuffle=True, num_workers=0, generator=generator)
    best, stale, best_epoch, history = -1., 0, 0, []
    torch.cuda.reset_peak_memory_stats()
    for epoch in range(1, MAX_EPOCHS+1):
        disk_budget(output)
        model.train()
        for module in model.encoder.modules():
            if isinstance(module, nn.BatchNorm2d):
                module.eval()
        losses, started = [], time.monotonic()
        for image, target, edge, valid in loader:
            image, target, edge, valid = (t.to('cuda:0') for t in (image, target, edge, valid))
            optimizer.zero_grad(set_to_none=True)
            mask_logits, boundary_logits = model(image)
            loss = masked_segmentation_loss(mask_logits, boundary_logits, target, edge, valid)
            require(torch.isfinite(loss).item(), 'Nonfinite M1 loss')
            loss.backward()
            nn.utils.clip_grad_norm_(model.parameters(), 5.)
            optimizer.step()
            losses.append(float(loss.detach().cpu()))
        prediction_records, _ = m1_predictions(model, dev)
        score = summarize(prediction_records)['iou_median']
        improved = score > best+.001
        if improved:
            best, stale, best_epoch = score, 0, epoch
            atomic_torch(out/'best.pt', dict(model=model.state_dict(), epoch=epoch,
                development_iou_median=score, generic_pretraining_sha256=provenance['sha256']))
        else:
            stale += 1
        entry = dict(epoch=epoch, train_loss_mean=float(np.mean(losses)),
                     internal_dev=summarize(prediction_records), best_epoch=best_epoch,
                     seconds=time.monotonic()-started, peak_cuda_bytes=torch.cuda.max_memory_allocated())
        write(out/'epochs'/f'{epoch:04d}.json', entry)
        history.append(entry)
        atomic_torch(out/'last.pt', dict(model=model.state_dict(), optimizer=optimizer.state_dict(),
            epoch=epoch, best_epoch=best_epoch, seed=SEED, stale_epochs=stale,
            torch_rng=torch.get_rng_state(), cuda_rng=torch.cuda.get_rng_state()))
        print(json.dumps(dict(route='m1', **entry)), flush=True)
        if stale >= PATIENCE:
            break
    del optimizer
    model.load_state_dict(torch.load(out/'best.pt', map_location='cpu', weights_only=True)['model'])
    _, probabilities = m1_predictions(model, dev)
    search = []
    candidates = {}
    for threshold in (.35, .45, .5, .55, .65):
        selected = []
        for record, probability, target, duration in probabilities:
            mask = probability >= threshold
            confidence = float(probability[mask].mean()) if mask.any() else 0.
            selected.append(result_record(record, native_metrics(mask, target), confidence, duration))
        candidates[threshold] = selected
        search.append(dict(mask_threshold=threshold, quality=summarize(selected)))
    # Internal-only fixed finite search; prefer .5 on tied medians.
    threshold = max(candidates, key=lambda t: (summarize(candidates[t])['iou_median'], -abs(t-.5)))
    report = quality_report(out, candidates[threshold], search)
    write(out/'frozen_selection.json', dict(checkpoint='best.pt', sha256=digest(out/'best.pt'),
        epoch=best_epoch, selection='internal grouped dev native median IoU at .5, min_delta .001',
        calibrated_mask_threshold=threshold, confidence_cutoff=report['quality_filter_confidence_cutoff'],
        official_val_test_evaluated=False, model_frozen_before_any_future_official_evaluation=True))
    return dict(epochs_trained=len(history), best_epoch=best_epoch, checkpoint='best.pt',
                checkpoint_sha256=digest(out/'best.pt'), internal_quality=report['all'], physical_batch=batch)


def train_m2(output, out, records):
    from .mask_model_training_rfdetr import train_rfdetr
    weights, provenance = generic_weights(output, 'm2')
    return train_rfdetr(output, out, records, weights, provenance)


def train_worker(output, route, run_name=None):
    output = Path(output).resolve()
    require(output.parent == PROJECT/'outputs', 'Research outputs must remain private under outputs')
    require(importlib.metadata.version('torch').startswith('2.6.0'), 'Pinned torch dependency changed')
    require(importlib.metadata.version('torchvision').startswith('0.21.0'), 'Pinned torchvision dependency changed')
    require(importlib.metadata.version('rfdetr') == '1.11.0', 'Pinned rfdetr dependency changed')
    gpu = gpu_receipt(route)
    disk_budget(output)
    manifest, records = verify_dataset(output)
    _, _, _, protection = verify_sources()
    torch.set_num_threads(4)
    cv2.setNumThreads(1)
    require(torch.cuda.is_available() and torch.cuda.device_count() == 1, 'One authorized CUDA GPU required')
    torch.cuda.set_device(0)
    name = run_name or route
    require(re.fullmatch(route+r'(?:_retry[1-9][0-9]*)?', name) is not None,
            'Run name must be route or an explicit fresh route_retryN')
    out = output/name
    out.mkdir(mode=0o700)
    (out/'epochs').mkdir(mode=0o700)
    (out/'code_snapshot').mkdir(mode=0o700)
    for path in sorted((PROJECT/'pallet').glob('mask_model_training*.py')):
        destination = out/'code_snapshot'/path.name
        with destination.open('xb') as stream:
            stream.write(path.read_bytes())
        destination.chmod(0o600)
    write(out/'worker_start.json', dict(state='worker_started_pretraining_or_training_not_yet_complete',
        route=route, gpu=gpu, dataset_manifest_sha256=digest(output/'dataset_manifest.json'),
        splits=manifest['splits'], independent_components=manifest['independent_components'],
        protected_formal_files=protection['protected_formal_files'],
        live_snapshot_fingerprints_verified=True,
        dependencies={name: importlib.metadata.version(name) for name in
                      ('torch', 'torchvision', 'rfdetr', 'numpy', 'pytorch-lightning')},
        code_sha256={str(path.relative_to(PROJECT)): digest(path) for path in
                     sorted((PROJECT/'pallet').glob('mask_model_training*.py'))},
        seed=SEED, max_epochs=MAX_EPOCHS, patience=PATIENCE,
        official_val_test_prediction_count=0, **FALSE_FLAGS))
    started = time.monotonic()
    try:
        result = train_m1(output, out, records) if route == 'm1' else train_m2(output, out, records)
        verify_dataset(output)
        write(out/'completion.json', dict(state='completed_internal_research_training_only', route=route,
            elapsed_seconds=time.monotonic()-started, peak_cuda_memory_bytes=torch.cuda.max_memory_allocated(),
            gpu=gpu, **result, **FALSE_FLAGS, production_writes=0, official_val_test_prediction_count=0))
        print(json.dumps(dict(route=route, state='completed', **result)), flush=True)
    except BaseException as error:
        write(out/'failure.json', dict(state='blocked_or_failed_not_complete', route=route,
            error_type=type(error).__name__, error=str(error), traceback=traceback.format_exc(),
            elapsed_seconds=time.monotonic()-started))
        raise
