# -*- coding: utf-8 -*-
"""纸表识别：离线 OCR + 固定模板结构解析。

设计原则：
1. 印刷坐标决定班级/时段/房号，OCR 只处理问题格内容；
2. 结果只是候选，低置信度、人数计分、高分值必须人工确认；
3. 识别过程不直接写飞书，提交由 app.py 的 /api/paper/confirm 完成。
"""
from __future__ import annotations

import io
import math
import re
import threading
import time
from collections import Counter

TEMPLATES = {
    'after_class': {
        'id': 'after_class',
        'name': '课后班级检查汇总表',
        'short': '课后勾选表',
        'dept': '纪检部',
        'priority': '首选试点',
        'summary': '班级行 × A–G 项目列，勾选后按印刷规则算分',
        'needs_grade': True,
        'needs_building': False,
        'items': [
            {'code': 'A', 'item': '灯未关（含黑板灯、走廊灯）', 'score': 5, 'unit': '项', 'rule': '5分/班'},
            {'code': 'B', 'item': '电脑/一体机未关', 'score': 5, 'unit': '项', 'rule': '5分/班'},
            {'code': 'C', 'item': '风扇/空调未关', 'score': 5, 'unit': '项', 'rule': '5分/班'},
            {'code': 'D', 'item': '门窗/窗帘未关', 'score': 5, 'unit': '项', 'rule': '5分/班'},
            {'code': 'E', 'item': '桌椅/卫生角/垃圾桶不规范', 'score': 2, 'unit': '处', 'rule': '2分/处'},
            {'code': 'F', 'item': '男女交往异常（已核实）', 'score': 50, 'unit': '人次', 'rule': '50分/人次', 'risk': 'high'},
            {'code': 'G', 'item': '安全隐患未及时报告', 'score': 2, 'unit': '项', 'rule': '2分/班'},
        ],
    },
    'daily_matrix': {
        'id': 'daily_matrix',
        'name': '年级日常量化表',
        'short': '年级手写矩阵',
        'dept': '纪检部',
        'priority': '第二期',
        'summary': '班级行 × 早/午/晚读列，一格可拆多项',
        'needs_grade': True,
        'needs_building': False,
        'items': [],
    },
    'dorm_log': {
        'id': 'dorm_log',
        'name': '宿舍内务纪律检查评价日志',
        'short': '宿舍手写矩阵',
        'dept': '宿管部',
        'priority': '第三期',
        'summary': '楼层 × 房号 × 类别，通过宿舍安排映射班级',
        'needs_grade': False,
        'needs_building': True,
        'items': [],
    },
}

ISSUE_WORDS = (
    '迟到', '早退', '讲话', '说话', '睡觉', '手机', '校服', '校牌', '早餐', '仪容',
    '打闹', '追逐', '吸烟', '打架', '顶撞', '卫生', '垃圾', '桌椅', '地面', '床铺',
    '被褥', '物品', '摆放', '未关', '灯', '电脑', '风扇', '空调', '门窗', '窗帘',
    '安全隐患', '交往', '熄灯', '讲话', '串寝', '晚归', '内务', '纪律',
)
STOPWORDS = {
    '检查人', '复核人', '年级', '班级', '日期', '星期', '早读', '午读', '晚读', '委员签到',
    '序号', '备注', '小计', '扣分', '加分', '楼栋', '楼层', '房间', '内务卫生', '纪律',
    '班主任', '值日负责人', '所在班级', '得分', '评分', '合计',
}
PERIODS = ('早读', '午读', '晚读')
CN_FLOOR = {'一': 1, '二': 2, '三': 3, '四': 4, '五': 5, '六': 6, '七': 7, '八': 8}

_OCR_LOCK = threading.Lock()
_OCR_ENGINE = None
_OCR_ENGINE_NAME = ''
_OCR_ERROR = None


class PaperError(RuntimeError):
    pass


def public_templates():
    out = []
    for tid in ('after_class', 'daily_matrix', 'dorm_log'):
        t = TEMPLATES[tid]
        out.append({k: t[k] for k in ('id', 'name', 'short', 'dept', 'priority', 'summary', 'needs_grade', 'needs_building', 'items')})
    return out


def image_from_bytes(data: bytes):
    """返回 (BGR numpy, RGB PIL)；处理 EXIF 方向与 RGBA 白底。"""
    from PIL import Image, ImageOps
    import numpy as np
    if not data:
        raise PaperError('没有收到图片数据')
    try:
        img = Image.open(io.BytesIO(data))
        img = ImageOps.exif_transpose(img)
        if img.mode in ('RGBA', 'LA'):
            bg = Image.new('RGB', img.size, 'white')
            bg.paste(img.convert('RGB'), mask=img.getchannel('A'))
            img = bg
        else:
            img = img.convert('RGB')
    except Exception as exc:
        raise PaperError('无法读取这张图片，请换 JPG/PNG 重试') from exc
    max_side = 2400
    if max(img.size) > max_side:
        scale = max_side / max(img.size)
        img = img.resize((max(1, int(img.width * scale)), max(1, int(img.height * scale))), Image.LANCZOS)
    rgb = np.array(img)
    bgr = rgb[:, :, ::-1].copy()
    return bgr, img


def image_quality(bgr):
    import cv2
    gray = cv2.cvtColor(bgr, cv2.COLOR_BGR2GRAY)
    h, w = gray.shape
    blur = float(cv2.Laplacian(gray, cv2.CV_64F).var())
    brightness = float(gray.mean())
    issues = []
    if w < 900 or h < 650:
        issues.append('图片分辨率偏低，建议使用 1080P 以上照片')
    if blur < 45:
        issues.append('画面较模糊，请保持手机/摄像头稳定后重拍')
    if brightness < 55:
        issues.append('画面偏暗，请补充光线')
    elif brightness > 215:
        issues.append('画面过亮，可能丢失手写细节')
    return {'width': w, 'height': h, 'blur': round(blur, 1), 'brightness': round(brightness, 1), 'issues': issues}


