"""Threshold segmentation and horizontal / vertical projection diagnostics.

No learned features, directory names, appearance-group labels or filled masks
enter these decisions. Unknowns and intermediate numeric evidence are explicit.
"""
from __future__ import annotations

from collections import Counter
import math

import cv2
import numpy as np

from pallet.cluster_targets import integer_box


def runs(binary):
    binary = np.asarray(binary, dtype=bool)
    if binary.ndim != 1:
        raise ValueError('A projection must be one-dimensional')
    transitions = np.diff(np.r_[False, binary, False].astype(np.int8))
    return list(zip(np.flatnonzero(transitions == 1).tolist(), np.flatnonzero(transitions == -1).tolist()))


def robust_slope(mask):
    h, w = mask.shape
    xs = np.flatnonzero(mask.any(axis=0))
    xs = xs[(xs >= .06*w) & (xs <= .94*w)]
    if len(xs) < max(12, .6*w):
        return 0., False
    ys = np.argmax(mask[:, xs], axis=0).astype(float)
    keep = np.ones(len(xs), bool)
    for _ in range(4):
        if keep.sum() < .5*w:
            return 0., False
        a, b = np.polyfit(xs[keep], ys[keep], 1)
        residual = np.abs(ys-(a*xs+b))
        keep = residual <= max(1.5, 2.5*np.median(residual[keep]))
    return float(a), bool(np.median(residual[keep]) <= max(1.5, .06*h))


def deskew(mask, slope):
    """Vertical nearest-pixel shear; invalid samples remain invalid, not background."""
    h, w = mask.shape
    yy, xx = np.indices((h, w))
    source_y = np.rint(yy+slope*(xx-(w-1)/2)).astype(int)
    valid = (source_y >= 0) & (source_y < h)
    result = np.zeros_like(mask, dtype=bool)
    result[valid] = mask[source_y[valid], xx[valid]]
    return result, valid


def clean_mask(mask):
    # Remove isolated threshold speckles only. Never close an opening or fill a hole.
    count, labels, stats, _ = cv2.connectedComponentsWithStats(mask.astype(np.uint8), connectivity=8)
    keep = np.zeros(count, bool)
    keep[1:] = stats[1:, cv2.CC_STAT_AREA] >= 2
    return keep[labels]


def bridge_projection_gaps(binary, maximum):
    """Join only tiny one-dimensional dips, not image-space fork openings."""
    result = np.asarray(binary, dtype=bool).copy()
    for start, stop in runs(~result):
        if start > 0 and stop < len(result) and stop-start <= maximum:
            result[start:stop] = True
    return result


