"""复盘表单:下单类型与开仓类型校验(三所共用)."""
from __future__ import annotations

from typing import Optional, Sequence, Tuple

from lib.strategy.strategy_trade_labels import (
    JOURNAL_ORDER_TYPE_OPTIONS,
    STRATEGY_ENTRY_REASON_OPTIONS,
    normalize_journal_order_type,
)
from lib.trade.entry_model_lib import (
    TRADE_STYLE_FALLBACK_ENTRY_REASONS,
    normalize_review_entry_reason,
)

_LEGACY_JOURNAL_ENTRY_REASONS: Tuple[str, ...] = (
    *TRADE_STYLE_FALLBACK_ENTRY_REASONS,
    *STRATEGY_ENTRY_REASON_OPTIONS,
)


def normalize_journal_entry_reason(
    raw: Optional[str],
    allowed: Sequence[str],
    *,
    allow_legacy: bool = False,
) -> str:
    s = normalize_review_entry_reason(raw, allowed)
    if s:
        return s
    if not allow_legacy:
        return ""
    legacy = (raw or "").strip()
    if legacy in _LEGACY_JOURNAL_ENTRY_REASONS:
        return legacy
    return ""


def journal_entry_reason_valid(raw: Optional[str], allowed: Sequence[str]) -> bool:
    return bool(normalize_journal_entry_reason(raw, allowed, allow_legacy=False))


def journal_order_type_valid(raw: Optional[str]) -> bool:
    return bool(normalize_journal_order_type(raw))


def normalize_journal_direction(raw: Optional[str]) -> str:
    s = (raw or "").strip().lower()
    if s in ("long", "buy", "多", "做多"):
        return "long"
    if s in ("short", "sell", "空", "做空"):
        return "short"
    # 兼容旧隐藏字段 direction_hint
    return ""
