from django.contrib.auth.models import User, Group
from django.shortcuts import redirect
from .models import Student, RebateReq, Meal, OverheadReq

def is_manager(user):
	return user.groups.filter(name='manager').exists()

def is_worker(user):
	return user.groups.filter(name='worker').exists()

def is_student(user):
	return user.groups.filter(name='student').exists()

def generate_pending_rebates():
	context = {'pending_rebates':[]}
	for reb in RebateReq.objects.filter(status='W'):
		context["pending_rebates"].extend([{
			'from': str(reb.fromDate),
			'to': str(reb.toDate),
			'ldap':reb.student.user.username,
			'name':f'{reb.student.user.first_name+reb.student.user.last_name}',
			'duration': str((reb.toDate-reb.fromDate).days)
			}])
	if len(context["pending_rebates"])==0:
		return None
	else:
		return context

def homepage_redirect(request):
	user=request.user
	if is_manager(user):
		return redirect('manager-homepage')
	elif is_worker(user):
		return redirect('worker-homepage')
	else: #is_student(user):
		return redirect('student-homepage')