"""
MRR (Monthly Recurring Revenue) normalisation helpers.

All amounts are in the same currency unit as stored (not cents).
"""


def normalize_to_mrr(amount: float, interval: str, interval_value: int) -> float:
    """
    Normalize a subscription amount to its Monthly Recurring Revenue equivalent.

    Calculation:
        day       →  amount * (365 / 12) / interval_value
        week      →  amount * (52 / 12) / interval_value
        month     →  amount / interval_value
        quarter   →  amount / (3 * interval_value)
        year      →  amount / (12 * interval_value)

    Args:
        amount: per-cycle charge amount
        interval: "day" | "week" | "month" | "quarter" | "year"
        interval_value: cycle multiplier

    Returns:
        Normalised monthly amount (float, 2 decimal precision).
    """
    if interval_value <= 0:
        interval_value = 1

    interval = interval.lower()
    if interval == "day":
        monthly = amount * (365 / 12) / interval_value
    elif interval == "week":
        monthly = amount * (52 / 12) / interval_value
    elif interval == "month":
        monthly = amount / interval_value
    elif interval == "quarter":
        monthly = amount / (3 * interval_value)
    elif interval == "year":
        monthly = amount / (12 * interval_value)
    else:
        monthly = amount

    return round(monthly, 2)
