export type LicenceState = "valid" | "warn" | "expired" | "none";

/** Amber inside 14 days, matching the window staff chase renewals on. */
export const EXPIRY_WARNING_DAYS = 14;

export function daysRemaining(iso: string): number {
    const ms = new Date(iso).getTime() - Date.now();
    return Math.ceil(ms / 86_400_000);
}

export function licenceState(expiresAt: string | undefined, hasLicence: boolean): LicenceState {
    if (!hasLicence || !expiresAt) return "none";
    const days = daysRemaining(expiresAt);
    if (days <= 0) return "expired";
    if (days <= EXPIRY_WARNING_DAYS) return "warn";
    return "valid";
}

export function formatDate(iso: string): string {
    return new Date(iso).toLocaleDateString("en-GB", {
        day: "numeric",
        month: "short",
        year: "numeric",
    });
}

export function formatStamp(iso: string): string {
    return `${new Date(iso).toISOString().slice(11, 19)} UTC`;
}

export function limitLabel(n: number): string {
    return n === -1 ? "unlimited" : String(n);
}