def _get_engine():
    global _OCR_ENGINE, _OCR_ENGINE_NAME, _OCR_ERROR
    if _OCR_ENGINE is not None:
        return _OCR_ENGINE
    errors = []
    # 优先新版本地引擎：RapidOCR 3.x（内置 PP-OCRv6）
    try:
        from rapidocr import RapidOCR as RapidOCR3
        _OCR_ENGINE = RapidOCR3(params={'Global.log_level': 'error'})
        try:
            from importlib.metadata import version
            _OCR_ENGINE_NAME = 'RapidOCR ' + version('rapidocr') + ' / PP-OCRv6'
        except Exception:
            _OCR_ENGINE_NAME = 'RapidOCR 3 / PP-OCRv6'
        _OCR_ERROR = None
        return _OCR_ENGINE
    except Exception as exc:
        errors.append('RapidOCR3: ' + str(exc))
    # 兼容旧安装包
    try:
        from rapidocr_onnxruntime import RapidOCR
        _OCR_ENGINE = RapidOCR()
        _OCR_ENGINE_NAME = 'RapidOCR / PP-OCRv3'
        _OCR_ERROR = None
        return _OCR_ENGINE
    except Exception as exc:
        errors.append('RapidOCR1: ' + str(exc))
    _OCR_ERROR = ' | '.join(errors)
    raise PaperError('本地 OCR 引擎未就绪：' + _OCR_ERROR[:200])


def engine_info():
    """不加载模型，只报告安装了哪一套本地引擎。"""
    try:
        from importlib.metadata import version
        v = version('rapidocr')
        return {'ready': True, 'name': 'RapidOCR ' + v + ' / PP-OCRv6', 'engine': 'rapidocr3'}
    except Exception:
        pass
    try:
        from importlib.metadata import version
        v = version('rapidocr_onnxruntime')
        return {'ready': True, 'name': 'RapidOCR ' + v + ' / PP-OCRv3', 'engine': 'rapidocr1'}
    except Exception:
        return {'ready': False, 'name': '未安装本地 OCR', 'engine': ''}


def ocr_available():
    global _OCR_ERROR
    try:
        _get_engine()
        return True, ''
    except Exception as exc:
        return False, str(exc)[:200]


def _token_from_box(box, text, score):
    pts = [(float(p[0]), float(p[1])) for p in box]
    xs, ys = [p[0] for p in pts], [p[1] for p in pts]
    return {
        'text': str(text or '').strip(), 'confidence': round(float(score) * 100, 0),
        'x': min(xs), 'y': min(ys), 'right': max(xs), 'bottom': max(ys),
        'cx': sum(xs) / len(xs), 'cy': sum(ys) / len(ys),
        'w': max(xs) - min(xs), 'h': max(ys) - min(ys),
    }


def _run_ocr(bgr):
    with _OCR_LOCK:
        engine = _get_engine()
        t0 = time.time()
        try:
            raw = engine(bgr)
        except Exception as exc:
            raise PaperError('OCR 执行失败：' + str(exc)[:160]) from exc
        elapsed = time.time() - t0
    tokens = []
    if hasattr(raw, 'txts') and hasattr(raw, 'boxes'):
        # rapidocr 3.x: RapidOCROutput（boxes 是 ndarray，不能用 or [] 判断）
        boxes = raw.boxes if raw.boxes is not None else []
        txts = raw.txts if raw.txts is not None else []
        scores = raw.scores if raw.scores is not None else []
        for box, text, score in zip(boxes, txts, scores):
            try:
                t = _token_from_box(box, text, score)
                if t['text']:
                    tokens.append(t)
            except Exception:
                continue
    else:
        # rapidocr 1.x: (result, elapse)
        result = raw[0] if isinstance(raw, tuple) else raw
        for row in result or []:
            try:
                t = _token_from_box(row[0], row[1], row[2])
                if t['text']:
                    tokens.append(t)
            except Exception:
                continue
    tokens.sort(key=lambda t: (t['y'], t['x']))
    return tokens, elapsed


def _norm_text(s):
    s = (s or '').replace('（', '(').replace('）', ')').replace('：', ':').replace('，', ',').replace('。', ' ').replace('、', ',')
    s = re.sub(r'\s+', ' ', s).strip()
    return s


def _class_no(text):
    s = _norm_text(text)
    m = re.search(r'(?<!\d)([1-9]|[12]\d|30)\s*班', s)
    return int(m.group(1)) if m else None


def _class_name(grade, no, classes):
    """把纸表上的班号映射到当前年级的班级字典。
    字典缺失或该班号不存在时返回空串——绝不编造“高一（5）班”这种假班级，
    由上层标成必须人工复核。"""
    pool = list((classes or {}).get(grade) or [])
    for c in pool:
        if re.search(r'[（(]%d[）)]班' % no, c) or re.search(r'(?<!\d)%d班' % no, c):
            return c
    return ''


def _grade_from_cls(cls):
    for g in ('初一', '初二', '初三', '高一', '高二', '高三'):
        if (cls or '').startswith(g):
            return g
    return ''


_OCR_FIXES = (
    ('退到', '迟到'), ('送到', '迟到'), ('买到', '迟到'), ('达到', '迟到'), ('迟刭', '迟到'), ('迟列', '迟到'),
    ('荣早', '带早餐'), ('早参', '带早餐'), ('带早', '带早餐'), ('带早餐餐', '带早餐'),
    ('末戴', '未戴'), ('未载', '未戴'), ('戴校卑', '未戴校牌'), ('戴枚牌', '未戴校牌'),
    ('藏枝牌', '未戴校牌'), ('戴枝牌', '未戴校牌'), ('藏板牌', '未戴校牌'),
    ('未戴校牌', '未佩戴校牌'), ('校牌', '校牌'),
    ('校服不合', '校服不合格'), ('浪服', '校服'), ('讲活', '讲话'), ('睡党', '睡觉'),
    ('卫坐', '卫生'), ('垃极', '垃圾'),
)

