"""
app/api/v1/review.py — 审核路由（组长）
通过(+5，最重) / 打回(写原因，-3，回 pending_selection)
打回原因存 preference_events.reason，不做 AI 归纳。
"""

import logging
from datetime import datetime, timezone
from typing import Annotated

from fastapi import APIRouter, Depends
from pydantic import BaseModel
from sqlalchemy.orm import Session

from app.core.database import get_db
from app.core.response import ok, paginate, raise_not_found, raise_state_invalid
from app.middleware.workspace_guard import CurrentUser, require_supervisor_or_above
from app.models.task import GenerationTask, ImageCandidate, TextCandidate
from app.models.user import User

logger = logging.getLogger(__name__)
router = APIRouter(prefix="/review", tags=["review"])


class RejectRequest(BaseModel):
    reason: str   # 原文存入 preference_events，不做 AI 归纳（契约§3）


@router.get("/queue")
def review_queue(
    page: int = 1, page_size: int = 20,
    current_user: Annotated[CurrentUser, Depends(require_supervisor_or_above)] = None,
    db: Session = Depends(get_db),
):
    """待审队列（pending_review 状态）。"""
    q = (
        db.query(GenerationTask)
        .filter(
            GenerationTask.workspace_id == current_user.workspace_id,
            GenerationTask.status == "pending_review",
        )
        .order_by(GenerationTask.updated_at.asc())
    )
    total = q.count()
    tasks = q.offset((page - 1) * page_size).limit(page_size).all()
    items = [_fmt_queue_item(t, db) for t in tasks]
    return ok(paginate(items, total, page, page_size))


def _fmt_queue_item(t: GenerationTask, db: Session) -> dict:
    import json
    from app.models.product import Product
    product = db.query(Product).filter(Product.id == t.product_id).first()
    operator = db.query(User).filter(User.id == t.operator_id).first()
    selected_text = db.query(TextCandidate).filter(
        TextCandidate.task_id == t.id, TextCandidate.is_selected == True
    ).first()
    selected_image = db.query(ImageCandidate).filter(
        ImageCandidate.task_id == t.id, ImageCandidate.is_selected == True
    ).first()

    # content 列存 JSON dict，解包取 title/content 分字段返回
    text_parsed: dict = {}
    if selected_text and selected_text.content:
        try:
            text_parsed = json.loads(selected_text.content)
        except (json.JSONDecodeError, ValueError):
            text_parsed = {"content": selected_text.content}

    return {
        "task_id": t.id,
        "product_name": product.name if product else None,
        "theme": t.theme,
        "submitted_at": t.updated_at.isoformat(),
        "operator_name": operator.username if operator else None,
        "selected_text": {
            "candidate_id": selected_text.id,
            "angle_label": selected_text.angle_label,
            "title": text_parsed.get("title", ""),
            "content": text_parsed.get("content", ""),   # 纯正文
            "tags": text_parsed.get("tags", []),
        } if selected_text else None,
        "selected_image": {
            "candidate_id": selected_image.id,
            "strategy": selected_image.strategy,
            "url": selected_image.url,
        } if selected_image else None,
    }


@router.post("/{task_id}/approve")
def approve_task(
    task_id: int,
    current_user: Annotated[CurrentUser, Depends(require_supervisor_or_above)] = None,
    db: Session = Depends(get_db),
):
    """通过（飞轮 +5，最重，状态 → approved）。"""
    from app.services.flywheel_service import record_signal
    task = db.query(GenerationTask).filter(
        GenerationTask.id == task_id,
        GenerationTask.workspace_id == current_user.workspace_id,
    ).first()
    if not task:
        raise_not_found("任务不存在")
    if task.status != "pending_review":
        raise_state_invalid("只有 pending_review 状态可审核")
    task.status = "approved"
    task.review_status = "approved"
    task.reviewer_id = current_user.user_id
    task.reviewed_at = datetime.now(timezone.utc)
    task.approved_at = datetime.now(timezone.utc)
    db.commit()
    record_signal(db, current_user, task, "approve")
    return ok({"task_id": task_id, "status": "approved"})


@router.post("/{task_id}/reject")
def reject_task(
    task_id: int, body: RejectRequest,
    current_user: Annotated[CurrentUser, Depends(require_supervisor_or_above)] = None,
    db: Session = Depends(get_db),
):
    """打回（飞轮 -3，回 pending_selection，原因下次注入 prompt）。"""
    from app.services.flywheel_service import record_signal
    task = db.query(GenerationTask).filter(
        GenerationTask.id == task_id,
        GenerationTask.workspace_id == current_user.workspace_id,
    ).first()
    if not task:
        raise_not_found("任务不存在")
    if task.status != "pending_review":
        raise_state_invalid("只有 pending_review 状态可打回")
    task.status = "pending_selection"   # 打回回 pending_selection
    task.review_status = "rejected"
    task.reviewer_id = current_user.user_id
    task.reviewed_at = datetime.now(timezone.utc)
    task.reject_reason = body.reason
    db.commit()
    record_signal(db, current_user, task, "reject_with_reason", reason=body.reason)
    return ok({"task_id": task_id, "status": "pending_selection"})
