"""Universal class-variant resolution for cross-class isolation."""

def allowed_classes_for(student_class):
    """Return all paper-storage variants for a given student class.

    Examples:
        '9'   -> ['9', '9th', '9th class', '9a', '9b', '9c']
        '10'  -> ['10', '10th', '10th class', '10a', '10b', '10c']
        'KG'  -> ['KG', 'kg', 'Kindergarten']
        'play group' -> ['play group', 'Play Group', 'playgroup']
    """
    if not student_class:
        return []
    c = str(student_class).strip().lower()
    base = c.replace('th', '').replace(' class', '').strip()
    variants = {c, base, base + 'th', base + 'th class'}
    for suffix in ('a', 'b', 'c'):
        variants.add(base + suffix)
    if 'kg' in c or 'kinder' in c:
        variants.update(['KG', 'kg', 'Kindergarten', 'kindergarten'])
    if 'play' in c:
        variants.update(['play group', 'Play Group', 'playgroup'])
    if 'prep' in c:
        variants.update(['prep', 'Prep', 'prep class'])
    return sorted(variants)


def is_personalized_student(student):
    """A student is 'personalized' ONLY if they have no enrolled books AND
    have never selected from a standard paper. Non-9th is NOT automatically
    personalized — it just means their content comes from their class papers."""
    if not student:
        return False
    if student.enrolled_subjects:
        return False
    from exams.models import ModelPaper
    has_standard = ModelPaper.objects.filter(
        student_class__in=allowed_classes_for(student.student_class),
    ).exclude(
        code__startswith='SELF-'
    ).exclude(
        code__startswith='CUSTOM-'
    ).exists()
    return not has_standard