def _fix_ocr_text(text):
    s = _norm_text(text)
    # 去掉勾选符号、单元格边缘漏进来的班级序号
    s = re.sub(r'^[Vv√✓✔×xX\s]+', '', s)
    s = re.sub(r'^(\d{1,2})(?!\s*(?:人|名|处|次|间))(?=[\u4e00-\u9fa5])', '', s)
    for a, b in _OCR_FIXES:
        s = s.replace(a, b)
    s = s.replace('未未佩戴', '未佩戴').replace('未未戴', '未戴')
    return s

_RULE_ALIAS = (
    ('板牌', '校牌'), ('校卑', '校牌'), ('未带校牌', '未佩戴校牌'), ('没戴校牌', '未佩戴校牌'),
    ('头发长', '头发过长'), ('长头发', '头发过长'), ('睡堂', '课堂睡觉'),
)


def _rule_core(item):
    """细则名去掉场景前缀后的“核心词”，用于短手写词的匹配。
    例：仪容仪表检查·头发过长 → 头发过长；早读巡查·迟到 → 迟到。"""
    s = re.sub(r'[（(].*?[）)]', '', item or '')
    parts = [x for x in re.split(r'[·•\-—_/]', s) if x.strip()]
    s = parts[-1] if parts else s
    s = re.sub(r'^(仪容仪表|早读|午读|晚读|课堂|宿舍|卫生|两操|课间|自习|就餐|升旗|集会)(检查|巡查|纪律|状态|管理|秩序)', '', s)
    return re.sub(r'[\s()（）:：,，。;；\-_/]', '', s)


def _rule_scores(text, rules, dept='', grade=''):
    """给每条细则打分（0-1），按分数降序返回 [(score, rule)]。不裁剪阈值，供上层决定。"""
    import difflib
    s = _fix_ocr_text(text)
    clean = re.sub(r'[\s\d:.,，。;；、()（）\-_/]', '', s)
    clean = re.sub(r'(人|名|人次|处|次|间|项)$', '', clean)
    if not clean:
        return []
    for a, b in _RULE_ALIAS:
        if a in clean:
            clean = clean.replace(a, b)
    out = []
    for r in rules or []:
        if (r.get('type') or '扣分') != '扣分':
            continue
        if dept and r.get('dept') and r.get('dept') != dept:
            continue
        rg = r.get('grade') or ''
        if grade and rg not in ('', '通用', grade):
            continue
        item = re.sub(r'[\s()（）·:：,，。;；\-_/]', '', r.get('item') or '')
        if not item:
            continue
        score = 0.0
        if clean == item:
            score = 1.0
        elif len(clean) >= 3 and (clean in item or item in clean) and abs(len(clean) - len(item)) <= 6:
            score = 0.92
        elif len(clean) == 2 and len(item) <= 6 and (clean in item or item in clean):
            score = 0.86
        elif len(clean) >= 4 and len(item) >= 4:
            ratio = difflib.SequenceMatcher(None, clean, item).ratio()
            if ratio > 0.55:
                score = ratio * 0.8
        # 核心词匹配：手写常只写事项本身，细则带“XX检查·”前缀
        if score < 0.9:
            core = _rule_core(r.get('item') or '')
            if core and len(clean) >= 2:
                if clean == core:
                    score = max(score, 0.95)
                elif clean in core:
                    extra = len(core) - len(clean)
                    if extra <= 2:
                        score = max(score, 0.88)
                    elif extra <= 6:
                        score = max(score, 0.80)
                elif core in clean and len(core) >= 3:
                    score = max(score, 0.82)
        if score > 0:
            out.append((score, r))
    out.sort(key=lambda x: (-x[0], len(x[1].get('item') or '')))
    return out


def _rule_match(text, rules, dept='', grade=''):
    """在现有细则里找最接近的扣分项。短词只在“近似等长”时匹配，避免把“迟到”匹配到长条款。"""
    for score, r in _rule_scores(text, rules, dept, grade):
        return r if score >= 0.72 else None
    return None


def rule_candidates(text, rules, dept='', grade='', topn=3, min_sim=0.55, prefer_dept=''):
    """本地 Top-N 细则候选（不调用任何模型）。返回 [{item, dept, score, sim}]。
    同部门候选给 0.03 加权，只影响排序，不影响是否入选。"""
    scored = _rule_scores(text, rules, dept, grade)
    if prefer_dept:
        scored = [(min(1.0, sc + (0.03 if (r.get('dept') or '') == prefer_dept else 0)), r)
                  for sc, r in scored]
        scored.sort(key=lambda x: -x[0])
    out, seen = [], set()
    for sim, r in scored:
        if sim < min_sim:
            break
        item = r.get('item') or ''
        if not item or item in seen:
            continue
        seen.add(item)
        try:
            sc = abs(float(r.get('score') or 0))
        except (TypeError, ValueError):
            sc = 0
        out.append({'item': item, 'dept': r.get('dept') or '', 'score': sc, 'sim': round(sim, 2)})
        if len(out) >= topn:
            break
    return out


def _extract_qty(text):
    s = _norm_text(text)
    m = re.search(r'(\d{1,3})\s*(?:人|名|人次|处|次|间)', s)
    if m:
        return max(1, int(m.group(1)))
    # “学号20、21”这类没有“人”字，但多个分隔数字表示多名学生
    nums = re.findall(r'(?<!\d)(\d{1,3})(?!\d)', s)
    if len(nums) >= 2 and re.search(r'[、,，/]', s):
        return len(nums)
    # 单独一个数字不等同于人数（可能是日期、总分或被识别出来的页眉）
    return 1


def _meaningful_text(text):
    t = re.sub(r'[\s\d:.,，。;；、()（）/\\\-_]+', '', _norm_text(text))
    if len(t) < 2 or t in STOPWORDS:
        return False
    return any(w in _norm_text(text) for w in ISSUE_WORDS)


