/**
 * Boredom — the factory's work evaporating off THE SCREEN.
 *
 * If nothing ships for a long stretch, the sky slowly forgets. The corruption LEDGER is
 * untouched: history is not erased, only the display decays. So when production resumes the
 * targets are instantly back where they were and the picture snaps home in about a second.
 *
 * The horizon is deliberately generous. A jam, a rebuild, a rethink of the bus — none of
 * those are supposed to read as punishment. The player should have to genuinely walk away
 * before the sky starts cleaning itself.
 */

/** No shipments for this long before the sky begins to forget at all. */
export const BOREDOM_GRACE_SECONDS = 60;
/** ...and this long again to fade from full corruption to clean. */
export const BOREDOM_HORIZON_SECONDS = 180;

const clamp01 = (x: number) => Math.min(1, Math.max(0, x));

/**
 * How much of the ledger's corruption is still on display, 0..1.
 * 1 = everything you earned is showing; 0 = the sky has forgotten (the tallies have not).
 *
 * `rot` (round 5 jammed-rot) accelerates forgetting. A CLEAN idle — the player walked away —
 * decays at the base rate. A factory JAMMED SOLID rots faster: the work isn't resting, it's
 * spoiling, and the sky should say so. rot 0 = clean idle; rot 1 = everything jammed.
 */
export function freshness(idleSeconds: number, rot = 0): number {
  if (!(idleSeconds > BOREDOM_GRACE_SECONDS)) return 1; // NaN-safe: unknown idle reads as fresh
  // A jammed factory shortens the grace and steepens the decay. At full rot the horizon is
  // roughly a third, so a wholly-stuck line visibly sags in tens of seconds, not minutes.
  const r = clamp01(rot);
  const grace = BOREDOM_GRACE_SECONDS * (1 - 0.5 * r);
  const horizon = BOREDOM_HORIZON_SECONDS * (1 - 0.66 * r);
  if (idleSeconds <= grace) return 1;
  return 1 - clamp01((idleSeconds - grace) / horizon);
}
