from exams.models import Student
from engagement.models import EngagementDailyRollup

def efficiency_score(user, days=7):
    if not hasattr(user, 'student'):
        return 0
    student = user.student
    rollups = EngagementDailyRollup.objects.filter(user=user).order_by('-date')[:days]
    total_sec = sum(r.total_seconds for r in rollups)
    total_hours = total_sec / 3600.0
    
    # Simple tier-based target: Tier 1 = 10 hrs/wk, Tier 5 = 3 hrs/wk
    target_hours = max(3.0, 13.0 - (student.tier * 2.0))
    score = min(100, int((total_hours / target_hours) * 100))
    return score
