from Subscriptions.models import RegistrationForm, Student
from django import template
import re

register = template.Library()

@register.filter
def getRegistrationPaymentPlan(pk):
    registerForm = RegistrationForm.objects.get(student=pk)
    if registerForm.registration_payment_plan:
        return registerForm.registration_payment_plan.plan_type.label
    return ""

@register.filter
def getTuitionPaymentPlan(pk):
    registerForm = RegistrationForm.objects.get(student=pk)
    if registerForm.tuition_payment_plan:
        return registerForm.tuition_payment_plan.plan_type.label
    return ""

@register.filter
def getRegistrationPaymentMethod(pk):
    registerForm = RegistrationForm.objects.get(student=pk)
    if registerForm.registration_payment:
        return registerForm.registration_payment.mode.label
    return ""

@register.filter
def getTuitionPaymentMethod(pk):
    registerForm = RegistrationForm.objects.get(student=pk)
    if registerForm.tuition_payment:
        return registerForm.tuition_payment.mode.label
    return ""

@register.filter
def getStudentLevel(pk):
    registerForm = RegistrationForm.objects.get(student=pk)
    level = registerForm.student.level
    if level:
        return level.name
    return ""

@register.filter
def getStudentGender(pk):
    registerForm = RegistrationForm.objects.get(student=pk)
    return Student.StudentGender(int(registerForm.student.gender)).label

@register.filter
def phone_format(value):
    if value.startswith("+33"):
        value = value.replace("+33", "+33 ")
    return re.sub(r"(\d{2})", r"\1 ", value).strip()