"""POST /api/v1/parse-ocr —— OCR 文本识别接口(占坑期 mock 实现)。

application/json:
  {"ocr_text": "...", "clusters": [{"id":"<uuid|long-str>","title":"..."}, ...]}

客户端在本地 OCR(或截图)后把整段文本发来,后端从文本里抠出
title/price/source_app + 归簇 + 估常见价。占坑期接 mock_extractor;
接真模型时改这里调真实 extractor_ocr。

注:此前 main.py 引用了本 router 但文件缺失,会在启动时 ImportError —— 本文件补齐。
"""
from __future__ import annotations

import logging

from fastapi import APIRouter, HTTPException

from app.mock_extractor import mock_extract_ocr
from app.schemas import ParseOcrRequest, ParseStrResponse

logger = logging.getLogger("app.parse_ocr")

router = APIRouter(prefix="/api/v1")


@router.post("/parse-ocr", response_model=ParseStrResponse)
async def parse_ocr(req: ParseOcrRequest) -> ParseStrResponse:
    text = req.ocr_text.strip()
    if not text:
        raise HTTPException(status_code=422, detail="empty_ocr_text")

    logger.info("parse_ocr request chars=%d clusters=%d", len(text), len(req.clusters))

    result = await mock_extract_ocr(text, req.clusters)
    if not result.get("success"):
        raise HTTPException(status_code=422, detail=result.get("reason", "extract_failed"))

    return ParseStrResponse(
        title=result["title"],
        price=result["price"],
        source_app=result["source_app"],
        cluster_id=result.get("cluster_id"),
        typical_price=result["typical_price"],
    )