def enclosed_background(gray, threshold):
    """Recognize closed dark slot boundaries, even with bright background returns.

    Only the *background* of sufficiently large closed contours is filled. The
    inferred regions are subtracted from foreground, never used to invent a leg
    or close the bottom of an open slot. Their coordinates remain diagnostic.
    """
    h, w = gray.shape
    dark = (gray < min(48, threshold*.3)).astype(np.uint8)
    dark = cv2.morphologyEx(dark, cv2.MORPH_CLOSE, np.ones((3, 3), np.uint8))
    contours, _ = cv2.findContours(dark, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
    result = np.zeros_like(dark)
    accepted = []
    for contour in contours:
        x, y, cw, ch = cv2.boundingRect(contour)
        hull = cv2.convexHull(contour)
        area = cv2.contourArea(hull)
        if (cw < .13*w or ch < max(3, .18*h) or cw/ch < 1.4
                or y < .12*h or y > .65*h or y+ch < .48*h
                or x < .02*w or x+cw > .98*w
                or area < .015*h*w or area > .7*h*w
                or (cw > .85*w and ch > .7*h)):
            continue
        # Complete the small dark-boundary gaps caused by missing ToF returns.
        # This fills a background opening, not the pallet foreground. Enclosing
        # this region never creates a lower beam in the thresholded image.
        cv2.drawContours(result, [hull], -1, 1, cv2.FILLED)
        accepted.append([int(x), int(y), int(cw), int(ch)])
    return result.astype(bool), accepted


def single_threshold(gray, threshold, policy, outline_gray=None, include_mask=False, measured_mask=False):
    h, w = gray.shape
    background, enclosed_regions = ((np.zeros_like(gray, bool), []) if measured_mask else
        enclosed_background(gray if outline_gray is None else outline_gray, threshold))
    raw_mask = clean_mask((gray > threshold) & ~background)
    slope, reliable = robust_slope(raw_mask)
    angle = math.degrees(math.atan(slope))
    if not reliable:
        slope = 0.
    if abs(angle) > policy['maximum_deskew_degrees']:
        return {'threshold': int(threshold), 'support_leg_count': None, 'closure_type': None,
                'reason': 'ambiguous_view', 'deskew_degrees': angle}
    mask, valid = deskew(raw_mask, slope)
    valid_counts = valid.sum(axis=1)
    horizontal = np.divide(mask.sum(axis=1), valid_counts, out=np.zeros(h), where=valid_counts > 0)
    row_density_scale = max(.4, float(np.percentile(horizontal, 95))) if measured_mask else 1.
    horizontal = np.minimum(1, horizontal/row_density_scale)
    dense = horizontal >= policy['minimum_top_beam_coverage']
    top_runs = [(a, b) for a, b in runs(dense) if a < .48*h and b-a >= max(1, round(.035*h))]
    if not top_runs:
        return {'threshold': int(threshold), 'support_leg_count': None, 'closure_type': None,
                'reason': 'no_clear_top_beam', 'horizontal_projection': horizontal.tolist(),
                'deskew_degrees': math.degrees(math.atan(slope))}
    top_start, top_end = min(top_runs, key=lambda interval: interval[0])
    # Bright returns inside a closed fork opening can be as bright as the pallet.
    # Keep the connected component anchored to the top deck, not detached bright
    # background islands. This does not fill holes or invent a lower beam.
    if not measured_mask:
        _, components = cv2.connectedComponents(mask.astype(np.uint8), connectivity=8)
        component_support = np.bincount(components[top_start:top_end].ravel())
        component_support[0] = 0
        anchor = int(np.argmax(component_support))
        if anchor == 0 or component_support[anchor] < .5*w:
            return {'threshold': int(threshold), 'support_leg_count': None, 'closure_type': None,
                    'reason': 'fragmented_top_beam'}
        mask = components == anchor
    horizontal = np.minimum(1, np.divide(mask.sum(axis=1), valid_counts,
        out=np.zeros(h), where=valid_counts > 0)/row_density_scale)
    top_profile = mask[top_start:top_end].mean(axis=0)
    active = np.flatnonzero(top_profile >= .5*row_density_scale)
    if len(active) < .65*w:
        return {'threshold': int(threshold), 'support_leg_count': None, 'closure_type': None,
                'reason': 'fragmented_top_beam'}
    left, right = int(active[0]), int(active[-1])+1
    mask, valid = mask[:, left:right], valid[:, left:right]
    width = right-left
    # Use the region below the top deck, never count a lower beam as a leg.
    band_start = max(top_end, round(.32*h))
    # Stop before a possible lower horizontal beam; otherwise use the full leg
    # height so short rear supports cannot be counted as front-face legs.
    bottom_dense = [a for a, b in runs(horizontal >= .72) if a >= .65*h and b-a >= max(1, round(.04*h))]
    band_stop = min(min(bottom_dense)-1 if bottom_dense else round(.9*h), h-1)
    if band_stop-band_start < 2:
        return {'threshold': int(threshold), 'support_leg_count': None, 'closure_type': None,
                'reason': 'upper_edge_only'}
    denom = valid[band_start:band_stop].sum(axis=0)
    vertical = np.divide(mask[band_start:band_stop].sum(axis=0), denom,
                         out=np.zeros(width), where=denom > 0)
    column_density_scale = max(.4, float(np.percentile(vertical, 95))) if measured_mask else 1.
    vertical = np.minimum(1, vertical/column_density_scale)
    if width >= 120:
        vertical = np.convolve(np.pad(vertical, 1, mode='edge'), np.ones(3)/3, mode='valid')
    leg_binary = bridge_projection_gaps(vertical >= policy['leg_projection_threshold'], max(1, round(.008*width)))
    leg_runs = [(a, b) for a, b in runs(leg_binary)
                if b-a >= max(1, round(policy['minimum_leg_width_ratio']*width))]
    endpoints_present = (len(leg_runs) >= 2 and leg_runs[0][0] <= .12*width
                         and leg_runs[-1][1] >= .88*width)
    sensible_widths = all(b-a <= .3*width for a, b in leg_runs)
    count = len(leg_runs) if endpoints_present and sensible_widths and len(leg_runs) in (2, 3, 4) else None
    reason = 'projection_candidate' if count else 'unclear_supports'
    closure = None
    gap_indices = []
    if endpoints_present:
        for (_, b), (c, _) in zip(leg_runs, leg_runs[1:]):
            pad = max(0, round((c-b)*.12))
            if c-b >= max(2, .025*width):
                gap_indices.extend(range(b+pad, c-pad))
    gap_horizontal = np.zeros(h)
    lower_start, lower_stop = max(top_end+2, round(.7*h)), max(1, round(.98*h))
    valid_lower = lower_stop-lower_start >= 2
    gap_coverages = []
    internal_lower_support = []
    if gap_indices and valid_lower:
        gap_horizontal = np.minimum(1, mask[:, gap_indices].mean(axis=1)/row_density_scale)
        beam_rows = gap_horizontal >= policy['beam_gap_coverage']
        beam_runs = [(a, b) for a, b in runs(beam_rows)
                     if b > lower_start and a < lower_stop
                     and min(b, lower_stop)-max(a, lower_start) >= max(1, round(policy['minimum_beam_thickness_ratio']*h))]
        gap_coverages = gap_horizontal[lower_start:lower_stop].tolist()
        if beam_runs:
            closure = 'tian'
        else:
            supports = [i for a, b in leg_runs for i in range(a, b)]
            support_lower = float(mask[lower_start:lower_stop, supports].mean())
            open_fraction = float(np.mean(gap_horizontal[lower_start:lower_stop] <= policy['open_gap_coverage']))
            if open_fraction >= .8 and support_lower >= .5*column_density_scale:
                closure = 'chuan'
        for a, b in leg_runs[1:-1]:
            internal_lower_support.append(float(mask[lower_start:lower_stop, a:b].mean()))
    if closure is None and reason == 'projection_candidate':
        reason = 'unclear_lower_region'
    result = {'threshold': int(threshold), 'support_leg_count': count, 'closure_type': closure,
            'reason': reason, 'deskew_degrees': math.degrees(math.atan(slope)),
            'top_beam_rows': [top_start, top_end], 'leg_band_rows': [band_start, band_stop],
            'lower_band_rows': [lower_start, lower_stop], 'active_columns': [left, right],
            'leg_runs': [[a+left, b+left] for a, b in leg_runs],
            'horizontal_projection': horizontal.tolist(), 'vertical_projection': vertical.tolist(),
            'gap_horizontal_projection': gap_horizontal.tolist(),
            'enclosed_background_regions': enclosed_regions,
            'internal_lower_support': internal_lower_support,
            'row_density_scale': row_density_scale, 'column_density_scale': column_density_scale,
            'bottom_gap_coverage_mean': float(np.mean(gap_coverages)) if gap_coverages else None,
            'foreground_fraction': float(raw_mask.mean())}
    if include_mask:
        full_mask = np.zeros((h, w), dtype=np.uint8)
        full_mask[:, left:right] = mask.astype(np.uint8)*255
        result['_binary_mask'] = full_mask
    return result


def analyze_measured_mask(full_mask, validity, row, policy):
    """Project unfilled coplanar measured pixels; never infer a hole from no return."""
    x1, y1, x2, y2 = integer_box(row['bbox'], full_mask.shape[1], full_mask.shape[0])
    mask = full_mask[y1:y2, x1:x2].astype(bool)
    h, w = mask.shape
    trials = []
    for change in (-.08, -.04, 0., .04, .08):
        variant = dict(policy)
        variant['leg_projection_threshold'] = float(np.clip(policy['leg_projection_threshold']+change, .4, .9))
        variant['beam_gap_coverage'] = float(np.clip(policy['beam_gap_coverage']+change, .4, .95))
        trial = single_threshold(mask.astype(np.uint8)*255, 127, variant, measured_mask=True)
        trial['projection_level_offset'] = change
        trials.append(trial)
    leg, lvote = consensus(trials, 'support_leg_count', policy)
    closure, cvote = consensus(trials, 'closure_type', policy)
    flags = []
    if leg is None: flags.append('measured_leg_projection_unstable')
    if closure is None: flags.append('measured_bottom_projection_unstable')
    if mask.mean() < policy['minimum_measured_mask_fraction']:
        flags.append('measured_mask_too_sparse'); leg = closure = None
    if row.get('crop_touches_image_edge'):
        flags.append('truncated'); leg = closure = None
    if w < policy['minimum_bbox_width'] or h < policy['minimum_bbox_height']:
        flags.append('low_resolution'); leg = closure = None
    selected = min(trials, key=lambda t: (abs(t['projection_level_offset']), t['projection_level_offset']))
    if leg == 4:
        flags.append('four_leg_candidate_review'); leg = None
    # Raw point validity is needed to distinguish background from missing data.
    gap_coverage = None
    if closure == 'chuan':
        a, b = selected.get('lower_band_rows', [round(.7*h), h])
        spans = selected.get('leg_runs', [])
        gaps = [x for (_, stop), (start, _) in zip(spans, spans[1:]) for x in range(stop, start)]
        vv = validity[y1:y2, x1:x2]
        vv, _ = deskew(vv, math.tan(math.radians(selected.get('deskew_degrees', 0))))
        gap_coverage = float(vv[a:b, gaps].mean()) if gaps and b > a else 0.
        if gap_coverage < policy['minimum_open_gap_point_coverage']:
            flags.append('missing_returns_not_an_opening'); closure = None
    exact = single_threshold(mask.astype(np.uint8)*255, 127, policy, measured_mask=True, include_mask=True)
    return {'support_leg_count': leg, 'closure_type': closure, 'review_required': bool(flags),
            'flags': flags, 'selected_trial': selected, 'leg_consensus': lvote, 'closure_consensus': cvote,
            'projection_trials': [{k: v for k, v in t.items() if not k.endswith('projection')} for t in trials],
            'gap_valid_point_coverage': gap_coverage,
            'measured_mask_fraction': float(mask.mean()), 'source_masks_filled': False,
            '_binary_mask': exact.get('_binary_mask')}


def consensus(trials, attribute, policy):
    counts = Counter(t[attribute] for t in trials)
    # Unknown votes cannot be discarded to inflate agreement.
    winner = min(counts, key=lambda value: (-counts[value], value is not None, str(value)))
    fraction = counts[winner]/len(trials)
    accepted = (winner is not None and counts[winner] >= policy['minimum_thresholds']
                and fraction >= policy['minimum_threshold_agreement'])
    return winner if accepted else None, {
        'candidate': winner, 'agreement': fraction, 'agreeing_thresholds': counts[winner],
        'total_thresholds': len(trials), 'agreement_is_probability': False,
        'votes': {str(k) if k is not None else 'unknown': v for k, v in counts.items()}}


def analyze_target(image, row, policy, include_mask=False):
    if image.ndim != 2 or image.dtype != np.uint8:
        raise ValueError('Expected the native uint8 intensity image')
    x1, y1, x2, y2 = integer_box(row['bbox'], image.shape[1], image.shape[0])
    gray = image[y1:y2, x1:x2]
    h, w = gray.shape
    flags = []
    if row.get('crop_touches_image_edge') or x1 == 0 or y1 == 0 or x2 == image.shape[1] or y2 == image.shape[0]:
        flags.append('truncated')
    if w < policy['minimum_bbox_width'] or h < policy['minimum_bbox_height']:
        flags.append('low_resolution')
    if h/w < policy['minimum_bbox_height_width_ratio']:
        flags.append('upper_edge_only')
    q10, q90 = np.percentile(gray, [10, 90])
    if q90-q10 < policy['minimum_intensity_contrast']:
        flags.append('low_intensity_contrast')
    blurred = cv2.GaussianBlur(gray, (3, 3), .6) if min(h, w) >= 12 else gray
    nonzero = blurred[blurred > 0]
    otsu, _ = cv2.threshold(nonzero.reshape(-1, 1) if len(nonzero) else blurred,
                            0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
    low = nonzero[nonzero <= otsu]
    high = nonzero[nonzero > otsu]
    # Otsu may return the *edge* of a wide empty histogram valley. Center the
    # perturbation tests between its two class medians instead of testing the
    # background peak itself (important for clean bimodal masks).
    baseline = float((np.median(low)+np.median(high))/2) if len(low) and len(high) else float(otsu)
    thresholds = sorted({int(np.clip(round(baseline+offset), 1, 253)) for offset in policy['threshold_offsets']})
    trials = [single_threshold(blurred, threshold, policy, outline_gray=gray) for threshold in thresholds]
    leg, leg_vote = consensus(trials, 'support_leg_count', policy)
    closure, closure_vote = consensus(trials, 'closure_type', policy)
    if leg is None:
        flags.append('unstable_or_unclear_leg_projection')
    if closure is None:
        flags.append('unstable_or_unclear_bottom_projection')
    if any(f in flags for f in ('truncated', 'low_resolution', 'upper_edge_only', 'low_intensity_contrast')):
        leg = closure = None
    # A bright floor/background cannot certify a beam without a lower boundary.
    below = image[y2:min(image.shape[0], y2+max(2, round(.12*h))), x1:x2]
    bottom_contrast = float(np.mean(gray[max(0, h-max(2, round(.1*h))):])-np.mean(below)) if below.size else None
    if closure == 'tian' and (bottom_contrast is None or bottom_contrast < 8):
        closure = None
        flags.append('beam_floor_boundary_unclear')
    if flags:
        selected = min(trials, key=lambda t: (abs(t['threshold']-baseline), t['threshold']))
    else:
        selected = min(trials, key=lambda t: ((t['support_leg_count'], t['closure_type']) != (leg, closure),
                                             abs(t['threshold']-baseline), t['threshold']))
    if leg == 4:
        # Four peaks are especially vulnerable to rear legs, forks and tiny
        # splits of the same front leg. Keep the raw candidate for rechecking.
        leg = None
        flags.append('four_leg_candidate_review')
    if leg == 3 and closure == 'chuan' and any(v < .4 for v in selected.get('internal_lower_support', [])):
        leg = None
        flags.append('short_or_rear_internal_support')
    if closure == 'chuan' and any(y+rh >= h-1 for _, y, _, rh in selected.get('enclosed_background_regions', [])):
        closure = None
        flags.append('lower_boundary_at_roi_edge')
    result = {'support_leg_count': leg, 'closure_type': closure,
            'review_required': bool(flags) or leg is None or closure is None,
            'flags': sorted(set(flags)), 'method': 'threshold_xy_projections',
            'native_integer_bbox': [x1, y1, x2, y2], 'native_roi_shape': [h, w],
            'otsu_threshold': float(otsu), 'thresholds': thresholds,
            'threshold_baseline_between_class_medians': baseline,
            'intensity_contrast_p90_p10': float(q90-q10), 'bottom_boundary_contrast': bottom_contrast,
            'leg_consensus': leg_vote, 'closure_consensus': closure_vote,
            'selected_trial': selected,
            'threshold_trials': [{k: v for k, v in t.items() if not k.endswith('projection')} for t in trials],
            'human_reviewed': False, 'training_eligible': False}
    if include_mask:
        result['_raw_threshold_mask'] = (blurred > selected['threshold']).astype(np.uint8)*255
        exact = single_threshold(blurred, selected['threshold'], policy, outline_gray=gray, include_mask=True)
        result['_binary_mask'] = exact.get('_binary_mask')
    return result
