from django import forms
from django.core.exceptions import ValidationError
from django.contrib.auth.models import User
from django.utils.translation import gettext_lazy as _
from .models import Company, Category, UserProfile
from django.utils import timezone

from Client.models import Bookings

class EmailForm(forms.Form):
	# subject = forms.CharField(max_length=100)
	# message = forms.CharField(required=True)
	email = forms.EmailField(required=True)
	def clean_email(self):
		email = self.cleaned_data['email']
		# raise Exception(email.split('@')[1])
		if email.split('@')[1] == 'hapsmiths.com':
			return email
		check = Company.objects.filter(domain=email.split('@')[1], status='a', expiry_date__gte=timezone.now()).count()
		if check == 0:
			raise ValidationError(_('Either this domain is not registered OR it is expired'))
		
		return email

class LoginForm(forms.Form):
	# subject = forms.CharField(max_length=100)
	otp = forms.CharField(required=True, max_length=4, min_length=4)
	email = forms.EmailField(required=True)
	def clean_email(self):
		data = self.cleaned_data['email']
		try:
			match = User.objects.get(email=data)
		except User.DoesNotExist:
			# Unable to find a user, this is fine
			raise ValidationError(_('Invalid email - Email does not exists'))
		return data

class ClientProfileForm(forms.Form):
	# category = forms.IntegerField(required=True)
	# def clean_category(self):
	# 	category = self.cleaned_data['category']
	# 	category_exists = Category.objects.filter(id=category).first()
	# 	if category_exists:
	# 		pass
	# 	else:
	# 		raise ValidationError(_('Invalid Category - Category field is not proper'))
	# 	return category
	name = forms.CharField(required=True)
	# removed for time being
	# gender = forms.CharField(required=True, max_length=20)
	# def clean_gender(self):
	# 	data = self.cleaned_data['gender']
	# 	if data == 'Male' or data=='Female' or data == 'Non Binary' or data == 'Rather Not Mention':
	# 		pass
	# 	else:
	# 		raise ValidationError(_('Invalid gender - Gender field is not proper'))
	# 	return data
	# dob = forms.CharField(required=True)
	profile_pic = forms.FileField(required=False)


# for messages
class SendMessageForm(forms.Form):
	message_from = forms.IntegerField(required=True)
	def clean_message_from(self):
		data = self.cleaned_data.get('message_from')
		# raise Exception(data)
		check = UserProfile.objects.filter(user_id=data, user__is_active=1).first()
		if check == None:
			raise ValidationError(_('Invalid From User - Either user does not exists or is blocked'))

		return data
	message_to = forms.IntegerField(required=True)
	def clean_message_to(self):
		data = self.cleaned_data['message_to']
		data1 = self.data.get('message_from')
		check = UserProfile.objects.filter(user_id=data, user__is_active=1).first()
		# raise Exception(check != None)
		if check == None:
			raise forms.ValidationError(_('Invalid To User - Either user does not exists or is blocked'))
		# raise Exception(data, data1)

		user_from = UserProfile.objects.filter(user_id=data1).first()
		# raise Exception(user_from, check.user_type, user_from.user_type, check.user_type == user_from.user_type)
		if check.user_type == user_from.user_type:
			raise ValidationError(_('Invalid Users - Users are of same type'))
		# raise Exception(check.category_id == 2 and user_from.user_type == 'client')

		if check.category_id == 2 and user_from.user_type == 'client':
			raise ValidationError(_('Invalid request - You can not send the message'))
		# raise Exception(check.category_id, user_from.user_type, check.category_id == 2 and user_from.user_type == 'client')
		return data
	booking_id = forms.IntegerField(required=True)
	def clean_booking_id(self):
		# raise Exception(self.cleaned_data, type(self.cleaned_data))
		user_to = self.data.get('message_to')
		user_from = self.data.get('message_from')
		data = self.cleaned_data['booking_id']
		# raise Exception(user_to, user_from, data, self.cleaned_data.get)
		user_type = None
		check_user_from = UserProfile.objects.filter(user_id=user_from).first()
		if check_user_from.user_type == 'client':
			user_id = user_from
			coach_id = user_to
		else:
			user_id = user_to
			coach_id = user_from

		# check for bookings is available and relationship is still there or not
		booking = Bookings.objects.filter(user_id=user_id, coach_id=coach_id, id=data).order_by('-id').first()
		if not booking:
			raise ValidationError(_('Either Booking does not exists or User associated with booking does not exists.'))
		if booking.is_relation_paused == 'yes':
			raise ValidationError(_('Your relation is paused, You can not send message to this user.'))
		return data
	# message = forms.CharField(required=True)
	# message_file = forms.FileField(required=False)
	# message_type = forms.CharField(required=True)

class OpenChatForm(forms.Form):
	# auth_user_id = forms.IntegerField(required=True)
	person_id = forms.IntegerField(required=True)
	# def clean_person_id(self):
	# 	data = self.cleaned_data['person_id']
	# 	# auth_user = self.cleaned_data['auth_user_id']
	# 	raise Exception(data)

class updateBookingStatusForm(forms.Form):
	booking_id = forms.IntegerField(required=True)
	def clean_booking_id(self):
		data = self.cleaned_data['booking_id']
		booking = Bookings.objects.filter(id=data).first()
		if booking == None:
			raise ValidationError(_('Invalid booking.'))
		return data
	person_id = forms.IntegerField(required=True)
	# status = forms.CharField(required=True)
	# def clean_status(self):
	# 	data = self.cleaned_data['status']
	# 	if data != "completed":

	# 		raise forms.ValidationError(_('Invalid status.'))
	# 	return data
