import hashlib
import re
from app.action_decider import decide_action
from app.action_mapper import map_action_decision
from app.config import settings
from app.persistence import save_action_run
from app.schemas import ActionDecision, AnalyzeRequest, AnalyzeResponse, UsageInfo
from app.task_service import create_task_from_action_result


async def analyze(request: AnalyzeRequest, raw_event_id: str | None = None) -> AnalyzeResponse:
    needs_review = False

    try:
        decision, action_result, usage, decision_source = await decide_action(request)
    except Exception as exc:
        needs_review = True
        decision = ActionDecision(
            action_code="REVIEW_MANUALLY",
            note=f"Falha na decisão Action Core: {exc}",
            confidence=0.0,
        )
        action_result = map_action_decision(decision)
        usage = UsageInfo(
            id=None,
            model=settings.openrouter_model,
            provider="error_fallback",
            prompt_tokens=0,
            completion_tokens=0,
            total_tokens=0,
            cost=0.0,
        )
        decision_source = "fallback"

    if str(action_result.action_code or "").upper() == "IGNORE_BOUNCE":
        # v4.8.5: NDR/bounce emails are already readable in Chatwoot/Thunderbird.
        # They must not create ClientFlow work, opportunities or fiscal customers.
        return AnalyzeResponse(
            app="ClientFlow",
            model=settings.openrouter_model,
            action_decision=decision,
            action_result=action_result,
            usage=usage,
            needs_review=False,
            action_run_id=None,
            message_id=None,
            task_id=None,
        )

    if not action_result.safe_to_post:
        needs_review = True

    action_run_id, message_id = save_action_run(
        request=request,
        action_decision=decision,
        action_result=action_result,
        usage=usage,
        needs_review=needs_review,
        model=settings.openrouter_model,
        decision_source=decision_source,
        raw_body=request.last_customer_message,
        clean_body=request.last_customer_message,
        raw_event_id=raw_event_id,
    )

    normalized_message_for_idem = re.sub(r"\s+", " ", str(request.last_customer_message or "").strip().casefold())
    message_fingerprint = hashlib.sha256(normalized_message_for_idem.encode("utf-8")).hexdigest() if normalized_message_for_idem else ""

    task_id = create_task_from_action_result(
        action_result=action_result,
        action_run_id=action_run_id,
        message_id=message_id,
        raw_event_id=raw_event_id,
        conversation_id=request.conversation_id,
        contact_id=request.contact_id,
        source_system=request.source or "manual",
        source_event_id=message_id,
        metadata={
            "created_from_analyzer": True,
            "content_fingerprint": message_fingerprint,
            "normalized_message_preview": normalized_message_for_idem[:240],
            "needs_review": needs_review,
            "decision_source": decision_source,
            "llm_customer_intent": getattr(decision, "customer_intent", ""),
            "llm_evidence": getattr(decision, "evidence", ""),
            "llm_history_used": getattr(decision, "history_used", False),
            "payment_intent": getattr(decision, "payment_intent", None),
            "llm_confidence": getattr(decision, "confidence", 0.0),
        },
    )

    return AnalyzeResponse(
        app="ClientFlow",
        model=settings.openrouter_model,
        action_decision=decision,
        action_result=action_result,
        usage=usage,
        needs_review=needs_review,
        action_run_id=action_run_id,
        message_id=message_id,
        task_id=task_id,
    )
