from __future__ import annotations

from decimal import Decimal, ROUND_HALF_UP


VALID_CT_VALUES = {"是", "否"}


def build_ct_filter(ct: str) -> tuple[str]:
    normalized_ct = ct.strip()
    if normalized_ct not in VALID_CT_VALUES:
        raise ValueError("统计CT必须是 是 或 否")
    return (normalized_ct,)


def format_wan_amount(amount: Decimal | int | float | None) -> str:
    value = Decimal("0") if amount is None else Decimal(str(amount))
    return format(value.normalize(), "f")


def format_share(share: Decimal | int | float | None) -> str:
    if share is None:
        return "/"
    value = Decimal(str(share)).quantize(Decimal("0.1"), rounding=ROUND_HALF_UP)
    return f"{value:.1f}%"


def format_pp_change(
    current_share: Decimal | int | float | None,
    baseline_share: Decimal | int | float | None,
) -> str:
    if current_share is None or baseline_share is None:
        return "/"
    change = (Decimal(str(current_share)) - Decimal(str(baseline_share))).quantize(
        Decimal("0.1"),
        rounding=ROUND_HALF_UP,
    )
    return f"{change:.1f}"

