# SPDX-License-Identifier: Apache-2.0
"""Retention (SPEC §5): the highest level content may still be read at a given age."""
from __future__ import annotations


def readable_level(age: float, schedule, limit: float | None = None) -> int:
    """schedule: ((until, max_level), …) with increasing ages and decreasing levels, in seconds.
    limit: a shortening agreed in the session or by the voice — from then on, nothing is kept."""
    if limit is not None and age >= limit:
        return 0
    for until, level in schedule:
        if age < until:
            return level
    return 0


def unreadable_after(level: int, schedule, limit: float | None = None) -> float:
    """Age (s) after which content kept at `level` must be unrecoverable (e.g. its key destroyed)."""
    last = 0.0
    for until, lv in schedule:
        if lv >= level:
            last = until
    return min(last, limit) if limit is not None else last
