import random def get_lead_score(stage, emails, meetings, close_gap, amount): score_ranges = { "Prospecting": (20, 40), "Proposal/Price Quote": (50, 70), "Negotiation": (70, 90), "Closed Won": (90, 100), "Closed Lost": (10, 30) } base = random.randint(*score_ranges.get(stage, (30, 60))) bonus = 0 if close_gap < 10: bonus += 5 if emails + meetings > 4: bonus += 3 if amount > 100000: bonus += 5 return min(100, base + bonus) def calculate_score(lead_score, emails, meetings, close_gap, amount): engagement_weight = (emails * 2 + meetings * 3) urgency = max(0, 10 - int(close_gap / 5)) size_factor = 5 if amount > 75000 else 0 deduction = random.randint(5, 15) return min(100, lead_score + engagement_weight + urgency + size_factor - deduction) def calculate_confidence(score): return round(min(0.95, 0.6 + score / 200), 2) def calculate_risk(score, confidence, emails, meetings): if score > 85 and confidence > 0.9: return "Low" if score < 55 or (emails + meetings) <= 2: return "Critical" return "Medium"