def _group_lines(tokens, y_tol=0.65):
    lines = []
    for t in sorted(tokens, key=lambda x: (x['cy'], x['cx'])):
        placed = False
        for line in lines:
            ref = line[0]
            h = max(8.0, min(ref['h'], t['h']))
            if abs(ref['cy'] - t['cy']) <= max(8.0, h * y_tol):
                line.append(t)
                placed = True
                break
        if not placed:
            lines.append([t])
    for line in lines:
        line.sort(key=lambda x: x['x'])
    lines.sort(key=lambda line: min(t['y'] for t in line))
    return lines


def _cluster_positions(values, tol):
    vals = sorted(values)
    groups = []
    for v in vals:
        if not groups or abs(v - sum(groups[-1]) / len(groups[-1])) > tol:
            groups.append([v])
        else:
            groups[-1].append(v)
    return [sum(g) / len(g) for g in groups]


def _detect_checkmarks(bgr):
    """检测小方框内是否有勾/叉。返回坐标和内部墨迹比例，不判断业务。"""
    try:
        import cv2
        import numpy as np
    except Exception:
        return []
    gray = cv2.cvtColor(bgr, cv2.COLOR_BGR2GRAY)
    bw = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY_INV, 21, 8)
    contours, _ = cv2.findContours(bw, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
    boxes = []
    for cnt in contours:
        x, y, w, h = cv2.boundingRect(cnt)
        if not (9 <= w <= 36 and 9 <= h <= 36):
            continue
        if not (0.68 <= w / max(1, h) <= 1.45):
            continue
        # 方框外轮廓本身应有一定的线条感
        peri = cv2.arcLength(cnt, True)
        if peri < 28 or peri > 180:
            continue
        m = 4
        if w <= 2 * m + 4 or h <= 2 * m + 4:
            continue
        inner = gray[y + m:y + h - m, x + m:x + w - m]
        if inner.size == 0:
            continue
        ratio = float((inner < 145).mean())
        boxes.append({'x': x, 'y': y, 'w': w, 'h': h, 'cx': x + w / 2, 'cy': y + h / 2, 'ink': ratio})
    # 同一位置可能得到内外两个轮廓，按中心去重
    out = []
    for b in sorted(boxes, key=lambda z: z['ink'], reverse=True):
        if any(abs(b['cx'] - a['cx']) < 6 and abs(b['cy'] - a['cy']) < 6 for a in out):
            continue
        out.append(b)
    return out


def _base_candidate(idx, template_id, item, qty, score, unit, formula, desc, conf, status='ok', issue='', **extra):
    c = {
        'id': 'p%d' % idx,
        'template': template_id,
        'item': item[:80],
        'qty': max(1, int(qty or 1)),
        'unit': unit or '项',
        'score': float(score or 0),
        'formula': formula or '',
        'desc': desc[:500],
        'confidence': int(round(max(0, min(100, conf or 0)))),
        'status': status,
        'issue': issue,
        'selected': True,
    }
    c.update(extra)
    return c


def _parse_after(tokens, boxes, bgr, ctx, classes):
    """印刷坐标 + 小方框勾选。只输出被勾选的项目。"""
    import numpy as np
    h, w = bgr.shape[:2]
    items = {x['code']: x for x in TEMPLATES['after_class']['items']}
    headers = {}
    for code in items:
        cand = []
        for t in tokens:
            if t['y'] > h * 0.48:
                continue
            tx = _norm_text(t['text']).upper()
            if len(tx) <= 8 and (code in tx and any(k in tx for k in ('灯', '电脑', '风扇', '门', '桌椅', '交往', '安全')) or tx == code):
                cand.append(t)
        if cand:
            headers[code] = min(cand, key=lambda z: abs(z['y'] - min(q[ 'y'] for q in cand)))['cx']
    # 班级行：左列印刷 1班、2班……；同一行右侧重复列时取最左
    class_tokens = []
    for t in tokens:
        no = _class_no(t['text'])
        if no and no <= 40 and t['x'] < w * 0.48:
            class_tokens.append((no, t))
    rows = []
    for no, t in sorted(class_tokens, key=lambda x: x[1]['cy']):
        if rows and abs(rows[-1]['cy'] - t['cy']) < max(9, t['h'] * 0.7):
            if t['x'] < rows[-1]['x']:
                rows[-1].update({'no': no, 'cy': t['cy'], 'x': t['x']})
        else:
            rows.append({'no': no, 'cy': t['cy'], 'x': t['x']})
    if not rows:
        return [], ['未识别到左侧班级行，无法把勾选映射到班级。请拍全左侧班级列。']
    if not headers:
        return [], ['未识别到 A–G 项目表头，无法判断勾选属于哪一项。请拍全表头。']

    # 小方框：只保留表格范围内的，使用中位墨迹作为空框基线
    min_y = min(r['cy'] for r in rows) - 35
    max_y = max(r['cy'] for r in rows) + 35
    min_x = headers.get('A', min(headers.values())) - 85
    max_x = headers.get('G', max(headers.values())) + 85
    valid_boxes = [b for b in boxes if min_x <= b['cx'] <= max_x and min_y <= b['cy'] <= max_y]
    inks = [b['ink'] for b in valid_boxes]
    base = float(np.median(inks)) if inks else 0.0
    threshold = max(0.055, base + 0.035)

    marks = [b for b in valid_boxes if b['ink'] >= threshold]
    # OCR 有时会直接识别出勾/叉，补充进候选位置
    for t in tokens:
        if _norm_text(t['text']).lower() in ('v', '✓', '√', '✔', 'x', '×') and min_x <= t['cx'] <= max_x:
            marks.append({'cx': t['cx'], 'cy': t['cy'], 'ink': 0.2})

    row_gap = 45.0
    if len(rows) > 1:
        diffs = [rows[i + 1]['cy'] - rows[i]['cy'] for i in range(len(rows) - 1)]
        diffs = [d for d in diffs if d > 5]
        if diffs:
            row_gap = float(np.median(diffs))
    seen, out = set(), []
    for b in sorted(marks, key=lambda z: (z['cy'], z['cx'])):
        row = min(rows, key=lambda r: abs(r['cy'] - b['cy']))
        if abs(row['cy'] - b['cy']) > max(12, row_gap * 0.62):
            continue
        code = min(headers, key=lambda k: abs(headers[k] - b['cx']))
        if abs(headers[code] - b['cx']) > 95:
            continue
        if (row['no'], code) in seen:
            continue
        seen.add((row['no'], code))
        meta = items[code]
        cls = _class_name(ctx.get('grade'), row['no'], classes)
        risk_high = meta.get('risk') == 'high'
        status = 'error' if risk_high else 'ok'
        issue = 'F类高分值且须核实，识别后仍需人工确认' if risk_high else ''
        conf = min(99, 88 + (b.get('ink', 0.1) * 100))
        known = ctx.get('_known_classes') or []
        if not cls:
            status = 'error'
            issue = '第%d班不在当前年级班级字典中，请核对年级或班级序号' % row['no']
        elif known and cls not in known:
            status = 'error'
            issue = '班级未匹配到当前年级班级字典'
        out.append(_base_candidate(
            len(out) + 1, 'after_class', meta['item'], 1, meta['score'], meta['unit'],
            meta['rule'], '来源：%d班 × %s项；复选框检测墨迹 %.3f；模板规则自动算分' % (row['no'], code, b.get('ink', 0)),
            conf, status, issue,             target=cls, grade=_grade_from_cls(cls), cls=cls, dept='纪检部',
            roi=[int(max(0, b.get('x', b['cx'] - 35))), int(max(0, b.get('y', b['cy'] - 35))),
                 int(min(w, b.get('right', b['cx'] + 35))), int(min(h, b.get('bottom', b['cy'] + 35)))],
            cell='%d班 × %s' % (row['no'], code), risk=meta.get('risk', ''),
        ))
    warns = []
    if not out:
        warns.append('未检测到明确勾选项。请确认纸表是“勾选表示存在问题”，或改用手动补录。')
    if len(valid_boxes) < 10:
        warns.append('检测到的方框数量偏少，照片可能未拍全或表格线不清晰。')
    if any(r['no'] > len(((classes or {}).get(ctx.get('grade')) or [])) for r in rows):
        warns.append('部分班级行号超出当前年级班级字典，请核对年级选择。')
    return out, warns


def _extract_cell_qty(text):
    """逐格二次识别后，允许“带早餐:3”这种无单位的单数字；上限 20，避免把页眉数字当人数。"""
    s = _norm_text(text)
    m = re.search(r'(\d{1,3})\s*(?:人|名|人次|处|次|间)', s)
    if m:
        return max(1, int(m.group(1)))
    nums = re.findall(r'(?<!\d)(\d{1,3})(?!\d)', s)
    if len(nums) >= 2 and re.search(r'[、,，/]', s):
        return len(nums)
    if len(nums) == 1 and re.search(r'[:：]', s):
        n = int(nums[0])
        if 1 <= n <= 20:
            return n
    return 1

def _ocr_cell_text(bgr, x0, x1, y0, y1):
    """把疑似问题格放大后单独 OCR 一次；返回 (文本, 平均置信度)。"""
    try:
        import cv2
    except Exception:
        return '', 0
    h, w = bgr.shape[:2]
    x0, x1 = max(0, int(x0)), min(w, int(x1))
    y0, y1 = max(0, int(y0)), min(h, int(y1))
    if x1 - x0 < 12 or y1 - y0 < 12:
        return '', 0
    crop = bgr[y0:y1, x0:x1]
    scale = max(2.0, min(4.0, 120.0 / max(1, crop.shape[0])))
    crop = cv2.resize(crop, None, fx=scale, fy=scale, interpolation=cv2.INTER_CUBIC)
    try:
        toks, _ = _run_ocr(crop)
    except Exception:
        return '', 0
    parts, scores = [], []
    for t in toks:
        s = _norm_text(t['text'])
        if not s:
            continue
        if re.fullmatch(r'[Vv√✓✔xX×\-—_.,，。:：;；]+', s):
            continue
        if not re.search(r'[\u4e00-\u9fa5]', s) and not re.search(r'\d', s):
            continue
        parts.append(s)
        scores.append(t['confidence'])
    return ''.join(parts), (sum(scores) / len(scores) if scores else 0)

def _cell_components(bgr, x0, x1, y0, y1):
    """统计单元格内小墨迹连通域，作为“这一格有手写”的补充证据。"""
    try:
        import cv2
        import numpy as np
    except Exception:
        return 0
    x0, x1 = max(0, int(x0)), min(bgr.shape[1], int(x1))
    y0, y1 = max(0, int(y0)), min(bgr.shape[0], int(y1))
    if x1 - x0 < 8 or y1 - y0 < 8:
        return 0
    gray = cv2.cvtColor(bgr[y0:y1, x0:x1], cv2.COLOR_BGR2GRAY)
    bw = (gray < 108).astype(np.uint8)
    n, lab, stats, cent = cv2.connectedComponentsWithStats(bw, 8)
    cnt = 0
    for j in range(1, n):
        area = int(stats[j, cv2.CC_STAT_AREA])
        ww = int(stats[j, cv2.CC_STAT_WIDTH])
        hh = int(stats[j, cv2.CC_STAT_HEIGHT])
        if 5 <= area <= 700 and ww >= 2 and hh >= 2 and ww <= (x1 - x0) * 0.9:
            cnt += 1
    # 窄格写得少也算；归一到约 300px 宽，减少列宽差异
    return cnt * (300.0 / max(1, x1 - x0))

def _parse_daily(tokens, bgr, ctx, classes, rules, refine=False):
    h, w = bgr.shape[:2]
    period_headers = {}
    for p in PERIODS:
        cand = [t for t in tokens if p in _norm_text(t['text']) and t['y'] < h * 0.55]
        if cand:
            period_headers[p] = min(cand, key=lambda z: z['y'])['cx']
    period_cols = sorted(period_headers.items(), key=lambda x: x[1])
    if not period_cols:
        return [], ['未识别到早读/午读/晚读表头，时段需人工确认。请拍全表头后重试。']
    # 班级行：取左半页印刷“N班”，同一行右侧重复列只保留最左
    rows = []
    for t in tokens:
        no = _class_no(t['text'])
        if no and no <= 40 and t['x'] < w * 0.5:
            if rows and abs(rows[-1]['cy'] - t['cy']) < max(9, t['h'] * 0.7):
                if t['x'] < rows[-1]['x']:
                    rows[-1].update({'no': no, 'cy': t['cy'], 'x': t['x'], 'right': t['right']})
            else:
                rows.append({'no': no, 'cy': t['cy'], 'x': t['x'], 'right': t['right']})
    if not rows:
        return [], ['未识别到左侧班级行，无法把问题格映射到班级。请拍全左侧班级列。']
    xs = [v for _, v in period_cols]
    bounds = [xs[0] - (xs[1] - xs[0]) * 0.55 if len(xs) > 1 else xs[0] - 180]
    for i in range(1, len(xs)):
        bounds.append((xs[i - 1] + xs[i]) / 2)
    bounds.append(min(w, xs[-1] + ((xs[-1] - xs[-2]) if len(xs) > 1 else 260) * 0.8))
    gaps = [rows[i + 1]['cy'] - rows[i]['cy'] for i in range(len(rows) - 1) if rows[i + 1]['cy'] > rows[i]['cy']]
    if not gaps:
        return [], ['班级行间距异常，无法定位问题格。']
    gaps.sort()
    gap = gaps[len(gaps) // 2]
    out, warnings = [], []
    for ri, row in enumerate(rows):
        cls = _class_name(ctx.get('grade'), row['no'], classes)
        y0 = row['cy'] - gap * 0.46
        y1 = row['cy'] + gap * 0.46
        for pi, (period, _) in enumerate(period_cols):
            bx0, bx1 = bounds[pi], bounds[pi + 1]
            cell_tokens = []
            for t in tokens:
                if not (bx0 + 4 <= t['cx'] <= bx1 - 4 and y0 <= t['cy'] <= y1):
                    continue
                s = _norm_text(t['text'])
                if not s or s in STOPWORDS or _class_no(s) or s in ('✓', '√', 'V', 'v'):
                    continue
                if any(p in s and len(s) <= 6 for p in PERIODS):
                    continue
                cell_tokens.append(t)
            cell_tokens.sort(key=lambda z: z['x'])
            raw_whole = _fix_ocr_text(' / '.join(t['text'] for t in cell_tokens))
            ink = _cell_components(bgr, bx0 + 10, bx1 - 10, y0 + gap * 0.08, y1 - gap * 0.08)
            rule0 = _rule_match(raw_whole, rules, '') if raw_whole else None
            meaningful = bool(rule0) or any(x in raw_whole for x in ISSUE_WORDS)
            if ink < 5.0 and not meaningful:
                continue
            if not meaningful and ink < 6.5:
                continue
            # 整页 OCR 已被表线干扰；对疑似问题格放大后二次识别，手写短句提升最明显
            raw = raw_whole
            conf_cell = 0
            refined = False
            if refine and ink >= 2.0:
                cell_text, cell_conf = _ocr_cell_text(bgr, bx0 + 12, bx1 - 12, y0 + gap * 0.1, y1 - gap * 0.1)
                if cell_text:
                    raw = _fix_ocr_text(cell_text)
                    conf_cell = cell_conf
                    refined = True
            # 没有中文也没有命中细则的碎片（如边缘“5K”）不生成候选
            if raw and not re.search(r'[\u4e00-\u9fa5]', raw) and not _rule_match(raw, rules, ''):
                continue
            conf = int(round(conf_cell)) if conf_cell else (int(round(sum(t['confidence'] for t in cell_tokens) / len(cell_tokens))) if cell_tokens else 0)
            qty_whole = max(_extract_cell_qty(raw_whole) if raw_whole else 1, _extract_cell_qty(raw) if raw else 1)
            clauses = [x.strip() for x in re.split(r'[、,，;；/]', raw) if x.strip()] if raw else ['']
            # 去重且只保留有意义的短句，最多拆 3 项，避免一行手写拆得过碎
            tmp = []
            for x in clauses:
                if x not in tmp and (len(x) >= 2 or re.search(r'\d', x)):
                    tmp.append(x)
            clauses = tmp[:3] or ['']
            for ci, clause in enumerate(clauses):
                rule = _rule_match(clause, rules, '') if clause else None
                dept = (rule or {}).get('dept') or ctx.get('dept') or ''
                qty = _extract_cell_qty(clause) if clause else 1
                if len(clauses) == 1:
                    qty = max(qty, qty_whole)
                if rule:
                    item = rule.get('item') or clause[:40]
                    base_score = abs(float(rule.get('score') or 0))
                    score = base_score * qty
                    formula = '%s；按 %s分 × %d%s（口径待确认）' % (item, rule.get('score'), qty, '人' if '人' in clause else '处')
                else:
                    item = clause[:40] if clause else ''
                    score = 0
                    formula = '检测到问题格有手写，OCR 未可靠匹配细则'
                status, issue = 'ok', ''
                if not cls:
                    status, issue = 'error', '第%d班不在当前年级班级字典中，请核对年级或班级序号' % row['no']
                elif not clause or not rule or score <= 0:
                    status, issue = 'error', '手写内容未可靠识别，请对照原图补全事项和分值'
                elif qty > 1:
                    status, issue = 'error', '识别到数量 %d，请确认按人还是按班计分' % qty
                elif conf < 78:
                    status, issue = 'error', '手写识别置信度偏低，请对照原图'
                desc_base = ('逐格识别：' if refined else '识别原文：') + clause
                desc = desc_base if clause else '检测到该问题格有手写内容，OCR 未识别出可靠文字'
                cell_label = '%d班 × %s' % (row['no'], period)
                if len(clauses) > 1:
                    cell_label += '·%d' % (ci + 1)
                out.append(_base_candidate(
                    len(out) + 1, 'daily_matrix', item, qty, score, '人' if ('人' in clause or '名' in clause) else '项',
                    formula, desc + ('；检测墨迹 %.1f' % ink), conf, status, issue,
                target=cls, grade=_grade_from_cls(cls), cls=cls, dept=dept,
                roi=[int(bx0), int(y0), int(bx1), int(y1)],
                cell=cell_label,
            ))
    if not out:
        warnings.append('未从早读/午读/晚读问题格中识别出扣分事项。请确认照片方向、清晰度和表头完整性。')
    if len(out) >= 60:
        warnings.append('候选较多，已截取前 60 个问题格，请核对是否拍入了额外纸张。')
    return out[:60], warnings


def _dorm_is_printed_noise(text):
    s = _norm_text(text)
    if not s:
        return True
    if re.fullmatch(r'[一二三四五六七八]\s*楼', s):
        return True
    if any(k in s for k in ('扣分', '加分', '说明', '打分', '分说', '分设', '分税', '分谨', '内务', '纪律', '卫生', '房号', '检查人', '评分', '标准', '时段', '星期', '宿舍', '项目')):
        # 带明确扣分数值的仍可能是手写备注，保留
        if not re.search(r'[-−]\s*\d', s):
            return True
    return False

def _parse_dorm(tokens, bgr, ctx, rules, dorm_rows):
    h, w = bgr.shape[:2]
    floor_tokens = []
    for t in tokens:
        m = re.search(r'([一二三四五六七八])\s*楼', _norm_text(t['text']))
        if m:
            floor_tokens.append((CN_FLOOR[m.group(1)], t))
    if not floor_tokens:
        return [], ['未识别到左侧楼层（一楼、二楼……），无法组合房号。请拍全左侧楼层列。']
    first_floor_y = min(t['cy'] for _, t in floor_tokens)
    room_hits = {}
    for t in tokens:
        s = _norm_text(t['text'])
        m = re.match(r'^0?(\d{1,2})(?:\s|$)', s)
        if m and t['y'] < first_floor_y - 15:
            n = int(m.group(1))
            if 1 <= n <= 30 and t['cx'] > w * 0.12:
                room_hits.setdefault(n, []).append(t['cx'])
    room_x = {}
    for n, arr in room_hits.items():
        if arr:
            arr.sort()
            room_x[n] = arr[len(arr) // 2]
    if not room_x:
        return [], ['未识别到房号表头（01、02……），无法定位房间。请拍全表格上方的房号列。']
    # 补全 OCR 漏掉的房号列（按已识别房号间距线性推断）
    nums = sorted(room_x)
    if len(nums) >= 3:
        dx = [room_x[b] - room_x[a] for a, b in zip(nums, nums[1:]) if room_x[b] > room_x[a]]
        dx.sort()
        step = dx[len(dx) // 2] if dx else 60
        for n in range(1, min(30, max(nums)) + 1):
            if n not in room_x:
                near = min(nums, key=lambda k: abs(k - n))
                room_x[n] = room_x[near] + (n - near) * step
        room_x = {n: x for n, x in room_x.items() if w * 0.12 <= x <= w * 0.95}
    building = (ctx.get('building') or '').strip()
    if not building:
        for t in tokens[:30]:
            m = re.search(r'([\u4e00-\u9fa5]{1,6}(?:栋|楼))', _norm_text(t['text']))
            if m and '教学' not in m.group(1):
                building = m.group(1)
                break
    room_map = {}
    for r in dorm_rows or []:
        if building and r.get('building') and r['building'] != building:
            continue
        room = str(r.get('room') or '')
        if room:
            room_map.setdefault(room, [])
            if r.get('cls') and r['cls'] not in room_map[room]:
                room_map[room].append(r['cls'])

    floors = sorted(floor_tokens, key=lambda x: x[1]['cy'])
    fys = [t['cy'] for _, t in floors]
    bands = []
    for i, (fl, t) in enumerate(floors):
        top = (fys[i - 1] + fys[i]) / 2 if i else fys[i] - 75
        bottom = (fys[i] + fys[i + 1]) / 2 if i + 1 < len(fys) else fys[i] + 155
        bands.append((fl, top, bottom))
    step_x = 60.0
    if len(room_x) > 1:
        diffs = [abs(room_x[b] - room_x[a]) for a, b in zip(sorted(room_x), sorted(room_x)[1:]) if room_x[b] != room_x[a]]
        if diffs:
            diffs.sort()
            step_x = diffs[len(diffs) // 2]
    out, warnings, seen = [], [], set()
    for t in tokens:
        text = _norm_text(t['text'])
        if not text or len(text) < 2 or len(text) > 40:
            continue
        if t['cy'] < first_floor_y - 8 or _dorm_is_printed_noise(text) or _class_no(text):
            continue
        if not re.search(r'[\u4e00-\u9fa5]', text) and not re.search(r'\d', text):
            continue
        if t['cx'] < min(room_x.values()) - 18 or t['cx'] > max(room_x.values()) + step_x * 0.65:
            continue
        band = next((b for b in bands if b[1] <= t['cy'] < b[2]), None)
        if not band:
            continue
        floor = band[0]
        room_no = min(room_x, key=lambda n: abs(room_x[n] - t['cx']))
        if abs(room_x[room_no] - t['cx']) > max(42, step_x * 0.72):
            continue
        room = '%d%02d' % (floor, room_no)
        key = (room, re.sub(r'\d', '#', text))
        if key in seen:
            continue
        seen.add(key)
        rule = _rule_match(text, rules, ctx.get('dept'))
        item = (rule or {}).get('item') or text[:40]
        mscore = re.search(r'[-−]\s*(\d{1,2}(?:\.\d)?)', text) or re.search(r'扣\s*(\d{1,2}(?:\.\d)?)', text)
        score = float(mscore.group(1)) if mscore else abs(float((rule or {}).get('score') or 0))
        classes = room_map.get(room, [])
        status, issue = 'ok', ''
        if not mscore:
            status, issue = 'error', '未从手写中确认扣分值'
        if not classes:
            status, issue = 'error', '宿舍安排中未找到该房间对应班级'
        if not rule:
            status, issue = 'error', (issue + '；' if issue else '') + '未匹配现有宿舍细则'
        if t['confidence'] < 82:
            status, issue = 'error', '手写识别置信度偏低，请对照原图'
        out.append(_base_candidate(
            len(out) + 1, 'dorm_log', item, 1, score, '间', '宿舍纸表手写分值',
            '识别原文：%s；房间：%s' % (text, room), t['confidence'], status, issue,
            target=('%s宿舍 → %s' % (room, '、'.join(classes))) if classes else ('%s宿舍 → 待映射班级' % room),
            building=building, room=room, classes=classes, dept='宿管部',
            cell='%s × %02d' % (('一二三四五六七八'[floor - 1] + '楼'), room_no),
        ))
    if not building:
        warnings.append('未识别到楼栋名称，请在识别前选择楼栋。')
    if not out:
        warnings.append('未从宿舍问题格识别出可靠文字。宿舍手写识别难度较高，建议对照原图补录，或改用现有宿舍扣分页。')
    else:
        warnings.append('宿舍日志为手写密集表，只提取可定位的文字，所有候选均建议对照原图确认。')
    return out[:60], warnings


def _detect_header_meta(tokens, ctx=None):
    """纸表上可能叠着上一张表；优先选择“同年级标题正上方”的日期。"""
    import datetime
    grade = (ctx or {}).get('grade') or ''
    found = []
    for i, t in enumerate(tokens[:140]):
        s = _norm_text(t['text'])
        m = re.search(r'(20\d{2})\s*[年./-]\s*(\d{1,2})\s*[月./-]\s*(\d{1,2})\s*日?', s)
        if not m:
            continue
        try:
            d = datetime.date(int(m.group(1)), int(m.group(2)), int(m.group(3))).isoformat()
        except Exception:
            continue
        score = 0.0
        # 日期一般位于本表标题右下方 0~150px
        for q in tokens[max(0, i - 35):i + 2]:
            if q is t:
                continue
            qt = _norm_text(q['text'])
            dy = t['cy'] - q['cy']
            if not (-15 <= dy <= 150):
                continue
            if grade and grade in qt and ('量化' in qt or '年级' in qt or '检查' in qt):
                score = max(score, 20.0 - abs(dy - 35) / 10.0)
            elif ('日常量化' in qt or '检查汇总' in qt or '检查评价' in qt):
                score = max(score, 8.0 - abs(dy - 35) / 20.0)
        # 日期下方很快出现表头，说明它属于当前大表
        for q in tokens[i + 1:i + 28]:
            if _norm_text(q['text']) in ('委员签到', '早读', '午读', '晚读', '班级', '房号'):
                if 0 < q['cy'] - t['cy'] < 130:
                    score += 2.0
                break
        found.append((score, t['cy'], d))
    date_s = max(found)[2] if found else ''
    text = ' '.join(_norm_text(t['text']) for t in tokens[:140])
    session = ''
    if '下午放学' in text or '课后' in text:
        session = '下午放学'
    else:
        counts = {p: text.count(p) for p in PERIODS}
        if counts and max(counts.values()) > 0:
            session = max(counts, key=counts.get)
    return date_s, session

def analyze_paper(data: bytes, template_id: str, ctx: dict, rules=None, classes=None, dorm_rows=None):
    if template_id not in TEMPLATES:
        raise PaperError('未知纸表模板')
    bgr, pil = image_from_bytes(data)
    quality = image_quality(bgr)
    tokens, elapsed = _run_ocr(bgr)
    boxes = _detect_checkmarks(bgr) if template_id == 'after_class' else []
    detected_date, detected_session = _detect_header_meta(tokens, ctx)
    warnings = list(quality['issues'])
    if len(tokens) < 4:
        warnings.append('OCR 只识别到极少文字，请检查照片方向、清晰度与完整度。')
    if template_id == 'after_class':
        candidates, more = _parse_after(tokens, boxes, bgr, ctx, classes or {})
    elif template_id == 'daily_matrix':
        candidates, more = _parse_daily(tokens, bgr, ctx, classes or {}, rules or [], refine=True)
    else:
        candidates, more = _parse_dorm(tokens, bgr, ctx, rules or [], dorm_rows or [])
    warnings.extend(more)
    if detected_date:
        warnings.append('纸表日期识别为 %s，提交时优先采用纸表日期，请复核。' % detected_date)
    if detected_date and ctx.get('date') and detected_date != ctx.get('date'):
        warnings.append('当前选择日期 %s 与纸表日期 %s 不一致，已优先采用纸表日期。' % (ctx.get('date'), detected_date))
    # 统一给候选编号，避免解析器内部编号重复
    for i, c in enumerate(candidates, 1):
        c['id'] = 'p%d' % i
        c.setdefault('grade', _grade_from_cls(c.get('cls', '')))
    return {
        'template': template_id,
        'template_name': TEMPLATES[template_id]['name'],
        'width': quality['width'], 'height': quality['height'],
        'blur': quality['blur'], 'brightness': quality['brightness'],
        'quality_issues': quality['issues'], 'warnings': warnings[:14],
        'detected_date': detected_date, 'detected_session': detected_session,
        'candidates': candidates,
        'raw_texts': [_norm_text(t['text']) for t in tokens if _norm_text(t['text'])][:200],
        'ocr_count': len(tokens),
        'ocr_seconds': round(elapsed, 2),
        'checkboxes_seen': len(boxes),
    }
