from django.db import models
import random
from django.contrib.auth.models import User
import string


class Coupon(models.Model):
    discount_functions = [(0,"CORE"), (1,"CUSTOMER"), (2,"LOYALTY"), (3,"GIFT_WITH_PURCHASE")]
    discount_methods = [(0,"POINT_REDEMPTION"), (1,"REDEEMED"), (2,"GIVE_AWAY"), (3,"BONUS_COUPON"), (4,"VOUCHER_GENERIC"), (5,"VOUCHER_LOYALTY"), (6,"VOUCHER_FUEL"), (7,"VOUCHER_THIRDPARTY")]
    discount_types = [(0,"AMOUNT"), (1,"PERCENT"), (2,"RE_PRICE"), (3,"ALT_PRICE")]
    how_often_apply = [(0,"ALL_APPLICABLE"), (1,"ONCE_PER_ORDER"), (2,"ONCE_PER_SELECTION")]
    qualification_types = [(0,"ALL"), (1,"ITEM"), (2,"ORDER")]
    rewards_types = [(0,"PURCHASES"), (1,"ITEMS"), (2,"VISIT")]

    def randomCouponCode():
        return ''.join(random.choices(string.ascii_uppercase + string.digits, k=10))

    def randomLoyaltyCode():
        return ''.join(random.choices(string.digits, k=10))

    name = models.CharField(max_length=50)
    discription = models.TextField(default="")
    pointsNeededForRedemption = models.IntegerField()
    couponCode = models.CharField(max_length=50, default=randomCouponCode)
    loyaltyCode = models.CharField(max_length=50, default=randomLoyaltyCode)
    discount = models.IntegerField()
    isExpired = models.BooleanField(default=False)
    expiryDate = models.DateTimeField(null=True, blank=True)
    discountFunction = models.IntegerField(choices=discount_functions, default=2)
    discountMethod = models.IntegerField(choices=discount_methods, default=0)
    discountType = models.IntegerField(choices=discount_types, default=0)
    howOftenApply = models.IntegerField(choices=how_often_apply, default=1)
    qualificationType = models.IntegerField(choices=qualification_types, default=2)
    rewardsType = models.IntegerField(choices=rewards_types, default=0)

    def __str__(self):
        return self.couponCode

class UserData(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    revelId = models.CharField(max_length=50, default="")
    refCode = models.CharField(max_length=50, default="")
    phone = models.CharField(max_length=15)
    birthDate = models.DateField()
    gender = models.CharField(max_length=10)
    streetName = models.CharField(max_length=50)
    city = models.CharField(max_length=50)
    state = models.CharField(max_length=50)
    country = models.CharField(max_length=50)
    pincode = models.CharField(max_length=10)
    otp = models.IntegerField(null=True, blank=True)
    rewardPoints = models.IntegerField(default=0)
    isVerified = models.BooleanField(default=False)
    isSubscribed = models.BooleanField(default=False)
    isBlocked = models.BooleanField(default=False)
    isDeleted = models.BooleanField(default=False)
    coupons = models.ManyToManyField(Coupon, blank=True, related_name='coupons')
    activatedCoupons = models.ManyToManyField(Coupon, blank=True, related_name='activatedCoupons')
    
    def __str__(self):
        return self.user.first_name + " " + self.user.last_name