"""VP proactive personality curves — tsundere vs romance inference."""

from __future__ import annotations

import re
from typing import Any

from niannian.chat.proactive_scope import ProactivePersonaPolicy

_TSUNDERE_MARKERS = frozenset({"傲娇", "tsundere", "明日香", "asuka", "毒舌"})
_ROMANCE_MARKERS = frozenset({"恋爱", "romance", "女友", "老婆", "宝贝", "亲爱的"})


def _parse_frontmatter_float(md: str, key: str, default: float) -> float:
    m = re.search(rf"^{re.escape(key)}:\s*([0-9.]+)\s*$", md, re.MULTILINE)
    if not m:
        return default
    try:
        return float(m.group(1))
    except ValueError:
        return default


def load_vp_proactive_policy(vp_row: dict[str, Any] | None) -> ProactivePersonaPolicy:
    """Infer proactive traits from VP profile_md and meta."""
    row = vp_row or {}
    md = str(row.get("profile_md") or "")
    label = str(row.get("label") or "")
    blob = f"{label}\n{md}".lower()

    if any(m.lower() in blob for m in _TSUNDERE_MARKERS):
        return ProactivePersonaPolicy(
            clinginess=0.25,
            self_disclosure=0.2,
            user_future_care=0.15,
            interruptiveness=0.35,
        )
    if any(m.lower() in blob for m in _ROMANCE_MARKERS):
        return ProactivePersonaPolicy(
            clinginess=0.9,
            self_disclosure=0.85,
            user_future_care=0.9,
            interruptiveness=0.95,
        )

    return ProactivePersonaPolicy(
        clinginess=_parse_frontmatter_float(md, "clinginess", 0.5),
        self_disclosure=_parse_frontmatter_float(md, "self_disclosure", 0.5),
        user_future_care=_parse_frontmatter_float(md, "user_future_care", 0.5),
        interruptiveness=_parse_frontmatter_float(md, "interruptiveness", 0.5),
    )
