#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
ANDYGROVE.SYSTEMS — 5-Minute Campus Operations Research & Hybrid Agent Seminar Lab Kit
University Edition: Ollscoil Luimnigh (University of Limerick) | Country: United Kingdom
Department / Track: MSc in Data Science and Statistical Learning & BSc in Immersive Software Engineering (ISE)
Recommended Part-Time R&D Pod: POD-02 ($20-$45/hr USD, 10-25 hrs/wk, 0 hrs Exam-Safe Pause)
Apply & Preprints: https://andygrove.systems/uni/gb-university-of-limerick | https://andygrove.systems/preprints
Notice: Independent; not affiliated with Intel Corporation or the Grove family
"""
import math

def littles_law_wip(arrival_lambda: float, lead_time_hours: float) -> float:
    """Little's Law (Little, 1961): L = lambda * W"""
    return arrival_lambda * lead_time_hours

def kingman_vut_queue_wait_minutes(c_a_sq: float, c_s_sq: float, u_util: float, service_time_min: float) -> float:
    """Kingman's VUT Approximation (1961): W_q = ((c_a^2 + c_s^2) / 2) * (u / (1 - u)) * t_s"""
    if u_util >= 1.0:
        return float("inf")
    return 0.5 * (c_a_sq + c_s_sq) * (u_util / max(1.0 - u_util, 1e-9)) * service_time_min

def wilson_95_lcb(successes: int, total: int) -> tuple[float, str]:
    """Wilson (1927) 95% Lower Confidence Bound -> Grove L0-L3 Task-Relevant Maturity Gate"""
    if total <= 0:
        return 0.0, "L0 (Shadow Only)"
    z = 1.959964
    p_hat = successes / total
    denom = 1.0 + (z * z) / total
    center = p_hat + (z * z) / (2.0 * total)
    rad = z * math.sqrt((p_hat * (1.0 - p_hat) + (z * z) / (4.0 * total)) / total)
    lcb = max(0.0, (center - rad) / denom)
    if lcb >= 0.97:
        lvl = "L3 (Autonomous Commit on Reversible Slices + Exception Pod)"
    elif lcb >= 0.90:
        lvl = "L2 (10-15% Stratified SPC Lot Sampling)"
    elif lcb >= 0.75:
        lvl = "L1 (100% Structured Human Verification Gate)"
    else:
        lvl = "L0 (Shadow Telemetry Only)"
    return lcb, lvl

def has_positive_managerial_leverage(t_rev_min: float, p_err: float, t_fix_min: float, t_manual_min: float) -> bool:
    """Grove Ch. 3 Extension: Positive leverage iff t_rev + p_err * t_fix < t_manual"""
    return (t_rev_min + p_err * t_fix_min) < t_manual_min

if __name__ == "__main__":
    w_72 = kingman_vut_queue_wait_minutes(c_a_sq=1.35, c_s_sq=1.65, u_util=0.72, service_time_min=6.0)
    w_95 = kingman_vut_queue_wait_minutes(c_a_sq=1.35, c_s_sq=1.65, u_util=0.95, service_time_min=6.0)
    lcb_val, trm_gate = wilson_95_lcb(successes=388, total=400)
    pos_lev = has_positive_managerial_leverage(t_rev_min=2.4, p_err=0.04, t_fix_min=18.0, t_manual_min=14.0)
    print(f"[University of Limerick] Kingman Human Verifier Queue Wait @ u=0.72: {w_72:.1f} min vs @ u=0.95: {w_95:.1f} min ({w_95/w_72:.1f}x explosion)")
    print(f"[University of Limerick] Little's Law WIP @ lambda=48/hr, W={((w_72 + 6.0)/60.0):.2f}h -> L = {littles_law_wip(48.0, (w_72 + 6.0)/60.0):.1f} active dossiers")
    print(f"[University of Limerick] Eval Suite (388/400): Wilson 95% LCB = {lcb_val:.4f} -> {trm_gate}")
    print(f"[University of Limerick] Positive Managerial Leverage Check (no negative-leverage rewrites): {pos_lev}")
