from .models import *
from customer_api.models import *
from datetime import date
from datetime import timedelta
from django.utils import timezone
from django.core.mail import send_mail
from rest_framework.response import Response
from celery import shared_task
# WARNING: Commenting the code below can have unintended consequences!
# Only comment if you understand the implications.
# OR don't touch the code if you touch you die.

# This code always check subscritpion on server side  if subscritpion expired then all the features are revoked.

# Advance level coding structure Warning don't touch it.
@shared_task
def check_subscription_expiry():
	try:
		print('check subscription')
		active_plans = CustomersSubscriptions.objects.filter(is_active=True, sub_expiry_date__lt=date.today())
		for plan in active_plans:
			plan.is_active = False
			plan.save()

			try:
				customer_obj = Customer.objects.get(id=plan.customer.id)
				customer_obj.has_subscription = False
				customer_obj.save()
			except Customer.DoesNotExist:
				pass

			ad_models = [BussinessForSale, HouseWantedForRent, InvestmentMyProject, InvestmentMyCompany, PropertyProject, SaleProperty, RentProperty]
			for ad_model in ad_models:
				ad_model.objects.filter(customerId=plan.customer, end_date__isnull=True).update(subscription_type=0)
	except Exception as e:
		print(f"An error occurred while checking subscription expiry: {e}")
# end 
# This is the basic structure to handle subscritpion
""" def check_subscription_expiry():
	active_plans = CustomersSubscriptions.objects.filter(is_active=True)
	print('active_plans',active_plans)
	today = date.today()
	for plans in active_plans:
		if plans.sub_expiry_date < today:
			plans.is_active = False
			plans.save()
			customerObj=Customer.objects.filter(id=plans.customer.id)
			print('customerObj',customerObj)
			customerObj.has_subscription = True
			customerObj.save()
			ads = [BussinessForSale, HouseWantedForRent, InvestmentMyProject, InvestmentMyCompany, PropertyProject, SaleProperty, RentProperty]
			for ad in ads:
				ad.objects.filter(customerId=plans.customer, end_date__isnull=True).update(subscription_type=0)
		else:
			pass
"""
# end of code 

def subscription_reminder(self):
	print("subscription reminder")
	three_days_from_now = timezone.now() + timedelta(days=3)
	two_days_from_now = timezone.now() + timedelta(days=2)
	one_days_from_now = timezone.now() + timedelta(days=1)
        
	expiring_subscriptions = CustomersSubscriptions.objects.filter(sub_expiry_date__in=[three_days_from_now, two_days_from_now, one_days_from_now])
	print(expiring_subscriptions)
	for subscription in expiring_subscriptions:
		print("Success",subscription)
		subject = 'Your subscription is about to expire'
		message = f'Hello {subscription.customer.firstName} {subscription.customer.lastName}, your subscription will expire on {subscription.sub_expiry_date}. Please renew your subscription to continue using our service.'
		from_email = 'testsingh28@gmail.com'
		recipient_list = [subscription.customer.email]
		send_mail(subject, message, from_email, recipient_list)

			
