const SPY_AGE_FRESH_DAYS = 30;
const SPY_AGE_STALE_DAYS = 90;

const SPY_AGE_COLOR_FRESH = "#4caf50";
const SPY_AGE_COLOR_STALE = "#ffc107";
const SPY_AGE_COLOR_ANCIENT = "#f44336";
const SPY_AGE_COLOR_UNKNOWN = "#f1f1f1";

function spyAgeInSeconds(epochSeconds) {
    if (!epochSeconds) return null;
    return Math.max(0, Math.floor(Date.now() / 1000) - epochSeconds);
}

function formatSpyAge(epochSeconds) {
    const age = spyAgeInSeconds(epochSeconds);
    if (age === null) return null;

    const hours = age / 3600;
    if (hours < 1) return "<1h";
    if (hours < 24) return `${Math.floor(hours)}h`;

    const days = hours / 24;
    if (days < SPY_AGE_FRESH_DAYS) return `${Math.floor(days)}d`;
    if (days < 365) return `${Math.floor(days / 30)}mo`;

    return `${(days / 365).toFixed(1)}y`;
}

function spyAgeColor(epochSeconds) {
    const age = spyAgeInSeconds(epochSeconds);
    if (age === null) return SPY_AGE_COLOR_UNKNOWN;

    const days = age / 86400;
    if (days < SPY_AGE_FRESH_DAYS) return SPY_AGE_COLOR_FRESH;
    if (days < SPY_AGE_STALE_DAYS) return SPY_AGE_COLOR_STALE;
    return SPY_AGE_COLOR_ANCIENT;
}
